Storing object with attributes in an array

0 favourites
  • 7 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I want to store an object in an array.

    Previously I was storing objects as text strings on Y=0 with the amount of the object on Y=1, so a Potion might appear as a text string on (0,0) of the array and the amount of potions would appear on (0,1) of the array.

    I'm wondering if there is a better way than this of storing an object with parameters because in the future I may add more, i.e. can I store an entire object 'Potion' in (0,0) of the array and access its parameters such as amount, type easily? If this is possible then what is the object type I am looking for? I noticed you cannot push an object to an array so there might be some other type I can use.

    Thanks

  • Yes you are right..you cant push an object..but you can push any of its attributes or Variables...

    I am pretty sure you can adjust the dimensions of am Array at anytime...Using the ' INSERT' action allows you to increase the size of an array at any dimension.

    Similarly you can "Delete" Values from the Array to decrease the size of a dimension

    Not sure if there is a better way than the way you mentioned...its fairly subjective...because the use of an array is dependent on the functions required by the data you wish to store...

    Personally I store object names on the X axis but its only a personal Preference...its not mandatory

    I am assuming you are only using a 2 dimensional array...

  • Thanks for the info. My current method could be the most efficient I'm just looking for a shortcut of doing the following.

    X=0, y=0 : item added. Now I need to add all of the associated attributes for that item to 0,1 0,2 0,3 pushing all of them separately for that stored item. This is because I cannot push an object to the array (which could have defined instance variables).

    When I add a new item it will be pushed to x=1, y=0. Then all associated attributes for that item such as the amount of them in the inventory, the type etc will be stored under 1,1 1,2 1,3 etc.

    Yes this works kinda like a table storage for all of the items but just wondering if there was a way to simply add items on x=0 only and have access to its attributes elsewhere or stored in a better way.

  • I think I may have gone a bit overboard here I'm just going to add each item collected to the x axis and then run a for each check to see how many of each specific item exist in the array. Multiple axes even 2-dimensional is confusing.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • There might be a way to do that..but I'm not sure...

    I guess you could make an event that determines how many variables each object has and then pushes the required number into the array as well as the data itself

    but it could be a complex Event to Program and maybe not worth the time to setup

    I'll look into it

  • I would recommend using a dictionary object instead of an array personally.

    You can add a dictionary object into a container with any other object - and then you automatically have a constant set of "plain language" accessible information sets.

    The main benefit of this method is that you can easily track instances of each object (since it's in a container with the dictionary) and you can still save the dictionary data as JSON string for webstorage or NWjs writing files to hard disk.

    I find array to be more useful when handling un-named data (like tile positions or something) but this is personal preference.

    ~Sol

  • Thanks for that, I'll take a look at Dictionaries.

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