Global Scope for ScriptObject
by Farao · in Torque Game Builder · 07/21/2011 (3:15 pm) · 2 replies
Hi everyone!
I'm trying to load data from a txt file (for example a savegame or options) when I my game start so I can have acces to it globaly in my code. This is what "troops.cs" contain, wich I call in my exec.cs:
My first 2 echo work as expected. But the get_troopsId() function always return 0. I know is is due to %troopInfo being wrongly called/defined so it fall out of global scope, but whatever I try never work.
Any hints or ideas?
I'm trying to load data from a txt file (for example a savegame or options) when I my game start so I can have acces to it globaly in my code. This is what "troops.cs" contain, wich I call in my exec.cs:
// Load Troops Data
%file = new FileObject();
if(%file.openForRead("~/game/data/troops.txt")) {
$i=0;
while(!%file.isEof()) {
%line = %file.readLine();
$id = getWord(%line, 0);
if ($id!="*") {
%troopInfo[$id] = new ScriptObject() {
id = getWord(%line, 0);
nation = getWord(%line, 1);
intId = getWord(%line, 2);
name = getWord(%line, 3);
(...)
};
$i++;
echo("Troops line " @ $i @ " = " @ %line);
}
}
%troopsInfoQty = $i;
echo("- DEBUG: troops.txt loaded: " @ %troopInfo[1].name);
}
else {error("CANNOT OPEN Troops.txt FOR READ");}
%file.close();
%file.delete();
// Get Troops Id
function get_troopsId(%nation, %intId) {
%res = 0;
for (%i = 0; %i < %troopsInfoQty; %i++) {
if (%troopInfo[%i].nation $= %nation && %troopInfo[%i].intId $= %intId) {
%res = %troopInfo[%i].id;
}
}
return %res;
}
echo("- DEBUG: get_troopsId test: " @ get_troopsId(1,2));My first 2 echo work as expected. But the get_troopsId() function always return 0. I know is is due to %troopInfo being wrongly called/defined so it fall out of global scope, but whatever I try never work.
Any hints or ideas?
#2
On an unrelated topic, but still worth asking since I'm sure it's another tiny little thing: I cant close any GuiWindowCtrl I open with Canvas.pushDialog(xyz);. Clicking on the X, or calling %this.exit(); dosent work even when the GuiControl have a name. Any idea?
Again, thanks a lot for your quick answer!
07/21/2011 (8:49 pm)
Doh! Stupid copy / paste... at some point everything start to blur a bit and you dont notice tiny details like this. Thanks a lot for slaping some sense into me, hehhehe.On an unrelated topic, but still worth asking since I'm sure it's another tiny little thing: I cant close any GuiWindowCtrl I open with Canvas.pushDialog(xyz);. Clicking on the X, or calling %this.exit(); dosent work even when the GuiControl have a name. Any idea?
Again, thanks a lot for your quick answer!
Torque 3D Owner Matt Huston
Atomic Banzai Games
Basically, %troopsInfoQty needs to be $troopsInfoQty and %troopInfo needs to be $troopInfo. The $i only needs to be a %i.