Can local scripts change global variables?

Started by Outcast, Thu 08/12/2016 19:59:20

Previous topic - Next topic

Outcast

I have a Global variable to keep track of money(int Money). But I'm having problems when my character changes rooms.

Let's say my character starts off with $1000 in the global variable, and then find $1000 more. I used the repeatedly_execute function to handle calculations but when I change rooms the money is back to $1000.

The values always revert back to the initial value of the global variable when the room changes despite any adding or subtracting that DID work in the first room.

My local scripts can read a global variable and use it, but how do I change the global variable so that it stays changed globally from various rooms

Thanks, any help appreciated.

Slasher

#1
If you have a label that shows how much money you have then you should add the label in rep exe always in global:


Code: ags

LMoney.Text=String.Format("%d",Money);


This string will update/reflect the total of Money in the label in any room.




Snarky

Quote from: Outcast on Thu 08/12/2016 19:59:20
The values always revert back to the initial value of the global variable when the room changes despite any adding or subtracting that DID work in the first room.

My local scripts can read a global variable and use it, but how do I change the global variable so that it stays changed globally from various rooms

Is this a Global Variable declared through the special Global Variables pane, or a global variable in the global script or some other script file?

Any changes you make to a global variable should persist. However, there are a couple of ways to do something wrong so it won't work: the most likely is that you're declaring the variable in a header instead of declaring it in a script file and then using export/import to make it available to other scripts.

Outcast

First of all, thanks to you both for the input. It turns out that I reassigned the variable value twice and messed up my own program. (wrong)
I do have another question if you'll bare with me a bit.

I'm using the rooms ability to store 5 backgrounds for animation, but I need a way to lock onto specific backgrounds without animating them. I'm
using the SetBackgoundFrame() function to do this, and it does work but here's the situation:

Each room uses 4 backgrounds and I use a good ole Global variable to keep track of which background the character sees.
So it's like this.

In every room the script assigns a value to global variable FRAME from a calculation.
When the character leaves one room and enter the next there is a SetBackgroundFrame(FRAME) function to show the correct
background.

When the character arrives background no#0 always shows first even with the SetBackgroundFrame(FRAME), then switches to the right frame, and stays there.
I put the SetBackgroundFrame command in a execute_repeatedly function.

Without your help this game will die before it is born. :cry:






Matti

Put SetBackgroundFrame in the before_fadein function, then it should work. Putting it in the rep_exec without any condition is not a good idea anyway, because then it'll get called 40 times a second (or whatever framerate you use).

Snarky

You should put entirely new questions in a new thread, but in this particular case the answer is simple: Instead of calling SetBackgroundFrame() in repeatedly_execute(), do it in the function for the "player enters room (before fade-in)" event.

(Edit: sniped!)

Vincent

Well I don't know if this somehow may find useful, but while you put SetBackgroundFrame you could check back the specific background by GetBackgroundFrame.

Code: ags

if (GetBackgroundFrame() == 4)
// SetBackgroundFrame(0);
// stop animating

Outcast

The before fade-in event function did the trick! The GetBackground function will probably come in handy as the game develops.

Once again many thanks. I was bout to give up.

SMF spam blocked by CleanTalk