Instance Variables vs Arrays

Discussion and feedback on Construct 2

Post » Sun Nov 03, 2013 3:17 am

In an rpg, where the character has ststa, hp, mana, str, int, agi, stam, gold, etc..
Is there any reason to use one over the other? I can easily give my player all the appropriate instance variables, but I could just as easily do this with an array. Is there any reason to use one over the other?
B
9
Posts: 12
Reputation: 351

Post » Sun Nov 03, 2013 3:28 am

Neither. I'd use dictionaries in containers with the characters... Each stat can be a key and you can save them all in a single file/webstorage key. Way better than instance variables.
Image
B
243
S
30
G
13
Posts: 1,787
Reputation: 18,770

Post » Sun Nov 03, 2013 2:47 pm

@MadFactory

can you explain a with a little more detail how you use the dictionaries?
i have the same concern about variables x arrays
B
48
S
5
G
6
Posts: 210
Reputation: 6,297

Post » Sun Nov 03, 2013 4:22 pm

I haven't actually tried it but...You make a container with your player and a dictionary. When the dictionary is created, add 1 key per stat and a default value like

-Add key "Health" , 100
-Add key "Attack" , 5
-Add key "Defense" , 2

and if your player is chosen in the events it'll use the stats in the corresponding dictionary.

Then when you go to save the game you can download all the dictionaries as JSON data into a file or webstorage key and load them later on.

Not sure what the best way to tell the difference between the dictionaries would be though. It might be better to NOT use containers and instead give the player and dictionary an identical variable to compare.

Image
B
243
S
30
G
13
Posts: 1,787
Reputation: 18,770

Post » Sun Nov 03, 2013 5:27 pm

Either a dictionary or array (I'm more comfortable using arrays, but they both work similarly in this case). The biggest reason is for saving. I can save everything in my game as a single JSON file and reload just as easily.
B
103
S
38
G
19
Posts: 962
Reputation: 17,996

Post » Sun Nov 03, 2013 5:30 pm

I prefer using arrays as well. But before I actually input all the character stats I use a spreadsheet file to organize things, then just reference it to my arrays in game.
B
18
S
4
G
3
Posts: 436
Reputation: 4,801

Post » Sun Nov 03, 2013 6:50 pm

[QUOTE=MadFactory]
Then when you go to save the game you can download all the dictionaries as JSON data into a file or webstorage key and load them later on.
[/QUOTE]

But is that necessary since i can just save the game with System -> save game? And load the game with System load game? What do I get out of being able to download them as json data?
B
9
Posts: 12
Reputation: 351

Post » Sun Nov 03, 2013 7:57 pm

Usable save data. I personally avoid the persist behavior, built-in save functions, etc. because you don't really have any control over it.
Image
B
243
S
30
G
13
Posts: 1,787
Reputation: 18,770

Post » Sun Nov 03, 2013 9:18 pm

You have control over it by using the no-save behavior to make that object not get saved. You can add this to any object to get complete control. Persist only allows an instance to have it's values persist on the layout even if you change layout and come back (a little like making it global instead of local)

Personally I would use global dictionaries rather than container dictionaries to make it easier on myself, and no need to keep track of multiple instances of dictionaries.Thndr2013-11-03 21:21:30
B
21
S
8
G
6
Posts: 346
Reputation: 4,891

Post » Sun Nov 03, 2013 10:21 pm

hmm dictionaries, well

thanks o/
B
48
S
5
G
6
Posts: 210
Reputation: 6,297

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 14 guests