Archived

This topic is now archived and is closed to further replies.

is it possible with lua

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

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.

Share this post


Link to post
Share on other sites
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 bounce
end


Obviously, none of this code has been tested, but I think the ideas are right.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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_ptr
end

Share this post


Link to post
Share on other sites