Jump to content

Communicating from Lua to C++


AggrorJorn
 Share

Recommended Posts

You can make a Lua script that exposes a string property. This value for all your objects you could use json or whatever to format the data you need. In the script set the entity key value to this data. Then in C++ you can probably find a pretty lightweight json parser and get the entity data.

 

I guess I'm not sure when the scripts Start() is called in relation to parsing the map in C++.

Link to comment
Share on other sites

  • 2 years later...

Since i didn't have a solution to do a server-client data layer in Lua i decided to build one in C++. This is mainly due to being more familiar with multi threading in C++, And this kind of stuff needs to be in a separate thread from the 3D rendering thread, in my opinion.

But i still wanted to use LUA for prefab scripting and all the other neat stuff Leadwerks.

 

Here is what i did to make this work:

 

I installed tolua++ on my development machine with

 

sudo apt-get install libtolua++5.1-dev

 

I build a class named ShotListener.h

 

#pragma once
#include "App.h"
#include "Leadwerks.h"
using namespace Leadwerks;
class ShotListener { //tolua_export
private:
public:
//tolua_begin
   ShotListener(void) {
   }											    // constructor 1
   ~ShotListener(void){}											 // destructor
   void shot(Object* oEntity, float damage) {
    App *app = App::getInstance();
    Entity * entity = (Entity*)oEntity;
    app->entityShot(entity, damage);
    return;
   }
   float getHealth(Object* oEntity) {
    App *app = App::getInstance();
    Entity * entity = (Entity*)oEntity;
    float value = app->getEntityHealth(entity);
    return value;
   }
};

 

I Made the App class into a Singleton, so that any Lua script can instantiate the ShotListener and call functions from the App.

I made a tolua++ ShotListener.PCK file to match the ShotListener class:

 

$#include "shot.h"

class ShotListener {
ShotListener();
~ShotListener();
void shot(Object* oEntity, float damage);
float getHealth(Object* oEntity);
};

 

In the terminal i used the following command to make the ShotListener.PCK into actual C++ code:

 

tolua++5.1 -o tolua_shot.cpp -H tolua_shot.h -n ShotListener shotlistener.pkg

 

This created two files tolua_shot.cpp and tolua_shot.h.

 

These i included in my project along with the ShotListener.h file.

 

In the main.cpp i added the following before the app->Start()

 

if (Interpreter::L==NULL) Interpreter::Reset();
tolua_ShotListener_open(Interpreter::L);

 

Hi then had a ton of debugging to avoid segmentation error bugs related to multithreading. But in the end it works.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...