Storing object with attributes in an array

Get help using Construct 2

Post » Wed Aug 03, 2016 11:03 am

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
B
45
S
15
G
73
Posts: 2,008
Reputation: 43,398

Post » Wed Aug 03, 2016 11:49 am

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...
When in doubt..or you are lost in a Zombie apocalypse or someone is giving you a hard time..Remember one thing..... Read the Construct 2 Manual..it will save your life.:)
B
35
S
13
G
8
Posts: 318
Reputation: 7,685

Post » Wed Aug 03, 2016 12:17 pm

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.
B
45
S
15
G
73
Posts: 2,008
Reputation: 43,398

Post » Wed Aug 03, 2016 12:29 pm

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.
B
45
S
15
G
73
Posts: 2,008
Reputation: 43,398

Post » Thu Aug 04, 2016 12:56 am

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
When in doubt..or you are lost in a Zombie apocalypse or someone is giving you a hard time..Remember one thing..... Read the Construct 2 Manual..it will save your life.:)
B
35
S
13
G
8
Posts: 318
Reputation: 7,685

Post » Thu Aug 04, 2016 4:05 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
81
S
34
G
40
Posts: 3,031
Reputation: 30,422

Post » Thu Aug 04, 2016 8:56 am

Thanks for that, I'll take a look at Dictionaries.
B
45
S
15
G
73
Posts: 2,008
Reputation: 43,398


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 13 guests