Game Development Community

Passing variables between scripts.

by Jack-S- · in iTorque 2D · 10/07/2011 (5:34 am) · 2 replies

Hi I was wondering if anyone could possibly give me some examples of how to pass variables between 2 different functions which exist in different scripts files.
For instance, I have two functions which do completely different things, my variable x with contents -10 exists in a function in script 1. I wish to move variable x and its contents into a different function which exists in script 2 so that further calculations can be made with its contents.
I have already had a fair whack at this but Im clearly doing it wrong as whenever I check x and its contents in script 2 it reads as having no value...
So I would be grateful if someone could hit me with some examples of how to do this.
Thanks in advance.

#1
10/07/2011 (5:51 am)
There are two ways:

1. Store the value in a global variable: $myVariable = 3;

2. Use a ScriptObject.

new ScriptObject(variableContainer)
{
   myVariable = 3;
};


If you are not doing this often, use a global variable. If you are tracking many variables, use the ScriptObject.
#2
10/07/2011 (4:20 pm)
@ michael - Thanks, I want to try and avoid global variables as much as possible.