Is it possible to reference an object's instance variable directly by name?

1 favourites
  • 4 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • In a similar manner as we can use the create an object by name feature in Construct 3, I was wondering if it would be possible to set up the same mechanism for instance variables.

    I am working towards making my events more streamlined however I hit a wall when it comes to checking a function parameter and then ading to an instance variable based on that parameter.

    For example, say an object has woodCapacity and meatCapacity, upon adding an item (wood) to the object and then adding an item (meat) to the object I would like to be able to set the action as add self.( & "Capacity") however to my current knowledge this is not possible. I am sure there is some fundamental mechanism with coding that may make this difficult (or perhaps impossible?) to add but boy oh boy would it reduce the amount of code required.

    I believe there are other ways around this such as setting a value within a dictionary or array that you set the woodCapacity or meatCapacity to and then when this value is updated also update the capacity value, but would it be possible to make a direct reference to a variable so this wouldn't be necessary?

    Also, if there is another way to achieve the same result I am grateful to hear about.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is not possible. You can use a dictionary - add it to the same container with the sprite, so that each sprite instance will automatically have its own instance of the dictionary. And then use this dictionary to keep all values, instead of instance variables. When an item is picked up and you need to add 1 to this item count, you can do something like this:

    Pick an instance of Object
     ObjDictionary Add key ( & "Capacity") 
    	with value (Self.Get( & "Capacity")+1)

    You can also create a couple of helper functions to make adding/retrieving data from this dictionary easier.

  • You should use UID (unique ID) for that. Put all those pick-up-able objects in a family, like "Items", then when the player picks up wood or anything else, you pass the UID of the item to the function, then in the function you do "Items -> Pick by unique ID" to pick the correct item from the family

    Then if you want to check what item it picked, you add a sub event after picking the family object by UID and add a "compare two values" and compare "Wood.pickedCount > 0" or "Meat.pickedCount > 0"

    (I hope I understood your question correctly)

  • I figured as much, thanks guys.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)