playMaker

Author Topic: Adding Playmaker's variables in editmode from code.  (Read 3162 times)

DiaG

  • Playmaker Newbie
  • *
  • Posts: 5
Adding Playmaker's variables in editmode from code.
« on: August 15, 2013, 06:54:10 AM »
Hello. I'm try to add PM variable from code in CustomActionEditor.
I did not find the interface to add or remove some variables, only the get/set methods in Fsm.Variables.(FsmType). However, using set-method remove all assignments of variables. I use it to load the variable names and their values ​​from the xml to display dialogs.
While I treat the following ways: one button compares the existing variables, and tells what variables are present in the xml, but not in the variables; the other button fills exist variables from the file; the third (with a warning that all assignments of type String will be lost) creates an array and sets Fsm.Variables.StringVariables.
Is it possible to add / remove variables from the code without losing assignments?
« Last Edit: August 15, 2013, 07:04:40 AM by DiaG »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Adding Playmaker's variables in editmode from code.
« Reply #1 on: August 16, 2013, 05:01:06 AM »
Hi,

 I am not sure what you mean by "assignment". Do you have a concret example or a screencast? then I'll be able to look at this properly.

bye,

Jean

DiaG

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Adding Playmaker's variables in editmode from code.
« Reply #2 on: August 16, 2013, 07:55:41 AM »
Sorry for bad english. I do not know how to accurately translate. I have code:
Code: [Select]
[CustomActionEditor(typeof(LoadStringsFromXML))]
public class MyCustomEditor : CustomActionEditor
{
    ...
    public override bool OnGUI()
    {
        var action = target as LoadStringsFromXML;
        ...
        List<FsmString> newList = new List<FsmString>();
...
        action.Fsm.Variables.StringVariables = newList.ToArray();
        ...
    }
}
Thereafter all field (type of FsmString) in all actions are set to None (for current FSM). 
The screenshot shows state before and after this action.
« Last Edit: August 16, 2013, 07:58:48 AM by DiaG »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4000
  • Official Playmaker Support
    • LinkedIn
Re: Adding Playmaker's variables in editmode from code.
« Reply #3 on: August 16, 2013, 05:49:19 PM »
Yeah, that code would replace all the string variables used by the FSM.

You could add the new variables to the array using ArrayUtility.AddRange():
http://docs.unity3d.com/Documentation/ScriptReference/ArrayUtility.AddRange.html

To be honest, this is not a use case that's been tested with custom editors, so let me know how this goes and we can work through any issues...



DiaG

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Adding Playmaker's variables in editmode from code.
« Reply #4 on: August 19, 2013, 03:16:22 AM »
You could add the new variables to the array using ArrayUtility.AddRange():
Thank you. I have tried to do so, but AddRange<T>(ref T[] array, T[] items) have "ref" modifier, and StringVariables is a property, not field. As result I get: "A property or indexer `HutongGames.PlayMaker.FsmVariables.StringVariables' may not be passed as `ref' or `out' parameter"

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4000
  • Official Playmaker Support
    • LinkedIn
Re: Adding Playmaker's variables in editmode from code.
« Reply #5 on: August 19, 2013, 03:58:35 AM »
Hmmm, you could write your own ArrayUtility:

Code: [Select]

public static T[] Add<T>(T[] array, T item)
{
var list = new List<T>(array) {item};
return list.ToArray();
}

public static T[] AddRange<T>(T[] array, T[] items)
{
var list = new List<T>(array);
        list.AddRange(items);
return list.ToArray();
}