Jump to content
  • Advertisement
Sign in to follow this  
D3DXVECTOR3

[LUA] I have a problem that I dont understand...

This topic is 4970 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 ahve 2 functions in a file and I call lua_dofile to execute this file. This file contains a few calls to C funtions and 2 lua fucntions. Here is what thet look like:

-- These functions are registered C functions
SomeCFuntion()
NewFunsiont()

-- Lua function
function LoadOggFile()
	local iFileID = -1
	iFileID = LoadVorbis("Data/Music/Sirius909.ogg", true)
	if (iFileID >= 0) then
		PlaySoundF(iFileID)
		InfoVorbis()
	end
end

-- call the lua function above ^^
LoadOggFile()

-- More C funtions
MoreCFuntions()




^^ This one crashes at the 'if' statement. If I remove the 'if' statement then it works perfectly. Continueing in the scipt file...

-- More C funtions
MoreCFunctions()
MoreCFunctions()

-- Lua function
function LoadSoundFile()
	local iFileID = -1
	iFileID = LoadSoundF("Data/Sounds/Rumbling.wav", true)
	if (iFileID >= 0) then
		PlaySoundF(iFileID)
	end
end

-- call the lua function above ^^
LoadSoundFile()

-- And again more registered C funtions
YetAnotherCFunctions()




In this function the 'if' statement doesnt crash but it looks like it doesnt work. When I remove the 'if' statement then everything work like it should work. And one more thing: When are they gonna fix the SEARCH on this forum!? [Edited by - D3DXVECTOR3 on January 5, 2005 9:11:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
Yes I'm sure it return an integer. LoadSoundF used 2 look like this (see below) and it worked, but I wanted place the iFileID in the lua script.


int LoadSoundF(lua_State *L)
{
if(lua_gettop(L) == 2) {
int iFileID = -1;
iFileID = dOpenAL->LoadSound(lua_tostring(L, -2), (ALboolean)lua_toboolean(L, -1));
return iFileID;
}

return -1;
}





I was just thinking "LoadSoundF" is a C function, can LUA use the return value of this function just like that or do I need 2 do something like lua_push<something>!?

Edit:
I just took a better look into the manual and now its all clear 2 me. The return value from the c function is the number of values pushed on the stack. So i need to do a lua_push 1st and then return a 1 because I only pushed 1 value on the stack.

From the lua manual:
In order to communicate properly with Lua, a C function must follow the following protocol,
which defines the way parameters and results are passed: A C function receives its arguments from
Lua in its stack in direct order (the first argument is pushed first). So, when the function starts, its
first argument (if any) is at index 1. To return values to Lua, a C function just pushes them onto
the stack, in direct order (the first result is pushed first), and returns the number of results. Any
other value in the stack below the results will be properly discharged by Lua. Like a Lua function,
a C function called by Lua can also return many results.
As an example, the following function receives a variable number of numerical arguments

Edit 2:

Yes it works. The C functions now looks like this and the lua function remains the same.


int LoadSoundF(lua_State *L)
{
if(lua_gettop(L) == 2) {
lua_pushnumber(L, dOpenAL->LoadSound(lua_tostring(L, -2), (ALboolean)lua_toboolean(L, -1)));
return 1;
}

return 0;
}



[Edited by - D3DXVECTOR3 on January 6, 2005 12:38:11 PM]

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!