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 8571061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:42:01+00:00 2026-06-11T18:42:01+00:00

I have been working on a simple component based c++ and lua game engine.

  • 0

I have been working on a simple component based c++ and lua game engine. Currently all the subsystems like audio and physics can be called from lua with there own module using luaL_newlib.

The problem I am having is how can a lua script call functions to modify its entity. Like SetPosition() is obviously going to need to be called on the instance of an entity rather than static functions. So is there a way I can have these instanced functions.

One idea I had was each entity registering functions for itself with a module name that is unique to it. So it would look like entity1.Setposition and entity2.Setposition. Would it be possible to register, effectively duplicate functions like this?

Or is there another way to have instanced functions like this so components can modify the entity that they are a part of?

If I have explained myself badly please do say so I will gladly provide more information

  • 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-11T18:42:02+00:00Added an answer on June 11, 2026 at 6:42 pm

    Lua has syntactic sugar for “instance methods.” If you call a function with a colon instead of a dot, like this:

    entity1:SetPosition(...)
    

    then Lua rewrites it to this:

    entity1.SetPosition(entity, ...)
    

    In other words, it adds an implicit first argument which is the object that the method is being called on. On the C side, you just see an extra argument. This is the preferred way to make instance methods.


    Two other noteworthy things: the rewrite above isn’t exactly what happens. If you do something like this:

    SomeFunctionReturningEntity():SetPosition(...)
    

    it doesn’t rewrite it to

    SomeFunctionReturningEntity().SetPosition(SomeFunctionReturningEntity(), ...)
    

    It actually just calls the function once, as you’d expect.

    Also, if you’re writing an instance method from Lua itself, not from C, there’s syntactic sugar for declaring one:

    function entity1:SetPosition(...)
    end
    

    This is equivalent to:

    function entity1.SetPosition(self, ...)
    end
    

    i.e. it adds the implicit first argument to the function, and calls it self.

    (Incidentally, that’s also equivalent to this:

    entity1.SetPosition = function(self, ...)
    end
    

    Technically, even declaring a function inside of a table is syntactic sugar.)

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

Sidebar

Related Questions

I have been thinking of trying out component based architecture for game development. I
I have been working on a simple text editor in Cocoa/Objective-C for a practice
I have been working with simple linq2sql statements for a bit but am new
I am new to Silverlight and have been working with Simple MVVM to create
I have been working on a simple app to play a random sound byte
I have been working on an Android game for the past 6 months or
I have been working with the jQuery UI Tab component, and have successfully implemented
I have been working on a simple GUI and have hit a roadblock. I
What is a simple way to check for room availability? I have been working
I have been working on a (relatively) simple tcp client/server chat program for my

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.