Issue with adding to Array using For Each

Get help using Construct 2

Post » Sun Feb 21, 2016 1:00 am

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.

Code: Select all
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.

- Set Array_Looper to 0
- Set Player_Finder to 0

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)

- Add 1 to Array_Looper


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...
B
8
S
2
Posts: 59
Reputation: 686

Post » Sun Feb 21, 2016 7:29 am

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.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Sun Feb 21, 2016 7:56 am

mindfaQ wrote: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.
B
8
S
2
Posts: 59
Reputation: 686


Return to How do I....?

Who is online

Users browsing this forum: Xh3maa and 4 guests