2

Currently I have something in lua that is like OOP using tables.

TCharacterController = {}
TCharacterController.speed = 10.0
TCharacterController.axis = "x"

function TCharacterController:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    return o
end

function TCharacterController:update()
    --this is a function that is called by the C application
end

The concept is that I will create a child object

ScriptObj = TCharacterController:new()

for each script instance attached to an object in my application (this is for a game). So I have an entity layer and all of the entities will have the ability to have a ScriptObj attached to them. My idea is that the Script is actually a class and it is instantiated for each entity it is attached too.

My question is, how do I instantiate the instance of the TCharacterController using the C API?

1 Answer 1

3

Since new is using the self reference syntatic sugar, you need to pass self as the first arg, the rest is just a function call of a table lookup:

lua_getglobal(L, "TCharacterController"); /* get the table */
lua_getfield(L, -1, "new");  /* get the function from the table */
lua_insert(L, -2); /* move new up a position so self is the first arg */
lua_pcall(L, 1, 1); /* call it, the returned table is left on the stack */
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.