Results 1 to 5 of 5

Thread: Global variables resetting themselves?

  1. #1
    Lesser Wizard
    Join Date
    Mar 2008
    Posts
    124

    Default Global variables resetting themselves?

    I have added the following line to game_start.lua, in amongst all the variable declarations:

    Code:
    g_playerTouching = "";
    I have then added the following code to one of my entity's scripts:

    Code:
    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:

    Code:
    m_object_text:RunFunction("SetText", g_playerTouching);
    Any ideas why this is happening?

  2. #2
    Administrator Seth's Avatar
    Join Date
    Jul 2002
    Location
    Japan
    Posts
    5,343

    Default

    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 = "";
    Seth A. Robinson
    Robinson Technologies

  3. #3
    Lesser Wizard
    Join Date
    Mar 2008
    Posts
    124

    Default

    Yup. that's fixed the problem.

    Thanks

  4. #4
    Squire
    Join Date
    Sep 2009
    Location
    Colorado, USA
    Posts
    25

    Default Global variables

    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?
    Last edited by sirlich; 10-13-2009 at 01:20 PM. Reason: change "set" to "declared"

  5. #5
    Administrator Seth's Avatar
    Join Date
    Jul 2002
    Location
    Japan
    Posts
    5,343

    Default

    Yep, that's right!
    Seth A. Robinson
    Robinson Technologies

Similar Threads

  1. Global Game Jam
    By sphair in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 01-03-2009, 02:29 PM
  2. Accessing the global application instance
    By Iori Branford in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 01-05-2007, 07:01 AM
  3. FQ not resetting a new day...
    By in forum Funeral Quest
    Replies: 5
    Last Post: 12-01-2003, 04:24 AM
  4. Dink Engine variables get confused
    By in forum Dink Smallwood HD
    Replies: 1
    Last Post: 11-11-2002, 09:17 PM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •