Jump to content
  • Advertisement
Sign in to follow this  
Plasmarobo

Embedding Lua with Global variables

This topic is 3643 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

I seem to have a problem. I have embedded Lua into my engine with few difficulties, however the functions that I use to shunt my data between the LUA scripts and the main engine uses a static namespace variable to hold the value of a class holding all the subsystems that I use to render or manager game elements. Now it seems to me that the pointer breaks occasionally. Unless I manually assign the value in the WinMain scope it becomes invalid (my printing routine breaks because of this). I have a few ideas on how to solve this problem. A) Move the LUA functions and destroy the global value. This would involve making each of my lua functions a member of a subsystem or the master class. I don't think this would solve the problem. B) Simply manually assign the value in the WinMain scope and forget about it. [Which is what I am doing now] C) Somehow lock the reference so it doesn't go out of scope. It seems to die when a member class is operating on it. Not sure if that is relevant or not. I could use some advice, I'm not really sure how to go about any of these. The last time I attempted to register member functions with LUA it screamed at me and threw things. I'm sure it was my inexperience and the fact that pointers hate me. My system right now is a convoluted nest of classes within classes within classes with a great deal of redundancy and ineffiency. However I am simply going for something that functions correctly right now. I can make it fast and pretty after. [Edited by - Plasmarobo on December 21, 2008 1:51:50 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what you mean by any of that. Member classes? Value of the control class? We could use some source, or at least some more context.

I tend to make any datatypes I want reachable by Lua into C structures with all methods operable on them from Lua installed in a metatable, and then I create up to two constructors for that type; one for calling from C, and sometimes one for calling from Lua (which I register as a global function, and which really just wraps the C constructor).

Take a look at this thread for a discussion and example of making a Matrix type used in a single instance from both C and Lua; similar code can be written for most any datatype.

Share this post


Link to post
Share on other sites
Unless you want the source of my entire engine posting source probably wouldn't be helpful. I can give a sort of example.

Let me be a little more specific: Lua interop or data exchange works fine.
I have a static pointer in a namespace that points to an instance of my container
class. I use this pointer to reference basically any point in my game (it gives
my subsystems access to my other subsystems.
Now, I'm not really interested in the best way to do this right now.

I guess my question is this:
How do you assign static namespace-scope pointers so that they don't go out of scope. Ever.

I have this:

namespace Game{
class Manager;
static Manager game=NULL;
class Manager{
//protected subsystems
//public accessors
};
}


Now, if I have another file:

namespace Game{
namespace Extended{
void SetGame(Game::Manager *ptr){ Game::game = ptr;}

}
}


Alright. So, the SetGame function doesn't seem to work. However...

WinMain(){
Game::Manager inst;
Game::game = &inst;
while(true){ //Conditional loop of some kind
}
}


That works just fine! I'm not really sure why.

So please tell me if a) this makes any sense
or b) there is a simple way I could change this without revising
every access to Game::game.

In retrospect, this would be a better topic for general programming or
for beginners...

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!