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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:01:04+00:00 2026-06-05T13:01:04+00:00

I have a game with a mainloop – on each loop i call for

  • 0

I have a game with a mainloop – on each loop i call for every NPC in the game ->ProcessAI() to execute any actions.
This is a server so the call to ProcessAI is not executed on every frame like on a client game! Its also singlethreaded.

Now i wanted to extend the C++ codebase with lua using luabind (maybe, even with boost overhead). So i expose some functions of my NPC class to LUA.

I wanted to create actor scripts for example – boss battles which have more sophisticated behaviour – whenever in my c++ ProcessAI function an event happens – i would delegate this to the corresponding lua script for the specific NPC.

i imagined in my boss.lua script i would have something like this

 function OnEngageCombat(NPC)
 NPC:say ("Some taunts...")
 ScheduleEvent(CastEvilSpell,/*time*/2000,/*numExecutions*/1,)
 end

 function CastEvilSpell(NPC)
 NPC:CastSpell("someSpell")
 end

However – i have no idea how to do this – i gather ScheduleEvent should be some C++ function exported to Lua – but what would be the best approach to keep the object reference of the NPC (boss) with this and call a function in that script about 2 seconds later ?

Furthmore along with this delayed execution – i want that NPCs can interact with each other – my current idea is to have an actor behavior script for each special NPC.

Now what i imagined is to initiate a conversation between two NPCs e.g.

function DoGossip(NPC)
// check if NPC1 is close to NPC2
if NPC:DistanceToNpc("SomeGuy") < 10 then
 StartConversation1()
end

function StartConversation1(NPC)
NPC:Say("Hello ...")
// wait a moment now trigger NPC2 to reply
????
end

Basically – how do i call a function from lua scriptA which exists in lua scriptB which is the behavior script for NPC2.
What would be a good design?

Thanks

  • 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-05T13:01:05+00:00Added an answer on June 5, 2026 at 1:01 pm

    If you use LuaBind, you can set a global scriptable object that refers to a wrapper class around your game engine that you expose to your script.

    So, whenever you set up your lua engine, you can register a global you want all scripts to have access to, like this in your C++ code:

    luabind::globals(m_pLuaState)["Game"] = m_pGameWrapperClass;
    

    Then, in your script, you could simply refer to the Game object to get at game specific functionality, like this:

    Game:ScheduleEvent(...)
    

    Your C++ game wrapper class would just implement a ScheduleEvent function that you’d bind with LuaBind. You could add/register as many game-y specific functions as you’d need on that object.

    To lead into your question 2, you could simply register a function on your Game class called FindNPC(), that takes a string argument or id or something that would look up an npc in your engine, and return a reference to it. You’d probably want to write a wrapper class around the npc object that exposes npc-y functionality just like you would do with your Game class, and FindNPC() would return userdata that represents an NPC, and exposes whatever functionality that is necessary to use it, or to make it do stuff in the Game itself.

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

Sidebar

Related Questions

I have this in my code in the main loop(2D game in a window):
I'm developing a game. I want to have game entities each have their own
I have this game where balloons are coming from bottom and player has to
I have a game server than can take requests from a user. A user
i have this game like doodlejump i inserted some monsters and bullets on the
I'm working on a Windows game, and I have this: bool game_cont; LRESULT WINAPI
I have a game in RoR. The game breaks down like this. There are
I have this game controller pad and I need to read it's commands for
Client + Server are both written in Javascript. I don't have a master game
I'm currently making a C++ game. I have my main loop during which the

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.