Page 1 of 1

Accessing global variables in global script

PostPosted: Fri Oct 01, 2010 2:50 pm
by peedalein
Hi!

I would like to store certain parameters in a single place so that it is easier for me to access/modify them. How can I do that?

What I have done so far:

Actor controls


Actor1

Key Down (right) > Script Editor

x += 10;

Key Down (left) > Script Editor

x -= 10;

Instead of "10" I would like to use a variable called "Speed". I declared a global variable "Speed".
In the Global Script Editor I put "Speed = 10;" and saved it as "VariableStor", but I do not know how to make GE access the Global Script ("x -= Speed" does not work)

As this did not work I created an actor called "Variables" with the condition Create Actor > Script Editor and put "Speed = 10;".
I am sure that there is a more elegant solution

Thanks in advance!

Re: Accessing global variables in global script

PostPosted: Fri Oct 01, 2010 6:09 pm
by savvy
on the player, create actor -> scrpt ed -> speed=10;
then key left -> scrpt ed -> x-=speed;
then key right -> scrpt ed -> x+=speed;

happy? this is simple var stuff :)

Re: Accessing global variables in global script

PostPosted: Fri Oct 01, 2010 10:46 pm
by DST
Don't set the condition in global script.

Use the variables tab to create the variable speed; if you want it to be set on game startup, use

view>createactor>

and set the variable to an amount in there.



Ge doesn't like certain things done in global script, don't ask me why.

Re: Accessing global variables in global script

PostPosted: Sat Oct 02, 2010 11:35 am
by Bee-Ant
Peedalein, did you type this in the Global code:
Code: Select all
int Speed=10;

or
Code: Select all
#define Speed 10

Both method should work...

Re: Accessing global variables in global script

PostPosted: Mon Oct 04, 2010 10:59 am
by peedalein
Hello!

I did not think about the view actor being a storage for my variables. Thanks for pointing out.

Code: Select all
int Speed=10;


This did not work for me. The problem is that I already had the variable declared. Therefore I just had to define the value as in

Code: Select all
#define Speed 10



Thanks to all for helping a newbie :)

Re: Accessing global variables in global script

PostPosted: Mon Oct 04, 2010 6:00 pm
by lcl
peedalein wrote:Hello!

Code: Select all
int Speed=10;


This did not work for me. The problem is that I already had the variable declared.


Because the variable is already defined, you can use:
Code: Select all
Speed = 10;

:D

Re: Accessing global variables in global script

PostPosted: Tue Oct 05, 2010 12:58 am
by Bee-Ant
lcl wrote:Because the variable is already defined, you can use:
Code: Select all
Speed = 10;

:D

peedalein wrote:I declared a global variable "Speed".
In the Global Script Editor I put "Speed = 10;"

He has already did... :wink:

Re: Accessing global variables in global script

PostPosted: Fri Nov 19, 2010 8:26 pm
by Fuzzy
If you are going to #define variables you should name them in such a way that it reminds you that they cannot be changed. Traditionally you would do this with all capitals.

#define SPEED 10