Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:53:23+00:00 2026-06-10T00:53:23+00:00

For example, let’s say I have a keyhandling interface, defined in C++ as :

  • 0

For example, let’s say I have a keyhandling interface, defined in C++ as :

class KeyBoardHandler 
{ 
public:
    virtual onKeyPressed(const KeyEventArgs& e);
    virtual onKeyReleased(const KeyEventArgs& e);
}

Now, I want to extend this to Lua, to allow Lua to take advantage and register a KeyboardHandler in script.

Here’s the prototype so far.

class ScriptKeyboardHandler : public KeyboardHandler
{
public:
    ... previous methods omitted
    static void createFromScript(lua_State* L);
    bool createCppData();

private:
    ScriptKeyBoardHandler(lua_State* L);

    int mSelf;
    int mKeyPressFunc;
    int mKeyReleaseFunc;
    lua_State* mpLuaState;
}

Now, I know the implementation will go something like this:

ScriptKeyboardHandler::ScriptKeyboardHandler(lua_State* L) :
    mpState(L)
{ }

ScriptKeyboardHandler::onKeyPressed(...) { 
     // check if mKeyPressFunc is a function
     // call it, passing in mself, and the event args as params
} 

// On Key Release omitted because being similar to the key pressed

ScriptKeyboardHandler::createFromScript(lua_State* L)
{
    auto scriptKeyboardHandler = new ScriptKeyboardHandler(L);
    if (scriptKeyboardHandler->createCppData())
    {
        // set the light user data and return a reference to ourself (mSelf)
    }
}

ScriptKeyboardHandler::createCppData() 
{
    // get the constructor data (second param) and find the keyPressed and keyReleased function, store those for later usage
    // any other data within the constructor data will apply to object
}

-- Syntax of the lua code
MyScriptHandler = { }
MyScriptHandler.__index = MyScriptHandler

MyScriptHandler.onKeyPress = function(self, args)
end

handler = createScriptHandler(handler, MyScriptHandler)
registerKeyHandler(handler)

I just don’t know how to find the functions when they are passed in as arguments within a table.

Am I doing this right? I hope I am, it’s been a pain the ass because tolua doesn’t support virtual classes easily, not ones you can derive from in script anyway.

I’m not worried about the other functions, just how do I find these variables (the key press function, etc, from my C code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:53:25+00:00Added an answer on June 10, 2026 at 12:53 am

    Here’s roughly what my implementation of onKeyPressed would look like.

    void ScriptKeyboardHandler::onKeyPressed()
    {
       //Get the table corresponding to this object from the C registry
       lua_pushlightuserdata(mpLuaState, this);
       lua_gettable(mpLuaState,LUA_REGISTRYINDEX);
    
       //Now get the function to call from the object
       lua_pushstring(mpLuaState,"onKeyPress");
       lua_gettable(mpLuaState,-2);
    
       //Now call the function 
       lua_pushvalue(mpLuaState, -2 ); // Duplicate the self table as first argument
       //TODO: Add the other arguments
       lua_pcall(mpLuaState, 1, 0, 0 ); // TODO: You'll need some error checking here and change the 1 to represent the number of args.
    
       lua_pop(mpLuaState,1); //Clean up the stack
    
    }
    

    However you’ll also need to update the constructor to store the lua object representing the handler into the registry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example let's say we have a class called Secretary and another class called
A simple example: Let's say I have one alias being sourced somewhere as: alias
Please indulge my simplistic example: Let's say I have 1 million pages on my
Maybe a silly question but here goes anyway. Example: Let's say I have one
I'm struggling with the F# type signature notation. For example let's say you have
Really simple example: Let's say I have this table: ID Name GUID1 John GUID2
For example let's say I have an app that uses $_SESSION['user_id'] . Now if
Should you always create unique keys whenever possible? For example let's say I have
I'm having some trouble writing a query. For example: Let's say I have just
I have 1 or more items in an array, for this example let's say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.