PDA

View Full Version : Global variables resetting themselves?



Pleng
06-08-2009, 10:52 AM
I have added the following line to game_start.lua, in amongst all the variable declarations:



g_playerTouching = "";


I have then added the following code to one of my entity's scripts:



function OnCollision(vPosition, vVelocity, vNormal, depth, materialID, entity, state)


if (GetPlayer:GetID()==entity:GetID()) then

//The collision is with the player

g_playerTouching = this:Data():Get("overtext");

//DEBUG
LogMsg (g_playerTouching);
//END DEBUG

GetPlayer:RunFunction("UpdateGUI");

end


end


Now it's a brand new variable but somewhere the logic cycle it is getting reset. When the collision takes place the LogMsg in the script will keep on being echoed to the console. But if I open the console and enter LogMsg (g_playerTouching) I am presented with a blank line - this happens either when the console is called up when the collision is still occurring, or after it has finished (the player has moved away from the item).

It also definitely happens before UpdateGUI is called, as the following line is in my interface.lua's update step:



m_object_text:RunFunction("SetText", g_playerTouching);


Any ideas why this is happening?

Seth
06-09-2009, 12:01 AM
Well, it should work. Maybe try putting _G. in front of your global var everywhere you access it (reading or writing), to insure it is a global that is being accessed and not the entity's namespace.

So, instead of:

g_playerTouching = "";

do:

_G.g_playerTouching = "";

Pleng
06-09-2009, 07:28 AM
Yup. that's fixed the problem.

Thanks :D

sirlich
10-13-2009, 01:07 PM
Let me see if I understand this correctly:

the _G will set variables as global so every other script has read/write access to it?

If so, then can that global be declared in ANY script?

Seth
10-13-2009, 01:50 PM
Yep, that's right!