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

Started by
1 comment, last by D3DXVECTOR3 19 years, 3 months ago
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]
Advertisement
Are you sure that your functions that load the sounds and
then the results to 'iFileID' contains an integer ?
That's the only thing I can think of is the problem.
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]

This topic is closed to new replies.

Advertisement