Transferring array values to instance variables Forum Home > Construct 2 General > How do I....? |
Post Reply
|
| Author | |
Post Options
Quote Reply
Topic: Transferring array values to instance variablesPosted: 26 Apr 2012 at 4:15am |
|
|
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. Edited by LaDestitute - 01 May 2012 at 4:01am |
|
![]() |
|
Post Options
Quote Reply
Posted: 26 Apr 2012 at 5:19pm |
|
|
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 |
|
![]() |
|
Post Options
Quote Reply
Posted: 27 Apr 2012 at 3:08am |
|
|
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. Edited by LaDestitute - 27 Apr 2012 at 5:44am |
|
![]() |
|
Post Options
Quote Reply
Posted: 27 Apr 2012 at 6:18am |
|
|
@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? |
|
|
Check out Nibble in the HBGames Build and Win Construct 2 Contest!
|
|
![]() |
|
Post Options
Quote Reply
Posted: 01 May 2012 at 3:57am |
|
|
Yes, somewhat.
Now, I managed to get it to work almost but the value always reads "1". ![]() Did I do this right? |
|
![]() |
|
Post Options
Quote Reply
Posted: 01 May 2012 at 8:55am |
|
|
@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 |
|
![]() |
|
Post Options
Quote Reply
Posted: 01 May 2012 at 9:35pm |
|
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. |
|
![]() |
|
Post Options
Quote Reply
Posted: 01 May 2012 at 10:52pm |
|
|
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. |
|
![]() |
|
Post Options
Quote Reply
Posted: 02 May 2012 at 12:43am |
|
|
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. Edited by LaDestitute - 02 May 2012 at 12:46am |
|
![]() |
|
Post Options
Quote Reply
Posted: 02 May 2012 at 1:47am |
|
|
@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!
|
|
![]() |
|
Post Reply
|
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |