Use of Lua_State

Started by
7 comments, last by aayudh 16 years, 4 months ago
Hi, I'm making a small 2D space shooter but I want to make it an open platform so I'm going down the scripting route. I have two classes Entity and EntityInstance, Entity basically contains a map of attributes (things like health, maximum speed, weapons etc) that are common between all EntityInstances of it's kind (Entity types are loaded from XML files). EntityInstance is the instantiation of those Entities in the engine. Now each EntityInstance is pretty generic (it contains code for position, basic movement and animation but not much beyond that) so the specifics of each EntityInstance will have to be done in script. I've decided to use Lua for scripting due to it being both easy to write and also relatively easy to set up. My question is the scripts will contain their own global variables (such as current health, selected weapon etc) and it's own logic (such as an update function which is called from code) meaning that I'll need a seperate Lua stack for each EntityInstance, so does this mean each EntityInstance will have to hold it's own Lua_State and I'll have to load and compile the script each time for each EntityInstance, or could I do something like hold a single Lua_State in the Entity class and spawn child states off of this entity state for each instance, would that work, meaning manipulating the variables in script for one instance won't affect the others?
Advertisement
There's a few reasons to have multiple Lua_States, but none of them really fit your scenario. Why do you want each EntityInstance's view of the Lua universe to have different globals? Shouldn't things like health, weapon, update function, etc. not be global?
I meant global to the script, not the program as a whole. Let's say I define a variable in script called health which will initially contain the maximum possible health but decrease when the entity is hit with a bullet. I need this health variable defined inside the script to remain persistant for each entity instance, so one entity instance can have a different health value than another, and let's say each script has an update function defined in it, when the code calls this function that function should have access to the calling instaneces specific health value, so it can act accordingly like run away or something. The thing is I'm not sure how to approach this other than having seperate lua states. I'm trying to avoid having variables definied in code and then the script simply manipulating them, I'd rather the script held it's own.
Quote:Original post by M_Johnson
I meant global to the script

Then you mean global. Global is global. Don't try to subvert the concept by giving your entities different globes. How about a table for each entity to store its data?
Can global's contain different values though by using different lua_states? Did you mean a table in code or in lua?

Also a quick question slightly unreleated. Let's say you load two seperate scripts using the same lua_state and both scripts contain a global variable called 'a' for example, when trying to access the global 'a' how do you specify which one to access?
Quote:Original post by M_Johnson
Can global's contain different values though by using different lua_states?

Yeah, and I'm telling you right now that that's a bad way to do it. You'll end up spending a lot of time and extra memory keeping the states in sync. Use one Lua state.
Quote:Did you mean a table in code or in lua?
In Lua. Probably with a reference held by the host language. I'm not sure what you mean by "code"... it's all code.
Quote:Also a quick question slightly unreleated. Let's say you load two seperate scripts using the same lua_state and both scripts contain a global variable called 'a' for example, when trying to access the global 'a' how do you specify which one to access?

Alright. So I'm going to rant about scripts right now.

So there's no such thing as a "script". What people learning Lua tend to think of as a script is a function containing a bunch of lua statements and other lua functions and lua variables. Lua's syntactic sugar for defining functions make this particularly pronounced, I think, but you've already gotten that variables can be declared at global scope by "scripts" so I'll just skip that part.

A script is just a function. When you do lua_loadfile (or lua_dofile), the Lua library essentially puts a "function() do" at the beginning and a "end" at the end of the text, and then compiles the result. That is, a "script" is just a function. During its execution, it may change the global table by putting functions and other variables in it. If it steps on some other variables already in the global table, it overwrites them. So there are not multiple global 'a's. There's just one. The global one.

Now a slight complicating factor. Some people would like to make a .lua file with an update function and a variable called "a" and want all that stuff to go into a table which is not the global table. You can make that happen: Just set the function environment of the loaded function to a new table before executing it. (Keep in mind that the script will see this empty table as its global table, so you can't really do significant computation in the script; this doesn't apply to functions which are put in the table by the script, though, if you execute them later without giving them a special function environment. Confused yet?)
Quote:Original post by Sneftel
Now a slight complicating factor. Some people would like to make a .lua file with an update function and a variable called "a" and want all that stuff to go into a table which is not the global table. You can make that happen: Just set the function environment of the loaded function to a new table before executing it. (Keep in mind that the script will see this empty table as its global table, so you can't really do significant computation in the script; this doesn't apply to functions which are put in the table by the script, though, if you execute them later without giving them a special function environment. Confused yet?)


Do you mean something like this:

Enemy_DumbEnemy = { }Enemy_DumbEnemy["Health"] = 100Enemy_DumbEnemy["Update"] = function(param)--Do somethingendEnemy_DumbEnemy["OtherFunction"] = function(param)--Do something elseend


So by doing that I won't be able to access the globals from the functions? Could then in another file I have:

Enemy_SmartEnemy = { }Enemy_SmartEnemy["Health"] = 200Enemy_SmartEnemy["Update"] = function(param)--Do something smartendEnemy_SmartEnemy["OtherFunction"] = function(param)--Do something else smartend


It should be noted I'll probably use LuaBind, so I could use lua::object variables to reference these tables. However, I'll still run into the problem of how to handle different instances of smart enemy for example. I could store the instance dependent variables (like current health) in a seperate table I suppose, and then just import them in for each entity like you said.

[Edited by - M_Johnson on November 29, 2007 11:12:58 AM]
Quote:Original post by M_Johnson
Quote:Original post by Sneftel
Now a slight complicating factor. Some people would like to make a .lua file with an update function and a variable called "a" and want all that stuff to go into a table which is not the global table. You can make that happen: Just set the function environment of the loaded function to a new table before executing it. (Keep in mind that the script will see this empty table as its global table, so you can't really do significant computation in the script; this doesn't apply to functions which are put in the table by the script, though, if you execute them later without giving them a special function environment. Confused yet?)


Do you mean something like this:

Enemy_DumbEnemy = { }Enemy_DumbEnemy["Health"] = 100Enemy_DumbEnemy["Update"] = function(param)  --Do somethingendEnemy_DumbEnemy["OtherFunction"] = function(param)  --Do something elseend


Not quite. What I mean is something like this:

health = 100function update(param)  --Do somethingendfunction otherfunction(param)  --Do something elseend

Then, after you load that chunk, you call lua_setfenv on it to set its environment to a new table, before calling it to fill that table. See the manual for more information.
Hi,

I have a strange question. is it possible to spawn(or open) a lua console for the created Lua_state* ?????

This topic is closed to new replies.

Advertisement