Scope - global $variable
by Norv Brooks · in Technical Issues · 01/17/2006 (8:30 am) · 5 replies
If you declare a global variable within a script - $variable = value; - how does it get its global scope? Can you declare a global variable within a script? e.g. init.cs in client
Thanks,
Norv
Thanks,
Norv
About the author
#2
// set up player skill level & icon variables
$playerSkill = "Rookie";
$playerSkillIcon = "\"./ui/Dialogs/skillLevelRookie\"";
echo("set up player skill level & icon variables");
echo($playerSkillIcon);
This seems to work; the console log shows the "echo" text like I would expect. Next, in signInGui.gui which I call from the mainMenuGui, I have the following code:
new GuiBitmapButtonCtrl(skillLevelNoviceBtn) {
Profile = "GuiDefaultProfile";
HorizSizing = "relative";
VertSizing = "relative";
position = "208 340";
Extent = "140 28";
MinExtent = "8 2";
Visible = "1";
command = "signInGui.setSkillLevelNovice();";
tooltipprofile = "GuiToolTipProfile";
tooltip = "Novice skill level";
text = "Button";
groupNum = "-1";
buttonType = "RadioButton";
bitmap = "./buttons/SignInBtnNovice";
};
In the above code the line: command = "signInGui.setSkillLevelNovice();";
this calls a function in "signInGui.cs"
In "signInGui.cs" there are four similar functions like the following one:
function signInGui::setSkillLevelNovice(){
echo("executing signInGui.setSkillLevelNovice");
$playerSkill = "Novice";
echo($playerSkillLevelIcon);
$playerSkillLevelIcon = "\"./Dialogs/skillLevelNovice\"";
echo($playerSkillLevelIcon);
Canvas.setContent("signInGui");
}
The 1st and the 3rd echo messages show up in the console log, but the 2nd echo is a blank line. This leads me to believe the $playerSkillLevelIcon declared in Init.cs goes out of scope and causes the blank line, while showing the new value when it's assigned in this function. Is my deduction wrong?
Thanks for your help Stephen
Norv
01/17/2006 (1:20 pm)
Stephen - I though global variables were suppose to work that way. Apparently, I doing something else wrong. I insert the following code into ./client/init.cs// set up player skill level & icon variables
$playerSkill = "Rookie";
$playerSkillIcon = "\"./ui/Dialogs/skillLevelRookie\"";
echo("set up player skill level & icon variables");
echo($playerSkillIcon);
This seems to work; the console log shows the "echo" text like I would expect. Next, in signInGui.gui which I call from the mainMenuGui, I have the following code:
new GuiBitmapButtonCtrl(skillLevelNoviceBtn) {
Profile = "GuiDefaultProfile";
HorizSizing = "relative";
VertSizing = "relative";
position = "208 340";
Extent = "140 28";
MinExtent = "8 2";
Visible = "1";
command = "signInGui.setSkillLevelNovice();";
tooltipprofile = "GuiToolTipProfile";
tooltip = "Novice skill level";
text = "Button";
groupNum = "-1";
buttonType = "RadioButton";
bitmap = "./buttons/SignInBtnNovice";
};
In the above code the line: command = "signInGui.setSkillLevelNovice();";
this calls a function in "signInGui.cs"
In "signInGui.cs" there are four similar functions like the following one:
function signInGui::setSkillLevelNovice(){
echo("executing signInGui.setSkillLevelNovice");
$playerSkill = "Novice";
echo($playerSkillLevelIcon);
$playerSkillLevelIcon = "\"./Dialogs/skillLevelNovice\"";
echo($playerSkillLevelIcon);
Canvas.setContent("signInGui");
}
The 1st and the 3rd echo messages show up in the console log, but the 2nd echo is a blank line. This leads me to believe the $playerSkillLevelIcon declared in Init.cs goes out of scope and causes the blank line, while showing the new value when it's assigned in this function. Is my deduction wrong?
Thanks for your help Stephen
Norv
#3
The second and third call $playerSkillLevelIcon and the second one is calling it before its declared... I'm assuming they are all supposed to be the same variable not 2 different ones right?
01/17/2006 (1:27 pm)
The first echo calls $playerSkillIconThe second and third call $playerSkillLevelIcon and the second one is calling it before its declared... I'm assuming they are all supposed to be the same variable not 2 different ones right?
#4
01/17/2006 (1:34 pm)
The variable names are different. In init.cs you have $playerSkill and $playerSkillIcon, whereas in the signInGui.gui you are echoing $playerSkillLevelIcon and then $playerSkillLevelIcon again. That first echo of $playerSkillLevelIcon is where the variable gets created with nothing in it since it hasn't been assigned a value, hence the blank line.
#5
Once again, thanks for your feedback.
Norv
01/17/2006 (3:42 pm)
Jonathan & Scott - thanks for your feedback. Yes, Scott, I did notice that error in the variable labels after I had posted the code. In the meantime I found that I could add my globals to $pref::Player:: and have done that, but probably correcting the labeling would have solved my origianl issue.Once again, thanks for your feedback.
Norv
Torque 3D Owner Stephen Zepp