Extent of Lua Global Variables

Started by
2 comments, last by mwmey1 15 years, 4 months ago
Hey all, Simple question: I call character-behaviour scripts each frame. Do the global variables I set in one frame retain their values when the script is executed in the next frame and so on? I would assume that they don't. If not, is there a way to implement this. I've read related questions but was unsure whether the answers referred to one script execution or many. Thanks alot, M
Advertisement
Global variables are bound to a lua state, not a specific script. So they should keep their values if you keep reusing the lua state.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

The idea of using the same script for a given set of objects, you'll need to do some setup work to do this correctly.

Since your calling the same script logic to operate upon a set of objects and those script function are just raw functions which have no concept of context other than the global tablem you'll have to create an system for accessing the current instance context.

C++ does this by using the concept of objects ( ie self contained code and instance ), in C++ you access the current context through the this ptr. You can implement the same ideas in Lua by creating Lua tables and use methods which access the self table, which corresponds to the context of that method call. This is basically OOP in Lua and for the most part will suit all your OOP needs.

in Lua (taken from Lua wiki):

Vector = { } -- Create a table to hold the class methodsfunction Vector:new(x, y, z) -- The constructor  local object = { x = x, y = y, z = z }  setmetatable(object, {    -- Overload the index event so that fields not present within the object are    -- looked up in the prototype Vector table    __index = Vector  })  return objectend-- Declare another member function, to determine the-- magnitude of the vectorfunction Vector:mag()  -- Reference the implicit object using self  return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)endvec = Vector:new(0, 1, 0)   -- Create a vectorprint(vec:mag())            -- Call a member function using ":"print(vec.x)                -- Access a member variable using "."


If you don't want to use OOP techniques and rather just build scripts that are always considered methods to some global context, there are several options.

I've seen some people use the concept of a this object living in global scope which stores the context for the current executing script. This this object is swapped out by higher level C++ code when executing the same script upon a set of objects. So the scripts explicitly assume the existence of a this object containing the current context ( ie any stateful data )

ie in Lua something like this : (presuppose C/C++ code has assigned correct this object for this instance)
--global this object exist at global scope, swapped out per execution of the script to correspond to current this context.this = {}this[hitpoints] = 100;function AddHitpoints(hp)  --note this is assumed to always exist and live in global scope.  this[hitpoints] = this[hitpoints]+hp;end 


Another method is to swap out the entire global table between scripts, this method is more extreme as it doesn't allow global data to be shared between scripts, without doing more work ( ie manually syncing up shared global data perhaps stored within some child table )

ie something like this .

--decleration of gHitPoints in global space, only instantiated once in LuagHitPoints = 0;function AddHitpoints(hp)  gHitPoints = gHitPoints + hp;end 


For some reason the + symbols are missing from the source..

Good Luck!

-ddn
Endurion: Thankyou very much for the reply. That's good news to me :).

ddn3: Thankyou also for the great amount of detail. Greatly appreciated.

Quote:this method is more extreme as it doesn't allow global data to be shared between scripts

So the global variables would be exposed to one script, and not the whole lua state?

Just to clarify, does it allow sharing between separate executions of the SAME script? ie Is it limited to one execution or to one script?

Many thanks
M

This topic is closed to new replies.

Advertisement