edited May 2014 in ORK Support
Is there a way to access Object Variables through code? I can change Game Variables, but there doesn't seem to be any way to change Object Variables.
  • Yes, there are two ways to access them.

    If you have the game object they're attached to, you can access them via the ObjectVariablesComponent on them. The function GetHandler() returns a VariableHandler containing the object's variables (it's the same class used for the global variables in ORK.Game.Variables).

    If you don't have the object, you can also get the VariableHandler of the object by calling ORK.Game.Scene.GetObjectVariables(string objectID).
    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 May 2014
    This is a little embarrassing, but I don't understand how to access the ORKFramework components via code. This isn't just for Object Variable Components, this also extends to components like Event Interaction.

    For example, I place a custom script on NPC_green. It only has one line of code:

    using UnityEngine;
    using ORKFramework;
    using System.Collections;

    public class TestObjectVar : MonoBehaviour {


    // Use this for initialization
    void Start () {
    Debug.Log(this.GetComponent<"CharacterController">().slopeLimit);
    }

    // Update is called once per frame
    void Update () {

    }
    }
    This works fine. As soon as I type GetComponent, the CharacterController is a valid selection. However, I also have an Event Interaction (Script) and Object Variable Components (Script) on NPC_green. But neither of these Components are available. If I type them I get:
    The type or namespace name `EventInteraction' could not be found. Are you missing a using directive or an assembly reference?
    Sorry, I know the solution to this must be basic, but I can't seem to figure this out.


    I was able to get the variables using the second method, but I don't see any way to change it like a Game Variable. How would I Set the variable?
    Post edited by Nixter on
  • You need to call GetComponent without the quotes:

    this.GetComponent<EventInteraction>();

    Getting it by name doesn't work, because it's in a namespace.

    Both functions return a VariableHandler class, and that's the same class that ORK.Game.Variables returns - so the functions are the same as for normal game variables.
    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!
  • Still having trouble. I sent you a message with a link to an example project.
  • Found the issue: don't access the object variables in your start function, because it'll most likely not be initialized with your values at this point.
    I'll do some changes to allow that in the next update :)
    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!
  • Ahhhh. I just loaded up my example and switched the code from Start() to Update() and now it works fine.

    For others who stumble onto this thread, here is the code for the two methods of getting Object Variables:


    this.GetComponent<ObjectVariablesComponent>().GetHandler().GetString("Test01");


    or


    ORK.Game.Scene.GetObjectVariables("a6a7e396-a936-46e7-aa75-39357e30e33f").GetString("Test01");


    "a6a7e396-a936-46e7-aa75-39357e30e33f" is an example Object ID. You will find each object's unique Object ID in the ObjectVariablesComponent:VariableSettings.

    Setting an Object Variable is fairly easy too. Instead of GetString("Test01"), use Set("Test01","MyTest"). This change seems persistent, since I was able to set the variable, save the game, remove the set code then reload the saved game and the variable remained changed. However the change will not be visible in the ObjectVariablesComponent, which continues to show the old variable value.

    Thanks for the help, gamingislove! Your support is legendary!
  • You'll be able to do this in the Start function in the next update - currently it's also no problem, but the auto initialization variables are currently only initialized in the object variable's start function, which can be after you're accessing 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!
Sign In or Register to comment.