Is there a way to add Object Variables to an Object with the Object Variable Component in a running game?

For example I have a Cube Object that starts with no Object Variable Component on it, but does have a TIck Machine on Update.
This Tick Machine checks if the an Object Variable Component Exists.
If True wait 1 second and remove this Tick Machine.
If False Add Component, Object Variable Component.

Now that the Object Variable Component is attached to the Cube, it has no Initial Object Variables.

Is there a way to add/set/remove variables to appear in the Object Variable Component list?

The reason I'm doing this is I found that when the Object Variable Component is attached during gameplay it generates a new Object ID and that is ultimately what I want. But I can't figure out how to add in the Variables to the Object Variable Component during gameplay.

Unless there is a easy way to generate a New Object ID on the Object Variable Component during gameplay via Schematic Nodes that I'm not seeing?

Any help is greatly appreciated!
Thanks,
-Nate
Portfolio - My Website
Follow my project on Reddit - My Game DevLog
relentingVids - My YouTube Channel
Half Super Shop - My Shop
  • Use an Add Component node to add an ObjectVariablesComponent component to the game object. You can set the objectID field (string) using a Change Fields node, generating a random object ID (like in the editor) isn't really possible.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Coming back to this, how do I access the objectID by script and add new variables to the object variables list by script?

    Any help is greatly appreciated!
    Thanks,
    -Nate
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
  • It's the objectID string field on the component, also, the localVariables bool field handles if it's using local variables or the ID.

    However, the handler is already cached the first time the variables are accessed, so if you change the object ID at a later time it might not have any impact.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • edited September 2021
    The main thing I want to do in the script is add the Object Variable Component, set the Object ID, and add the new variables I want to it all in one go. Is that possible? If so how do I go about doing that in code?

    Edit* I figured out how to place the ObjectVariableComponent and set the objectID. Now I'm just trying to figure out how to add the variables to it by code.


    Thanks,
    -Nate
    Post edited by relenting_1 on
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
  • Generally pretty easy:

    ObjectVariablesComponent component = gameObject.AddComponent<ObjectVariablesComponent>();
    component.objectID = "id";
    VariableHandler handler = component.GetHandler();
    handler.Set("key", value);


    Add the component, set the ID, get the handler and change variables on the handler with key/value as you want :)
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • edited September 2021
    I was so close, I had something similar but it wasn’t working.
    I was doing just

    component.handler.Set(key, value);

    Without getting the handler first.
    But that wasn’t doing anything from what I could tell.

    Appreciate all the help!

    Thanks,
    -Nate

    Post edited by relenting_1 on
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
  • Ok this works if the game is running.
    However the way I’m trying to update the object variables is all at once by a menu item, in an editor script when the game isn’t running. Is this a possibility?
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
  • For the editor?
    In that case you'd have to set up the whole initial variable setup by code, which is very complicated and can lead to lots of errors when not done correctly.

    Isn't it easier just setting up the component once, copy it and paste it on other game objects?
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • I can copy it over to other game objects. But then I’d have to fill in each Object Variable Component with different values. That’s ok if it’s just a few to change but I got a bunch.

    My script currently is reading a CSV file and then splits and parses the data for each line. It creates a ScriptableObject with that lines split parsed data and an empty gameObject with a name value from the data. It then creates a Prefab out of that named empty GameObject and destroys that GameObject Immediately afterwards so it’s not left in the scene.

    I came up with a work around for now since I can’t figure out how to directly tie the data to the Object Variable Component. I made another script with just public variables and have that added to to Prefab when it’s created and then inserts the CSV data into those public fields. I then created a schematic that will pull the data from that script and add it to the Object Variable Component automatically at runtime with it placed in an Auto Machine Start. I know it’s a lot of duplicated variables but ultimately having the data in the Object Variable Component makes it easier to work with Makinom. I have a video that runs through the scripts I made here if interested.

    If I could get the data to go directly on the Object Variable Component that would save me the trouble from doing the workaround.
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
  • To get it into the component in the editor, you have to create the initial variables out of it (initialVariables field).
    It's complex but doable - depending on which type of variable it is (bool, float, string, etc.) you'll have to initialize different things.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Right now most of my variables are int and string but I’m sure I’ll eventually have bool, float, and Vector3.

    How does the initialVariables field work? Is it similar to the VariableHandler?
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
  • No, that's the setting you can set up in your components - i.e. basically an array of variable change settings.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • So when defining the variables I want to set up I need to specify the initialVariables array index and then what variable goes there? I’m a bit lost but I’ll keep looking at the source code to understand what does what.
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
  • You need to create the whole array in the size you need and set up the individual elements of it.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Could you show a short example for how this is done. I can’t seem to wrap my head around the set up.
    Portfolio - My Website
    Follow my project on Reddit - My Game DevLog
    relentingVids - My YouTube Channel
    Half Super Shop - My Shop
Sign In or Register to comment.