Jump to content
  • Advertisement
Sign in to follow this  
mwmey1

Extent of Lua Global Variables

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 methods
function 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 object
end
-- Declare another member function, to determine the
-- magnitude of the vector
function Vector:mag()
-- Reference the implicit object using self
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
end
vec = Vector:new(0, 1, 0) -- Create a vector
print(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 Lua
gHitPoints = 0;

function AddHitpoints(hp)
gHitPoints = gHitPoints + hp;
end




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

Good Luck!

-ddn

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!