Page 1 of 1

Arrays and Index

PostPosted: Thu Jan 13, 2011 8:45 pm
by peedalein
Hello everybody!

For a part of my game I need to calculate a sort of interest for the player's borrowings.
Each time the player borrows money interest is calculated. For each frame he pays a little interest. As he keeps on borrowing, I need to calculate
the interest for his aggregated borrowing. The player always borrows the same amount and never pays it back (except by refinancing). Actually, this
is not too important.

I created an array that stores the information on which frame he borrowed. Each time the player collides with the money the frame is stored.

Code: Select all
InterestFrames[a] = fcount;
a = a + 1;


//fcount counts the frames, a is an index

Another text actor is supposed to display the latest frame (for testing)

Code: Select all
textNumber = InterestFrames[a];


The text actor does not display the value of the InterestFrames array.

If I put

textNumber = InterestFrames[1];

the text actor displays the value (true for any number of the array)

=> Why does "InterestFrames[1]" work while "InterestFrames[1]" does not?

Thanks for your help.

I attached a file.

Re: Arrays and Index

PostPosted: Thu Jan 13, 2011 10:15 pm
by Game A Gogo
is your variable a created inside a script in the money actor, an actor variable or a global variable? if you want to access it from another actor, you'll have to make it global or using Actorname.a if you want to keep it an actor variable. But you can't have it created inside a script.

Re: Arrays and Index

PostPosted: Fri Jan 14, 2011 9:06 pm
by peedalein
Thanks for the answer!

The index [a] is defined as a global variable, therefore I do not think that this is the problem.

Re: Arrays and Index

PostPosted: Fri Jan 14, 2011 10:28 pm
by skydereign
The problem is that this is your collision event.
Borrow -> Collide with Money -> Script Editor
Code: Select all
InterestFrames[a]=fcount;
a=a+1;


You set the interest for the current a, and then display the next a, which should be zero. If you put the a=a+1 before setting the InterestFrames, then it will change and display the same a. Just a tip, you should always try to keep only one script per event. You had two script editor actions tied to a destroy actor event. Instead of doing that, you can put the DestroyActor at the end of the other Destroy Actor script.

Re: Arrays and Index

PostPosted: Mon Jan 17, 2011 8:06 pm
by peedalein
Thanks, I changed my code to
Code: Select all
a=a+1;
InterestFrames[a]=fcount;


Now it works fine. Thanks for the help.