Advertisement

is it possible with lua

Started by November 29, 2003 03:24 PM
6 comments, last by DirectXXX 20 years, 11 months ago
suppose in my game two balls may collide and if there is an intersection i will call a function ie React(). C does not know what React() is. It is just a pointer to some other function. the other functions which React() may point in C can be... blowup() bounce() which function is to call? will be defined by lua. is it possible.
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
I''m not sure if I understand your question, but I''ll give this a try

I think it is possible. Have React() call a Lua function that returns an integer, 1 for blowup() and 2 for bounce(). Then if(result==1) blowup() else bounce()

void React(){	// Call the lua function	lua_pushstring(luaVM, "lua_react_function");	lua_call(luaVM, 0, 1);	// Extract the lua function''s result	int result = lua_tonumber(-1);	// Act on the result	if (result == 1){		blowup();	}else{		bounce();	}} 


function lua_react_function ()	-- Lua code goes here to determine whether to bounce or blow up	return 1 -- to blowup	-- or	return 2 -- to bounceend 


Obviously, none of this code has been tested, but I think the ideas are right.
SpiffGQ
Advertisement
you have come very close. But suppose if react() has to check for 30 or more functions check which will make a big switch statement in c.

as in c we can use Pointers to Functions ie React(blast). Is to possible to call a C function(React)with pointer to a C function (blast)
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
Functions in lua are just another variable type.

Suppose you have this:

function blowup()
...
end


function bounce()
...
end



then you can do:

react = blowup;

react(); // calls blowup

react = bounce;

react(); // calls bounce

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

If the functions are written in C, then I can think of two options. Either write the functions as C functions and then "export" the functions to Lua so Lua code actually calls the C function. Each C function will be required to conform to a specific prototype, and each function will need Lua API code to interface with the Lua VM.

The second option (I''m not entirely sure about this one) is to pass pointers to the C functions to Lua as "user data". Then the lua function can determine which C function it wants to be called and then return the user data. The React() function will then take that user data and cast it into a function pointer and then call the function.

And then, of course, the third option is to write the functions in Lua, as Endurion suggests. Which method works best really depends heavily on your program.
SpiffGQ
quote: Original post by spiffgq
The second option (I''m not entirely sure about this one) is to pass pointers to the C functions to Lua as "user data". Then the lua function can determine which C function it wants to be called and then return the user data. The React() function will then take that user data and cast it into a function pointer and then call the function.


how to do that?
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
Advertisement
quote: Original post by DirectXXX
quote: Original post by spiffgq
The second option (I''m not entirely sure about this one) is to pass pointers to the C functions to Lua as "user data". Then the lua function can determine which C function it wants to be called and then return the user data. The React() function will then take that user data and cast it into a function pointer and then call the function.


how to do that?


According to the Lua 5.0 reference manual:
quote:
The type userdata is provided to allow arbitrary C data to be stored in Lua variables.


So, somewhere in your program (during initialization, I assume) you need to create Lua variables that store the function pointers.

Then change React() function to something like:

void React(){	// Call the lua function	lua_pushstring(luaVM, "lua_react_function");	lua_call(luaVM, 0, 1);	// Extract the lua function''s result	void* result = lua_touserdata(-1);	if (result == NULL){		return;	}	// Create a function pointer variable	int(*funcPtr)(int);		// Cast the user data to a function pointer	funcPtr = (int(*)(int))result;	// Call the function	int retval = (*funcPtr)(SOME_INT);	// Of course, this assumes that the bounce() 	// and blowup() functions have an int(*)(int) 	// prototype, so you''ll have to modify it to suit 	// your program.} 


The the Lua function will be something like this:

function lua_react_function ()	-- Lua code goes here to determine whether to bounce or blow up	return blowup_c_function_ptr	-- or	return bounce_c_function_ptrend 
SpiffGQ
thanx im working on it
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de

This topic is closed to new replies.

Advertisement