playMaker

Author Topic: Array Contains strange behaviour[SOLVED]  (Read 3745 times)

wmsgva

  • Playmaker Newbie
  • *
  • Posts: 8
Array Contains strange behaviour[SOLVED]
« on: January 21, 2018, 02:54:50 PM »
I have encountered a strange issue within one of my FSMs. One of its actions seeks a string value in a Global string array using Array Contains.

If the value I'm seeking has been set in the array within the editor, the FSM does detect the value in the array. However, if I start with the global array empty, then in some part of the game add values to the array, then test, the tested value is not detected (array contains returns -1).

In the attached screenshot, the situation when the global array (EventConditions) had had values added to it by code (values "Blue", "True", "Night", "0") and I'm trying to see if it contains the string value "Blue" contained in local variable "CondVariable". As you can see in debug, the array does contain the added values, we are testing "Blue", the value located at array index 0, yet the array contains function is returning -1 !!!

Am I missing something? The only reason I can see is that the FSM in which I'm using array contains is a prefab (needed as it is executed from an FSM above using Array for each).

Any help would be greatly appreciated.
« Last Edit: January 25, 2018, 02:16:15 AM by jeanfabre »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Array Contains strange behaviour
« Reply #1 on: January 21, 2018, 04:41:47 PM »
Hi.
i just tested with some manual values and it seems to be okay (latest unity and PM version),

Maybe the 'CondVariable' has a space behind 'Blue'?

Try with some manual values to find the issue.

If it returns a -1 it means that it was not found and there must be some difference.
Here is a unity doc about it

And i made a gif from my test :)


wmsgva

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Array Contains strange behaviour
« Reply #2 on: January 22, 2018, 03:12:45 AM »
No, there's no space in the CondVariable and as you can see in the screenshot below in another FSM, the value "Blue" transferred into the global array is 4 characters long (In this FSM, we moved the values "Blue" and "True" into the global array).

wmsgva

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Array Contains strange behaviour
« Reply #3 on: January 22, 2018, 09:04:24 AM »
Investigated further and have realised that the problem is not with the array.contains action, but with reading the content of the global array.

As you can see in the attached picture, the EventsCondition global array contains 4 values ("Blue","True","Night","0"), so to see why the array.contains wasn't working, I decided to retrieve the first value in the array and then compare it to my CondVariable (which contains "Blue").

Interesting result: Using Array.Get on EventsCondition is not returning "Blue" as expected in my Test variable (which I later compare to CondVariable), but "empty". Yet, in debug mode, you can see that EventsCondition does contain the 4 values!

In this case, I'm in an FSM that is 2 levels below the "top" level. A top level FSM has an array.for each action, which calls another prefab FSM, which also contains an array.for each, which calls this FSM prefab. As EventsCondition is a global variable, I don't think this should be an issue, but it's the only thing I can think of.

Any ideas?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Array Contains strange behaviour
« Reply #4 on: January 22, 2018, 11:09:41 AM »
Hi.
Are you placing this data in to the global array @ runtime?

Maybe somehow it happens 'after' using "Array Contains" and the array is still empty.

Try setting a breakpoint on the state that is using array contains (right click on the state and select 'Toggle Breakpoint'

If that is not the case, i can have a look (you can use colaborate and add me)

But do make a backup before you do.

wmsgva

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Array Contains strange behaviour
« Reply #5 on: January 22, 2018, 11:34:23 AM »
Hi,

I am currently placing data into the global array at runtime, within another FSM.

My last screenshot was copied from the state which has the array.contains Action when I had placed a breakpoint in it (also tried placing one in a following state and returned to see variable values, with the same result).

Another other ideas before I share? I would rather keep parts of the platform I'm creating private at the moment...

When is the best time for us to have a look together (where are you based)?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Array Contains strange behaviour
« Reply #6 on: January 23, 2018, 01:49:38 AM »
Hi,

 This is likely due to race condition, you are setting the value, but the action that check if it contains that value is executed before you set this value, however visually, when the debugger kicks in, internally the array was set and then it shows the value inside it, thus to clash.

 Can you confirm this value was set int he same frame as when you checked or was it set well before, at least several frames or seconds? else can you try? and implement for example a state with a next frame event before you check on that array for this value? this will allow the array to be set in memory and then used properly. The same happens sometimes with transform, if you move a transform and check for its position, it may not be up to date and you have to wait a full frame to get the right position. It's rare but the case happens.

 Bye,

 Jean


djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Array Contains strange behaviour
« Reply #7 on: January 23, 2018, 08:16:38 AM »
Hi.
With Collaborate only i (and you of course) can access/see the project.
At the moment i am on gmt+1 (western europe)
But i have very irregular working hours, so i can't really set a date/time.

Maybe you can join the playmaker channel on discord and see when i'm online :
https://hutonggames.com/playmakerforum/index.php?topic=9805.0


wmsgva

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Array Contains strange behaviour
« Reply #8 on: January 23, 2018, 10:11:48 AM »
@jeanfabre:

No, this is not a frame condition, as the global array is set within another FSM, after the player clicks a hotspot, several seconds before the other FSM loop runs and can't read the array's value.

You can see this in the screenshots below:

1) Within the FSM in which I'm trying to check array.contains, I added an array.get action to read the first value (index 0) of the global EventConditions array and to copy it into the global string variable Test.

2) First screenshot shows the global array contains 4 values

3) Second screenshot shows that Test is empty, despite going through the loop to read the array's first value.

This proves that something is wrong in trying to retrieve the value of this global array in this nested FSM.

@daydino:

I'm based in France, near Geneva, so we're in the same time zone. Don't you have any preferred work hours (can be outside of usual office hours)? It's difficult for me to have to monitor Discord to see if you're online.

wmsgva

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Array Contains strange behaviour
« Reply #9 on: January 23, 2018, 01:44:09 PM »
Furthermore, if I move the array.get action to the top FSM (recall I have an FSM, which calls one below with an array.for each, which then again calls another FSM using a second array.for each) and try to transfer the first entry of the global array into my test variable, this time it works.

Conclusion: There's an issue trying to read a global array when it's from within a prefab FSM (needs to be a prefab when called from array.for each). Could you try replicating this by building a "three-level" FSM?

Just one further consideration: I'm using Unity 5.6.3p2 (64-bit), not sure if that may have an incidence (not using the latest versions - need stability).

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Array Contains strange behaviour
« Reply #10 on: January 24, 2018, 02:39:50 AM »
Hi.
This should not be an issue.
Can you make a new scene and replicate what you are doing in the main scene.
and if you have the same issue, can you share that.
or duplicate your project and remove the parts you do not want to share.

I understand that you might be scared to share your project to us, but jean and me have 0 intentions to steal any ideas/projects from anyone.
The thing is that your case it is hard to pinpoint what is wrong.

wmsgva

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Array Contains strange behaviour
« Reply #11 on: January 24, 2018, 08:36:17 AM »
Finally solved!

The issue was related to the test being run multiple times with an array that had it's value set as a parameter in one of the superior loops.

I had been using array.clear instead of array.resize 0 to clear the loop above before using array.add to specify some conditions. The array was therefore looping too many times in the lower FSM in which I was using the array.contains.

My mistake, case can be closed. Sorry for the inconvenience.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Array Contains strange behaviour[SOLVED]
« Reply #12 on: January 25, 2018, 02:53:14 PM »
Hi.
No problem.
This kind of things happen.
And even with PM veterans and coders (even me) :)