Instance Variables vs Arrays

  • 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?

  • 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.

  • can you explain a with a little more detail how you use the dictionaries?

    i have the same concern about variables x arrays

  • 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.

  • 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.

  • 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.

  • 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.

    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?

  • Usable save data. I personally avoid the persist behavior, built-in save functions, etc. because you don't really have any control over it.

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • hmm dictionaries, well

    thanks o/

  • So, it seems like the consensus here is use dictionaries, of some sort.

  • I would prefer the dictionnary on an array , as a dictionnary has no fixed size , so you can add a key without affecting your code much , but otherwise , I don't see any other difference between the two , Imagine the array as a table of a fixed size , and the dictionnary as an infinite list

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