Arrays and Index

Game Editor comments and discussion.

Arrays and Index

Postby peedalein » Thu Jan 13, 2011 8:45 pm

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.
Attachments
InterestRates.zip
(196.54 KiB) Downloaded 75 times
peedalein
 
Posts: 54
Joined: Mon May 17, 2010 2:26 pm
Score: 1 Give a positive score

Re: Arrays and Index

Postby Game A Gogo » Thu Jan 13, 2011 10:15 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Arrays and Index

Postby peedalein » Fri Jan 14, 2011 9:06 pm

Thanks for the answer!

The index [a] is defined as a global variable, therefore I do not think that this is the problem.
peedalein
 
Posts: 54
Joined: Mon May 17, 2010 2:26 pm
Score: 1 Give a positive score

Re: Arrays and Index

Postby skydereign » Fri Jan 14, 2011 10:28 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Arrays and Index

Postby peedalein » Mon Jan 17, 2011 8:06 pm

Thanks, I changed my code to
Code: Select all
a=a+1;
InterestFrames[a]=fcount;


Now it works fine. Thanks for the help.
peedalein
 
Posts: 54
Joined: Mon May 17, 2010 2:26 pm
Score: 1 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron