Advertisement

how do i do such a thing in Lua ????

Started by July 18, 2004 11:17 PM
14 comments, last by GameDev.net 20 years, 4 months ago
That is really not right, Lua is most definately capable of creating and using C++ objects.

They are called Userdata.

Read about them here:
http://www.lua.org/pil/28.html

And calling C from Lua here:
http://www.lua.org/pil/26.html
I don't think anyone said Lua wasn't capable, only that it's usually a poor way of approaching the problem.
Advertisement
before i spend the next few days driving myself crazy trying to figure out how to do this in Lua (i plan on writing wrapper C++ functions so i dont have to directly allocate memory or de-reference pointers, instead ill just call the C++ functions from Lua...). why exactly is it a bad / poor idea? thanks for any help!!
FTA, my 2D futuristic action MMORPG
ok, well, it took awhile, but... i finnally figured it out!! i designed and implemented a system to encapsulate my tree building system! basically, i encapsulate the pointers so that instead of having to call new, and having to de-reference pointers, i use a std::map<int,NPC_Reply*> nodes;. then, i store all my node's in this map. whenever i want to de-reference a node, i just call a function sending the node's ID #! no more pointers needed! at least not in the Lua interface!

now, i can make my wrapper class acessible from Lua. now theres just one more problem i cant solve : how do i return this object once i fill it with data in Lua? this is what ive got so far:

I have a class called Conversation. I make my class and its members available to Lua via Module(L)[];

now, once i do this, i write a Lua script which instantiates a Conversation object, and builds the dialogue tree. now i just have to return this object! how how how do i do this? how do i call my Lua script, and have it return my object that was created? lets say i have a Lua script which looks like this:

function Fill_Convo()	        Conversation convo()        --now i build my dialogue tree, filling convo with all the neccesary data        return convoend


how do i do such a thing? how can i call this function from my C++ code and have it return the object? like i said, i can set up Lua to know about my object, but how do i return it to my C++ code? ive searched the luabind docs and the lua docs and cant figure it out! am i supposed to use lua

Conversation Get_Convo(){		/* the function name */	lua_getglobal(L, "Get_Convo");	// call the function with 0	arguments and return 1 result 	lua_call(L, 0, 1);	//get the result	Conversation *convo lua_touserdata(L,1);; 		//pop the value off the stack	lua_pop(L, 1);	return convo;}


then i would just do something like

Conversation convo = Get_Convo();

but i get an error : cannot convert from void * to Conversation*. also, i tried casting but that didnt work. am i even doing the right thing? and if so, what am i doing wrong? thanks for any help!!!

[Edited by - graveyard filla on July 20, 2004 5:49:27 AM]
FTA, my 2D futuristic action MMORPG
I'd put the whole dialog into an Xml file. I think this would work very well. Each dialog node would also have a flag if the option/response is available, and some kind of ID that would trigger a script (if wanted) to set some variables/perform some tasks.
I think writing dialogs directly in Lua/C++ doesn't look that nice.
You could even make a little dialog-editor that spits out the right Xml to be used by the game.
The only problem I see, otherwise it looks fine.

Conversation *convo lua_touserdata(L,1);;

should be

Conversation *convo = (Conversation *)lua_touserdata(L,1);

Now, I don't really think that you need to call a function like that from C++, instead I would recommend that you create some binding functions to cover the functions used in the C++ object, then create a metatable to fold those functions in lua.

Then when you create your userdata, assign the metatable to the user data.

Lets say you had a method inside Conversation called "hello()" that calls some of your windowing code to display a "Hello world!" box.

Then you would create a binding function in c that goes something like:

static int LuaConvoHello (lua_State *L)
{
Conversation *convo = (Conversation *)lua_touserdata(L,1);

convo->hello();

return 0; // No arguments back to lua.
}

Now as long as you have this function in the metatable, and your new function sets the meta table on your user data, in your luacode you can do:

Convo = Conversation:new()
Convo:hello()

And it will properly call the C++ function.

Hope that helps.

This topic is closed to new replies.

Advertisement