Using LUA for scripting

Started by
51 comments, last by smokes 21 years ago
Any ideas how to pass a Lua table as a parameter and save it in C/C++?
---------------------http://www.stodge.net
Advertisement
I''ve been checking the book stores for books on Lua but none to be found. Anyone know of any books on Lua out there already or headed for the press?

Value of good ideas: 10 cents per dozen.
Implementation of the good ideas: Priceless.
Proxima Rebellion - A 3D action sim with a hint of strategy
Value of good ideas: 10 cents per dozen.Implementation of the good ideas: Priceless.Machines, Anarchy and Destruction - A 3D action sim with a hint of strategy
Game Scripting Mastery has a good introduction to it.

-nosfy
Lead programmer
Creation Objet Inc.
stodge: You can''t load any data using lua_dobuffer. This function expects to receive a memory buffer containing the script (compiled or ascii) and the length of the buffer.

To execute a script, compiled or not, from a file use lua_dofile(). This is what I use most of the time (excepted for the console where I use lua_dostring()).

-nosfy
Lead programmer
Creation Objet Inc.
do_file loads compiled bytecode? I didn''t realise that!

Thanks!
---------------------http://www.stodge.net
Thanks matthias6S , gonna try it out
Yes lua_dofile() loads compiled byte code. In my game I have a flag that tells my scripting interface to load compiled or ascii script (development vs production environment).

-nosfy
Lead programmer
Creation Objet Inc.
So you have to specify if the file is bytecode or source?
---------------------http://www.stodge.net
Nope, you don''t have anything to do. It''s just that my compiled scripts have the extension .cscript and my source .ascript. So if I want to load the source (which will be compiled at load time) I will load the .ascript file. If I want to load the compiled script I will load the .cscript file.

Thus the need for the flag. : ) You could put them in different folders too, it''s another alternative.

The thing is, you need to make sure that when you load the compiled scripts they are the compiled versions of your latest ascii scripts. In your build process you have to issue commands to build your scripts.

-nosfy
Lead programmer
Creation Objet Inc.
Sorry for the bumpage, but it seemed sensible rather than start a new thread.

I''m stumped. Any advice gratefully accepted. Caveat: only an average C++ programmer

I''m in the process of implementing a scripting engine using Lua and C++ (Lua v4 and Visual C++ 6.0). The engine stores a std::vector of objects (see below), each object spins off a thread with _beginthreadex() and each thread sits in a loop with a timed "heartbeat" and executes a Lua script with each "pulse".

  class CMyClass {private:	lua_State *lvm;	int m_MyData;	UINT uiThreadID;	// other member datapublic:	// constructors, etc. lua_State is opened in constructor and closed in destructor	RunMain(); // this is the main loop we pass to _beginthreadex()	int GetMyData();};  

As you can see, each object instance runs an instance of its own lua_State.

The problem is, whilst I can use tools like toLua to expose the class and its methods to Lua, to my understanding (having read the toLua documentation) this only allows me to do things like create new instances of CMyClass objects using Lua scripts.

What I''m after is a way to expose the member data of a class that is already running its own Lua VM to its scripts and any other (non member) functions that I''ve made visible to Lua. For example, an instance of CMyClass on thread #1 runs a script called PrintMyData.lua - I want the script to be able to call its calling object''s GetMyData() member function and retrieve the value for m_MyData (and only its calling object''s value - threads, remember).

It would also be nice to have other functions (e.g. GetNearestTarget()) outside CMyClass that have been exposed to Lua, to accept instance of CMyClass objects and perform some action upon them. Is it possible to expose a function to Lua that accepts a reference to an existing CMyClass object? Hmmmm, I guess that would involve pushing an object onto the Lua stack and then retrieving it in the function?

Is there anyone out there that has used Lua like this before (there must be)? Is it possible? Have I mis-read the toLua docs?

Thanks in advance.

-- Mull

This topic is closed to new replies.

Advertisement