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

  • Home
  • SEARCH
  • 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 6009619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:59:48+00:00 2026-05-23T01:59:48+00:00

I am working on a game engine in C++ using Lua to define NPCs.

  • 0

I am working on a game engine in C++ using Lua to define NPCs.

I can define a prototypic NPC like this:

orc =
{
    name = "Generic Orc",
    health = 100
}

function orc:onIdle()
    print("Orc idles...")
end

and then spawn an instance of “Orc” with entitySpawn(orc). This is a C++ function that reads values like health and name from the given table, creates an Entity object in C++ with the given values and in addition creates a Lua table for the specific NPC.

Now, I would like to have a direct connection between the orc.health variable in Lua and the mHealth member variable of the corresponding Entity object in C++, so I could assign a value in Lua and instantly use it in C++ and vice versa.

Is this even possible? Or do I have to make use of setter / getter functions? I have taken a look at light userdata and got to the point of storing a pointer to the C++ variable in Lua, but could not assign a value.

  • 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-05-23T01:59:49+00:00Added an answer on May 23, 2026 at 1:59 am

    This is possible. I’m assuming that entitySpawn returns the table that C++ created for the entity. I’ll also assume that you can expose a function from C++ that takes an entity’s table and returns the current health, and similarly for setting. (You can use a light userdata pointer to the C++ object as a member of this table to implement this.)

    So the ugly way would look like this:

    local myOrc = entitySpawn(orc)
    local curHealth = getEntityHealth(myOrc)
    setEntityHealth(myOrc, curHealth + 10)
    

    To make this prettier, we can have some fun with metatables. First, we’ll put the accessors for all the properties we care about.

    entityGetters = {
        health = getEntityHealth,
        -- ...
    }
    entitySetters = {
        health = setEntityHealth,
        -- ...
    }
    

    Then we’ll create a metatable that uses these functions to handle property assignments.

    entityMetatable = {}
    
    function entityMetatable:__index(key)
        local getter = entityGetters[key]
        if getter then
            return getter(self)
        else
            return nil
        end
    end
    
    function entityMetable:__newindex(key, value)
        local setter = entitySetters[key]
        if setter then
            return setter(self, value)
        end
    end
    

    Now you need to make entitySpawn assign the metatable. You would do this with the lua_setmetatable function.

    int entitySpawn(lua_State* L)
    {
        // ...
        // assuming that the returned table is on top of the stack
        lua_getglobal(L, "entityMetatable");
        lua_setmetatable(L, -2);
        return 1;
    }
    

    Now you can write it the nice way:

    local myOrc = entitySpawn(orc)
    myOrc.health = myOrc.health + 10
    

    Note that this requires that the table that is returned by entitySpawn does not have a health property set. If it does, then the metatable will never be consulted for that property.

    You can create the entityGetters, entitySetters, and entityMetatable tables, as well the __index and __newindex metamethods, in C++ instead of Lua if that feels cleaner to you, but the general idea is the same.

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

Sidebar

Related Questions

I'm working on a game engine in C++ using Lua for NPC behaviour. I
Alright so here is my issue. I'm working a game engine that will eventually
I'm working on a simple 2D game engine in Java, and having no trouble
I'm working on a game (C#) that uses a Robocode-like programming model: participants inherit
I'm working on a game (using Ruby) and planning to have it available in
I'm working on an iPhone game that takes place on the ocean surface. Can
I'm working on a fairly big open source RTS game engine ( Spring ).
I am working on game engine prototype and have the following question: Right now
I'm working on a game engine and I'm too much of a wuss to
I've been working on a game engine for awhile. I've started out with 2D

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.