Hi GIL,

I need unique Ids to be generated at runtime, because I use object variables on combatants and they need to not be shared. So I need each combatant to have unique Ids. How do I generate a new id and assign it to a combantant when they're initialized? I think I can put a game event in the default init of each combatant, then call a function in the game event, but I just need some help with the script. I would do something like

combatants[1].Variables.ChangeString(xxx, "whatever")

But I don't know how to
1- change the ORK GUID
2 - Make the change by actors or by selected data, not by combatant index, so I can change the variables of the combatant that started the script.
  • edited April 9
    That's probably a job for the custom component save data feature.

    E.g. like this:

    using UnityEngine;
    using ORKFramework;
    using ORKFramework.Behaviours;

    public class SaveVariableObjectID : MonoBehaviour, IComponentSaveData
    {
    // save key used to store different components of this class
    public string saveKey = "variableObjectID";

    private ObjectVariablesComponent variables;

    private void Start()
    {
    this.variables = this.GetComponent<ObjectVariablesComponent>();
    if(this.variables != null)
    {
    this.variables.objectID = System.Guid.NewGuid().ToString();
    }
    }


    public string GetSaveKey()
    {
    return saveKey;
    }

    // called when the component data is saved/stored
    public DataObject SaveGame()
    {
    DataObject data = new DataObject();

    if(this.variables != null)
    {
    data.Set("objectID", this.variables.objectID);
    }

    return data;
    }

    // called when the component data is loaded
    public void LoadGame(DataObject data)
    {
    if(data != null &&
    this.variables != null)
    {
    data.Get("objectID", ref this.variables.objectID);
    }
    }
    }


    Add the component to your combatant's game objects - it'll create a random object ID when spawned and save/load the ID with the combatant's data.
    Post edited by gamingislove on
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Thanks, it works but there's a small problem. I attached the script to my prefabs, it generates a new GUID, but it generates one every time the combatant is spawned, so every time I change scene the combatants get new GUIDs, even if they had object variables with values in them, then all their object variables are wiped out. I need the GUID to be generated only once so the combatants keep their respective object vars through the entire game. How can i modifiy the code for that? Thanks
  • How are your combatants set up in the scene?
    Using a Combatant Spawner with Remember Combatants enabled should solve that issue, as it'd store the combatant's status (and with that the custom component save data).
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • For enemies: I use a combatant spawner. I just checked remember combatants, it doesn't work, pretty sure it's because the script generate a new GUID every time, so even if remember combatants does save correctly, the value of the GUID is changed. I understand the problem but I don't know how to make the script run only once per combatant.

    For the player group : I use standard ORK, it spawns a prefab combatant, with the script attached on the prefab. Same problem with the player: new guid every time I change scene.

  • edited April 12
    Hm, yeah ... I didn't consider that. The loaded key is overridden by the start function ...

    Try changing private void Start to private void Awake.
    Post edited by gamingislove on
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Thanks, but Awake doesn't solve the problem. I did a couple of tests, what seems to happen is not because of the start function( you put a !null condition, it should only run once?) I think it's the ork GUID from the editor. If I put the script on awake, the script creates a new GUID, but then the GUID from the ork editor overrides it. If I put the script on start, the script overrides the ORK GUID, but it does it every time, so theres a new GUID for every scene, I need a permanent GUID for the entire game per combatant.

    I tried putting a static bool to only run the script once... but then my the new guid gets overwritten by ORK GUID then. Tried putting no GUID in ork editor, but then it generates one. Tried putting an obj var component instead, same problem.

    Any ideas?
  • The Start and Awake functions of Unity components only run once per scene load or instantiation of a game object. The null check is just there to not get an error when there was no object variables component.

    I'll do some tests to get this working.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Yep, my bad - the variables aren't yet found when the data is loaded, use this code instead:


    using UnityEngine;
    using ORKFramework;
    using ORKFramework.Behaviours;

    public class SaveVariableObjectID : MonoBehaviour, IComponentSaveData
    {
    // save key used to store different components of this class
    public string saveKey = "variableObjectID";

    private ObjectVariablesComponent variables;

    private void Start()
    {
    this.InitVariables();
    }

    public void InitVariables()
    {
    if(this.variables == null)
    {
    this.variables = this.GetComponent<ObjectVariablesComponent>();
    if(this.variables != null)
    {
    this.variables.objectID = System.Guid.NewGuid().ToString();
    }
    }
    }

    public string GetSaveKey()
    {
    return saveKey;
    }

    // called when the component data is saved/stored
    public DataObject SaveGame()
    {
    DataObject data = new DataObject();

    if(this.variables != null)
    {
    data.Set("objectID", this.variables.objectID);
    }

    return data;
    }

    // called when the component data is loaded
    public void LoadGame(DataObject data)
    {
    if(data != null)
    {
    this.InitVariables();
    if(this.variables != null)
    {
    data.Get("objectID", ref this.variables.objectID);
    }
    }
    }
    }
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Thanks a lot , that did work!

    I have one last simple question regarding this. I will be using this unique GUID in the ork event system, so I need to store the GUID into a game variable on the combatant object. So I thought I'd just add a line under there:

    this.variables.objectID = System.Guid.NewGuid().ToString();
    // to store the GUID into a game var
    this.variables.initialVariables.gameVariable.SetValue(whateverGameVariable, this.variables.objectID)


    I looked around and tried to understand, but I have no idea of the syntax, or if I'm even using the right command. Can you point me to the right direction?
  • You can set a (string) variable in the object variables like this:

    this.variables.GetHandler().Set("key", this.variables.objectID);
    Instead of "key", use the variable key you want to store the GUID as.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Thanks GIL, that worked, you rock!
Sign In or Register to comment.