playMaker

Author Topic: Think I found a bug in cached variables in public class SetFsmVariable : Fsm  (Read 2043 times)

Vena

  • Playmaker Newbie
  • *
  • Posts: 2
Hello,
I am posting this here and not the bug report because I am not sure if I'm not doing something wrong.

In short: Create Object 1, and use FSetFsmVariable. When creating a new Object 2 and using FSetFsmVariable for the same variable, PlayMaker uses the caches variable that is still pointing to Object 1.

My scenario:
GameObject A, with multiple FSMs.
FSM1 receives event and transits to a new state "Create Object".
In that state, it creates an object OBR using a prefab, stores the reference to it in a variable. Still in that state, it uses FSetFsmVariable to set var RACKID and sends an event to the created object to go to initialize state.
So far so good, FSM1 completes the state and returns to state READY, and created object PBR also finishes its initialized state and goes to READY state.

GameObject A, FSM1, receives the same event again, transits to new state, creates a new object again. The object has the same name, but the reference/variable to the object is correctly updated and calls FSetFsmVariable to set var RACKID, and here is where things get wrong.

Debugging public class SetFsmVariable: FsmStateAction, it checks for a cached object, it correctly detects the new object is not cached and gets the correct reference.
Then it checks for cached var RACKID, and it INCORRECTLY thinks it has the cached variable and uses the old reference! In other words, it updates (again) RACKID variable on the first created object.

By commenting out the below lines, it now correctly updates the variables on the second object.
//if (variableName.Value != cachedVariableName)
//{
targetVariable = targetFsm.FsmVariables.FindVariable(setValue.Type, variableName.Value);
cachedVariableName = variableName.Value;
//}

I did not dive into the code enough, but I would guess that if the cached object is changed, then checking for a cached variable should be skipped.



jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Hi,

 good catch!!

can you try to add "cachedVariableName = string.Empty; // invalidates variable cache" at the end of this section ( would line 81)

Code: [Select]
            if (go != cachedGameObject || fsmName.Value != cachedFsmName)
            {
                targetFsm = ActionHelpers.GetGameObjectFsm(go, fsmName.Value);
                if (targetFsm == null)
                {
                    return;
                }
                cachedGameObject = go;
                cachedFsmName = fsmName.Value;

                cachedVariableName = string.Empty; // invalidates variable cache
            }

let me know how it works for you.

Bye,

 Jean

Vena

  • Playmaker Newbie
  • *
  • Posts: 2
Just FYI, I changed the code to be like this:

(line starting at 71)
Code: [Select]
               
            if (go != cachedGameObject || fsmName.Value != cachedFsmName)
            {
                targetFsm = ActionHelpers.GetGameObjectFsm(go, fsmName.Value);
                if (targetFsm == null)
                {
                    return;
                }
                cachedGameObject = go;
                cachedFsmName = fsmName.Value;

                //VENA Object changed, don't even try to check cached var
                targetVariable = targetFsm.FsmVariables.FindVariable(setValue.Type, variableName.Value);
                cachedVariableName = variableName.Value;
            }
            else
            {//VENA object was in cache, lets check its var is also
                if (variableName.Value != cachedVariableName)
                {//VENA it was not in cache
                    targetVariable = targetFsm.FsmVariables.FindVariable(setValue.Type, variableName.Value);
                    cachedVariableName = variableName.Value;
                }
            }

That worked ok so far, but tbh I did not put it through more than simple tests.
But your version is way more elegant, I didn't like to have the same code in two places.
Code updated and all working fine, thanks.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Hi,

 ok, changes pushed, they will be featured in the next PlayMaker update

Bye,

 Jean