Game Development Community

Local variables and files .Cs

by Yue -Rookie Torque 3D- · in Torque 2D Beginner · 09/27/2013 (9:31 pm) · 9 replies

The scope of local variables is widely applied to files?

// Main.cs
%Name = "John";

//Main2.cs
exec(Main.cs);
echo(%Name);

This variable is eliminated alone, or touches deleteVariables use something like ()?

About the author

http://www.iris3d.tk -Games Studios- <<I do not speak English: I use the google translator>> My goal as a rookie is knowing Torque 3D


#1
09/27/2013 (9:54 pm)
The variable is deleted, use $Name for globals. Maybe you should ask your questions in both english and spanish. Bilingual users can respond in both languages as well.


Spanish:
El variable sera destruido, utilize $Name para globales. Quizas deberias hacer preguntas en ambos idiomas.
#2
09/27/2013 (10:02 pm)
Local variables only exist within the scope of the function they are contained within. To make your example above work, you would need to use a global variable.

See the section on variables here:
github.com/GarageGames/Torque2D/wiki/TorqueScript-Syntax
#3
09/28/2013 (8:35 am)
Sorry, I made a mistake this should go in torque 3d for beginners, yet it seems that the principle is the same with the variables.

As had understood if home declared a variable in a file, it would only be possible to recover your data within the source file (main.cs), however the value of the local variable declared as what I can recover from another file and show in the console work if any problems.
#4
09/28/2013 (8:47 am)
i44.tinypic.com/2evgbiu.jpgi42.tinypic.com/ormq21.jpgi42.tinypic.com/33eu3ut.jpg
This leads me to the conclusion that local variables are declared as accequibles from any source file and the only difference is about the roles where you can not take your content value.
#5
09/28/2013 (3:20 pm)
Um, that shouldn't be happening o_O

local vars being accessed through multiple files? I think TorqueScript has derped itself.
#6
09/28/2013 (3:44 pm)
As if this happening, I declare I have local variables and access them from another file, however in all continuous functions equal, I have no access to local variables.
#7
09/28/2013 (4:16 pm)
This topic pops up every few years or so, depending on who is most bent on exposing TorqueScript quirkiness. What Yue -Rookie Torque 3D- (can I just say "Yue"?) has done is expected behavior. When someone asks about the main rules of TorqueScript variables, here is my response:

1. All variables are strings

2. Local variables are defined with at %

3. Global variables are defined with a $

4. Local variables typically scope to the function they are declared in, then are deleted after the function executes.

5. Global variables exist in all scopes.

6. When a local variable is declared outside of a function, it gets scoped globally due to the way script execution works. This is the same case with console code executing.

It's not that it has derped itself. I've seen this for years. It's just that you don't see many people declare a local variable outside of a function. That kind of behavior is relegated to utility scripts that are passed into the executable.

I love threads like these. It is both beginner and professional. It sparks up a discussion that really digs into the engine. Not to derail, but look into the --> operator sometime. Same kind of fun exploration like Yue has discovered.

Edit - There is a deeper, technical explanation of how this works. If anyone is interested, we can dive into the specifics. At that point, we will be digging into the script interpreter, string dictionary, and so on.
#8
09/28/2013 (4:30 pm)
Well, I have always something to learn every day that comes, and this community is a help to overcome my ignorance and I'm glad understand a bit more the issue with the help of you (Michael), my question is, if I declared the variable Local outside a function, it is removed only when the program ends?

%NameUser = "John";
function UserName(%Name)
{
    echo(%Name);

}

UserName(%NameUser);
#9
09/28/2013 (4:57 pm)
@Yue - If declared outside of a function, the value of %NameUser will exist as long as Torque is running. Once you close Torque, the memory leaves. If you reassign %NameUser in a function...well, I've never actually tried that before. Not sure, what happens without actually debugging it.