Game Development Community

Forward reference globals

by Howard Dortch · in Torque Game Engine · 10/15/2004 (8:38 am) · 3 replies

Hard to explain this one in words so simple example

exec (fileA.cs)

in fileA.cs there is a function that references a global in a file that has not been exec'd but function is not called yet.
function foo()
{
echo("GLOBAL IS" @$GlobalVal); <<<< always prints a 0
}


next we exec the file with the global

exec (fileB.cs)

function bar()
{
$GlobalVal = 1;
foo(); <<<< prints a 0 ?
}


so the question is $GlobalVal gets set in a file exec'd after it's referenced for a print by the function foo() in fileA.cs
is not called untill after function bar() and it always prints a 0 reguardless of the value set in bar() is this proper operation?

Is this a problem with forward referencing or needing a preload or?

#1
10/15/2004 (1:46 pm)
Hi Howard,

I think there's just a mistake somewhere in your code. :)

First, does bar(); get called in main.cs or what?

Next... is the code exactly as you paste it above? Including the missing semi-colons on the execs? :)

Here's code that works for fileA.cs...
echo("start fileA");


function foo()
{
   echo("Inside foo... and the globar var is: " @ $globalVar);
}


echo("end fileA");

Here's code that works for fileB.cs..
echo("start fileB");


function bar() 
{
   $globalVar = 1;
   foo();
}


echo("end fileB");

Here's code that works for main.cs...
setLogMode(2);

echo("start test");

setModPaths("scripts");
exec("scripts/fileA.cs");
exec("scripts/fileB.cs");

bar();

echo("end test");


quit();

function onExit() {}

This outputs...
//-------------------------- 10/15/2004 -- 15:17:52 -----
start test
Loading compiled script scripts/fileA.cs.
start fileA
end fileA
Compiling scripts/bar.cs...
Loading compiled script scripts/fileB.cs.
start fileB
end fileB
Inside foo... and the globar var is: 1
end test

Let me know if that works for ya, or if I misunderstood your post or something. :)
#2
10/15/2004 (2:39 pm)
Well this is one of them chicken/egg things as I discovered.
I'm trying to load bot information from a file so I put that part of the code in the fps/client/scripts. The global I refer to is a bot count I read from the file. The problem is the bot info resides in the fps/server/scripts directory because thats where I create the bot players and at the time the gui routines are loaded, the server/game.cs file hasn't been exec'd so the $Global is actually created in the client because it doesn't yet have a reference defined. Then the server gets launched and the $Global got over written by the new bot.cs file.
I solved the problem in a really really ugly way by creating a temp global in the gui routines and when the server gets fired up it coppies the data over when it spawns the bots. See what I'm after?
I was trying to be clean in the way I organized the files so gui stuff is in fps/client/scripts and the player creation in fps/server/scripts but it looks like I might have to define the bot in the gui code but then the bot has a reference to a player datablock which doesn't exist at the time the gui is running.
#3
10/15/2004 (6:37 pm)
Hmmm,

Seems that the problem must be somewhere else. I have a global bot count value that's attached to the GUI. It works with no problems.

Maybe the file is being read after the bot function is called. Or maybe you have two globals with the same name, one in the client and one in the server.

I checked my code and saw that I created the global in my game's main.cs file. Maybe this will make sure that the global that is read is the same that you check