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!
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?