Issue with adding to Array using For Each

0 favourites
  • 3 posts
From the Asset Store
Kids Game
$49 USD
New Sounds Added Update: 115 new sound effects added for no additional cost!
  • Hi all,

    I store all my "player data" in an array called Player.Array. When a value changes I want to be able to write data back into my Array, editing fields such as "Strength" or "Agility" immediately when a player levels up, for example. The benefit of this is that when the instance is destroyed he won't lose his stats.

    Player.Array:

    ID,NAME,CLASS,STR,AGI,INT

    0,Dingle,Mage,3,5,9

    1,Muscle,Warrior,9,6,1

    2,Flight,Druid,6,6,3

    3,Joseph,Bard,5,5,7

    4,Stars,Druid,5,4,6

    Here is my current method (not working) for editing data.

    Scenario: "Flight" levels up and chooses a point in Intelligence. I need to search the Array for his ID number (2) and then Add 1 to his Intelligence column.

    On Function "LevelUp" (param(0)) ## Function is called with the UID of the character who levelled up.
    Local number Array_Looper = 0 ## To loop through the Array incrementally.
    Local number Player_Finder = 0 ## To grab the UID from the instance to look up in the Array.
    
    [ul]
    	[li]Set Array_Looper to 0[/li]
    	[li]Set Player_Finder to 0[/li]
    [/ul]
    Pick instance of Player with UID Function.Param(0) - Set Player_Finder to Player.Player_ID ## An Instance variable that stores ID from Array on the instance when it is spawned into the game.
    
    For each XY element in Player.Array:
    If Player.Array Value at (0,Array_Looper) = Player_Finder - Set value at (5,Array_Looper) to Player.Array.At(5,Array_Looper)+1)
    
    [ul]
    	[li]Add 1 to Array_Looper[/code:22ce4qg8][/li]
    [/ul]
    So I have done a few tests and not come up with much. The annoying thing is that this code works elsewhere in the same project. I more or less mirrored it and changed the details, but I guess that's where inconsistency sneaks in.
    
    Any help would be much appreciated. I'll try any suggestions in a new project tomorrow just to get away from a very busy load of code and try some ideas out on a blank canvas...
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hmm, normally I would expect the row to come first, then the column. This would also enable you to work with "For each X" instead of "For each XY".

    A faster solution without a loop would be:

    Player carries an instance variable of where is saved in the array. (if elements will be removed from the array at some point, you need to update the positions in those cases, though)

    then you could: "Levelup" (Parameters: player.arrayposition, parameter_num, value_to_add)

    on "LevelUp":

    set array at (player.arrayposition, parameter_num) to array.at(player.arrayposition, parameter_num) + value_to_add

    Obviously you need to switch x and y, if you want to use x for columns and y for rows.

  • Hmm, normally I would expect the row to come first, then the column. This would also enable you to work with "For each X" instead of "For each XY".

    A faster solution without a loop would be:

    Player carries an instance variable of where is saved in the array. (if elements will be removed from the array at some point, you need to update the positions in those cases, though)

    then you could: "Levelup" (Parameters: player.arrayposition, parameter_num, value_to_add)

    on "LevelUp":

    set array at (player.arrayposition, parameter_num) to array.at(player.arrayposition, parameter_num) + value_to_add

    Obviously you need to switch x and y, if you want to use x for columns and y for rows.

    Thanks for your help. I will take note of this and try to adjust my code.

    I found out what the issue was with my particular example - I was using a plugin "CSVtoArray" to import my data and it converts every single entry in the Array to "Text" so even numbers are not classed as "Number". I re-worked my code above so that my local variable was a text object (for searching the array) and now it works.

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