Transferring array values to instance variables

Get help using Construct 2

Post » Thu Apr 26, 2012 4:15 am

I've tried searching for how to do this, but there wasn't very much I could find and even then the examples I found where hard to understand.

How do I use "tokenat" to get a value from an array, say the number 60 stored at 3, 1, 0?

A tutorial or a method of making the expression easier to understand would be very helpful for me.

EDIT: MY intention to take a value from an array (say again, 60 at 3, 1, 0) and store the found value in an instance variable.LaDestitute2012-05-01 04:01:59
B
20
S
6
G
4
Posts: 131
Reputation: 3,153

Post » Thu Apr 26, 2012 5:19 pm

you dont need tokenat for this. Look at the Array entry on the manual it has a way to get the value at a given position youd just use that to set the value of the instance variable.

tokenat is for strings that contain certain characters you want to use to split that string into multiple values. Like the token at 1 in the string "a,b,c" would be "b" if the string was split up at commas
B
13
S
4
G
2
Posts: 306
Reputation: 3,112

Post » Fri Apr 27, 2012 3:08 am

At(X, Y, Z)
Or
would it be Arrayname.At (X, Y, Z)?

Edit: Okay, I get it partially.
I have the value, now how do I transfer to the instance variable? Setting "Playerstats.At (2, 0, 0)" as the value of an instance variable doesn't appear to work.LaDestitute2012-04-27 05:44:05
B
20
S
6
G
4
Posts: 131
Reputation: 3,153

Post » Fri Apr 27, 2012 6:18 am

@LaDestitute, hope I'm not misunderstanding you here, but if you have a variable (ex: var1) of an object (ex: Sprite1), you can set the value of that variable to the array index as follows:

Sprite1.var1 = Playerstats.At(2,0,0)

Does that help?
B
124
S
6
G
4
Posts: 178
Reputation: 7,019

Post » Tue May 01, 2012 3:57 am

Yes, somewhat.

Now, I managed to get it to work almost but the value always reads "1".


Did I do this right?
B
20
S
6
G
4
Posts: 131
Reputation: 3,153

Post » Tue May 01, 2012 8:55 am

@ladestitute. If I understand you correctly, I think it should be:

Set Player.MaxHP to Playerstats.At(2,0,0)

This will set the Player.MaxHP from the array



B
24
S
4
G
5
Posts: 151
Reputation: 6,049

Post » Tue May 01, 2012 9:35 pm

[QUOTE=simwhi] @ladestitute. If I understand you correctly, I think it should be:

Set Player.MaxHP to Playerstats.At(2,0,0)

This will set the Player.MaxHP from the array



[/QUOTE]
I don't know what's causing it, but when I set "Playerstats.At(2,0,0)" directly as the value of Player.MaxHP, I get a zero.
B
20
S
6
G
4
Posts: 131
Reputation: 3,153

Post » Tue May 01, 2012 10:52 pm

Because if you are using only the X (width) size of an array to stock values, you don't need to indicate the Y and Z locations.
So
Set Player.maxHP to Playerstats.at(2) is enough

Getting a 0 out of an array cell often means the cell doesn't exist/is blank.

Also if Player.maxHP is a number instance variable you might need to cast the result returned from the array.
The values stored in arrays are string, you need to convert them when affecting them to numbers.

Set Player.maxHP to int(Playerstats.at(2))
(int() will return integer number, float() will return a float number)

In the how do I FAQ, section "Arrays" you have several topics on the subject that explains the very same thing.
You can, for example, find that commented capx.
Moderator
B
154
S
53
G
28
Posts: 5,415
Reputation: 41,358

Post » Wed May 02, 2012 12:43 am

I only had the y axis because I have multiple stats, so I wanted a row for each so I wouldn't have to reorder the array for new values later on.

Edit: Nevermind. I fixed my problem, it was a group I had the stat storage in. It was never getting calculated since it was at the bottom instead of being the first group.LaDestitute2012-05-02 00:46:59
B
20
S
6
G
4
Posts: 131
Reputation: 3,153

Post » Wed May 02, 2012 1:47 am

@LaDestitute. I'm glad that you have fixed the problem. You have to be careful how you order your groups. I had a similar issue recently!
B
24
S
4
G
5
Posts: 151
Reputation: 6,049

Next

Return to How do I....?

Who is online

Users browsing this forum: LI0NHART, tawny, Txori, Ubivis and 1 guest