Jump to content
  • Advertisement
Sign in to follow this  
algumacoisaqualquer

Going crazy with lua

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

Ok, I have a racing car demo (with OpenGL, copy from NeHe Lesson24, but it doesen't matter right now), and I wanted to load the tracks from a file. I tried to make a file loader function, but I couldn't get it to work, so I decided to try lua (it's a scripting language, there's an article here on GameDev) as I wouldn't need to deal directly with the file. I did managed to make the example work (with some modifications), so I tried to place it on my game engine. It worked at first, with a test funtion, but the real functions that I'm interested in are all inside classes, and in diferent files to make it worse. Ok, so I added my lua functions to my the files. I have a CGameHold class, and inside it I have the functions I'm interested in, things like: GameHold.MainTrack.AddTerrain(index, maxspeed, aceleration, negaceleration, aderence); and similars. I tried making my lua functions globals, but when I do that, I have no reference to my Main GameHold class (it's a global class, but it's declared on Main.cpp, and my lua functions are on GameHold.cpp), so when I compile the compiler will complain that there is no GameHold. Then I temporarely moved all my lua functions inside GameHold. However, when I try to register there functions by calling
lua_register( state, "DoStuff", this->l_DoStuff);

I get the error:
57 C:\Dev-Cpp\Mine\Race\GameHold.cpp argument of type `int (CGameHold::)(lua_State*)' does  not match `int (*)(lua_State*)'

What does that means? I see no problem in my code, but I have little undestanding of function pointers. Everything is compiling fine... except for that line. When I coment it out, everyting works fine!

Share this post


Link to post
Share on other sites
Advertisement
you cant register nonstatic functions of classes with lua.
you have to call static functions with the pointer to the object as parameter, look for userdata in the lua manual.

Share this post


Link to post
Share on other sites
There are some good wrappers available for the LUA API that allow you to export classes and their member functions to C++.

Google for toLUA and LuaBind.

Share this post


Link to post
Share on other sites
hum... ok, thanks a lot! I tried looking at toLua and LuaBind, but I didn't understood how they really work. Right now, I just moved my functions to Main.cpp. I know it's a bad idea, but at least it worked...

(...)

Ok, now I have another problem... Here's what happens: I have a function AddTerrain(...) being called from my lua script. the registered function is l_AddTerrain:


int l_AddTerrain(lua_State* luaVM) // int index,float maxspeed,float aceleration,float negaceleration,float aderence
{
MessageBox (NULL,"start adding...","AKA",MB_OK);
int index;
float maxspeed;
float aceleration;
float negaceleration;
float aderence;
index = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1 );
maxspeed = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1);
aceleration = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1);
negaceleration = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1);
aderence = lua_tonumber(luaVM, 1);
GameHold.MainTrack.AddTerrain(index, maxspeed, aceleration, negaceleration, aderence);
//MessageBox (NULL,"What the hell?? I get an critical error if I take this message box out of here!!!","AKA",MB_OK);
}


Now, if I run this function passing 4 or less parameters (ex. AddTerrain() or AddTerrain(7, 8) ) It will work out fine, but if there are more then 5, I will get my program shutdown by windows ("this program caused an ilegal operation and will be closed..."). The weird part is... if I add a MessageBox in the end of the function, everyting will work fine. I checked the GameHols.MainTRack.AddTerrain function, it is working fine, the problem is in some lua function. Oh, and another function I'm calling from lua is DoStuff(...) wich takes to l_DoStuff:

int l_DoStuff(lua_State* luaVM)
{
float a, b, c, d;
a = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1 );
b = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1);
c = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1);
d = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1);
d = lua_tonumber(luaVM, 1);
lua_remove( luaVM, 1);
d = lua_tonumber(luaVM, 1);
char buffer[10];
itoa((int)(a + b + c + d), buffer, 10);
//MessageBox(NULL,buffer,"ERROR",MB_OK|MB_ICONEXCLAMATION);
return 1;
}


Now, DoStuff will work even with the MessageBox comented out, and with any number of arguments I send to the function!

Hum... I don't want to waste people's time here because I'm trying to do something I obviulsly didn't understood how it works, but if anyone spot an obvius error in here or some thing like that, please point me out. Thanks!

Share this post


Link to post
Share on other sites
From what you just said, it sounds like you're trying to remove 4 parameters from the stack even when you pass it fewer than that. I expect that is liable to make it crash. Check the result of lua_gettop to see how many you were actually passed.

(PS. I moved this to the Scripting forum.)

Share this post


Link to post
Share on other sites
Duh... I was going to try your sugestion, When I went to my only working function, l_DoStuff, to copy some lines for the other function... That's when I realised that there was a return 1; in the end of the function... that was the problem... I have no idea why, but when I fixed that (putting return 1; in the end of every funciton, since they where int l_DoStuff) everything worked fine.

Btw, Sorry for the wrong forum. I had never posted in the Scripting forum before, so I completly forgot about it.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!