How do I find&replace a particular value in an array ?

Get help using Construct 2

Post » Mon Aug 11, 2014 4:23 pm

Hello,

I am a beginner with arrays, so I apologize if the answer of my question seems obvious. But I did not find the answer in the tutorials I have read.

I have a one-dimension array, it may look like this

0 : "boat"
1: 0
2: "tank"
3: "plane"
4: 0
5 : "infantry"

First, I want to check automatically if there is any 0 in my array (there is in my example, but it might not...).
Then, the FIRST time a 0 is found, I want to replace it by "submarine". But only for the first 0 found. The second 0 in this example would not be changed.

How would you do that ?
Thanks a lot :)
B
6
S
2
Posts: 32
Reputation: 575

Post » Mon Aug 11, 2014 4:50 pm

Make loop that repeats array width times and when loopindex = 0. Set value at loopindex to "Submarine" and stop loop.
B
34
S
9
G
3
Posts: 517
Reputation: 5,276

Post » Tue Aug 12, 2014 5:40 am

Pretty much what Katala said, as a function, so you don't have to repeat it :)

array_replace.png
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests