Advertisement

Call local Lua function from C?

Started by April 19, 2011 09:30 PM
5 comments, last by Downsider 13 years, 7 months ago
I want users to be able to add new weapons/NPCs into my game through scripts. Basically, there's several entry points into these weapons and scripts that are called by C.

For instance:


local function onWeaponFired()
--do stuff
end


should be called when the player hits the attack button. However, I can't figure out a way to call a local function from C, and I want global memory to be shared across each Lua weapon.. Can anybody rig up a way to call local functions in Lua from C?
Here you can find the Lualib code to call a function. You can ignore the SWIG part if you just need to call it without parameters.
[size="2"]I like the Walrus best.
Advertisement
You will have to forgive owl for he does not know of what he speaks :)

Whilst what you are trying to achieve is not impossible it is unconventional. I suspect the reason you want the functions to be local is due to the restrictions of functions in the global namespace being unique or otherwise they overwrite the previous entry in the table, yet you want to look for predefined function names for the actions. I would ask you to think again about your approach and instead supply a function which the user can register the callbacks themselves, either individually or via supplying a table which has functions using these predefined names. For example
local Browning =
{
onWeaponFire = function()
--do stuff
end
,onWeaponReload = function()
--do stuff
end}

register_weapon('Browning 9mm', Browning )


If you really wanted to carry on with your plan then it may depend on how these scripts will be loaded. Would they be loaded from C or Lua? If C would you have control over the loading or would the user load them?
smh rolleyes.gif
[size="2"]I like the Walrus best.

You will have to forgive owl for he does not know of what he speaks :)

Whilst what you are trying to achieve is not impossible it is unconventional. I suspect the reason you want the functions to be local is due to the restrictions of functions in the global namespace being unique or otherwise they overwrite the previous entry in the table, yet you want to look for predefined function names for the actions. I would ask you to think again about your approach and instead supply a function which the user can register the callbacks themselves, either individually or via supplying a table which has functions using these predefined names. For example
local Browning =
{
onWeaponFire = function()
--do stuff
end
,onWeaponReload = function()
--do stuff
end}

register_weapon('Browning 9mm', Browning )


If you really wanted to carry on with your plan then it may depend on how these scripts will be loaded. Would they be loaded from C or Lua? If C would you have control over the loading or would the user load them?


I considered something like this, but I also thought about how I would do entities in the world.

I have several entry points, for example, here's an entity that's a block that moves toward the right once every five seconds:

local function onCreated()
setTimer(1);
end

local function onTimeOut()
this.x = this.x + 1;
setTimer(1);
end


This is really the way I want to structure it. Each entity would be self-contained, but have access to the same global variables as every other script in the game. I do that by opening a new lua_state by calling lua_newthread. Right now everything works, but only if there's one of each item or if I give them a unique name/identifier, due to the fact that I have to call globals..

In other words, in order to get what I really, really want, I'd have to be able to call local functions, OR have a way to easily share variables of any type between lua states.. The first is preferable! laugh.gif



EDIT:
To elaborate, here's how I have everything structured.

When the game loads, it goes through all the NPCs in the level and loads their scripts. Each one has the same script. The example above is an example of an entity that simply moves toward the right. These entities share the same lua_state so they can easily communicate globals between one another, created using lua_newthread off of the main state. I need these globals to be shared, yet I also need to be able to call those functions properly from C.

I would be willing to do something like this, as you described:


local weapon =
{
onCreated = function()
--blah
end
,onWeaponFired = function()
--blah
end
}

return weapon;


By returning that object, surely I'd now have access to those functions? But how can I store and access that table from C?
By returning that object, surely I'd now have access to those functions? But how can I store and access that table from C?[/quote]
Personally I would not return the table but that is another matter.

There are numerous options for storing tables, the normal locations are the Global table and the Registry.
You could store your own table in Global into which the user then inserts there own, store the table directly in Global, store the table in the registry using the name provided by the user, use a Registry reference ...

One thing that does confuse me from your example code is the use of 'this' in your functions, in Lua this is normally referred to as 'self' yet you are using functions which are not part of a table?

Edit: GameDev stole my lines breaks :(
Advertisement

By returning that object, surely I'd now have access to those functions? But how can I store and access that table from C?

Personally I would not return the table but that is another matter.

There are numerous options for storing tables, the normal locations are the Global table and the Registry.
You could store your own table in Global into which the user then inserts there own, store the table directly in Global, store the table in the registry using the name provided by the user, use a Registry reference ...

One thing that does confuse me from your example code is the use of 'this' in your functions, in Lua this is normally referred to as 'self' yet you are using functions which are not part of a table?

Edit: GameDev stole my lines breaks :(
[/quote]

This seems like the better approach. And "this" is passed to the script. But! You did say that local functions were possible to call, just unconventional. Can you show me how to use them?

EDIT:

To elaborate a bit, I'm trying to mimic how another, very obscure, closed-source scripting language works. I like it's structure in a lot of ways and that's why I want to call local functions in this manner. I did, however, get your method working, and I can live with it. laugh.gif But the other way is preferable if it's not a completely ass-backwards method..

This topic is closed to new replies.

Advertisement