Jump to content

Development with c++ and Lua on Linux


pracedru
 Share

Recommended Posts

Here is how i made it possible to call C++ from LUA.

 

I want to build Multiplayer game in Leadwerks. But i couldn't find networking libraries and threading libraries that would make it usable in Lua. So i build a Networking client in C++ with libcurl and libpthread . But i want to still use the Lua scripting on the prefabs since a lot of work is already done for me here. And also programming in Higher level languages is just faster.

 

I decided to use the libtolua++ libraries, since this is already build into Leadwerks.

 

I work on Ubuntu 15.04. Here the tolua++ libs are installed with:

 

 

sudo apt-get install libtolua++5.1-dev

 

The tolua++ program needs a pck file which shall contain the c++ like seudo code that is translated into lua classes.

 

My package:

 

$#include "shot.h"

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

 

I have made two functions. One to emit shots detected on the Entities from the Lua scripting. Another to get the current health status from C++ side.

 

My actual ShotListener.h class looks like this:

 

#pragma once
#include "App.h"
#include "Leadwerks.h"
using namespace Leadwerks;
class ShotListener {
private:
public:

ShotListener(void) {
}											
~ShotListener(void){}						
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;
}
};

 

As you can see i changed the default App class into a Singleton so that i can get the App object from the instances of ShotListener that is used from Lua.

Having placed these two files in the same folder i ran the following function from the terminal:

 

tolua++5.1 -o tolua_shot.cpp -H tolua_shot.h -n Shot shot.pkg

 

This generated two files tolua_shot.cpp and tolua_shot.h that i included in my C++ project along with the shot.h file.

 

This way i can do a

 

self.shotListener = ShotListener:new()

 

In the Script:Start() function in mu Lua objects

 

and a

 

self.shotListener:shot(self.entity, self.hits)

 

When Hurt is called in the Lua object.

 

After this i just had to sort out all the Segmentation error bugs because of multithreading.

 

I forgot to mention that the ShotListener Lua also needs to be initialized in the main.cpp before app->Start() with the following:

 

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

tolua_ShotListener_open(Interpreter::L);

Edited by pracedru
  • Upvote 4
Link to comment
Share on other sites

I really need to look more into this in the future, Is it possible you could write a few blog entries walking through how to make a C++ class fully communicable with other lua scripts? That would be cool, as I'm not following you when you said:

 

As you can see i changed the default App class into a Singleton so that i can get the App object from the instances of ShotListener that is used from Lua.
  • Upvote 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

A Singleton is a class which will only have one and only one instance in your project. The Singleton can be accessed from anywhere.Here is how to make a Singleton class:

 

class MySingleton
{
private:
 MySingleton() {} // Prevent users from making instances

public:
static MySingleton& instance() // returns the one and only instance
{
	 static MySingleton instance;
	 return instance;
}

void SayHello()
{
	 System:Print( "Hello");
}
};

 

Now you can use it anywhere in any project file like this

 

MySingleton::instance().SayHello();

  • Upvote 1

Roland Strålberg
Website: https://rstralberg.com

Link to comment
Share on other sites

I didn't show how I actually made the App class into a singleton, however Roland's example is exactly right. This is only to do c++ calls from lua. I haven't actually figured out how to call lua scripts from c++ yet. I suppose I can figure that out by looking at the implementation in the default code in a leadwerks project.

  • Upvote 1
Link to comment
Share on other sites

Oh, so THAT's how you do it. If I knew how to do that, I would not pass classes along as pointers in LEX. (Gotta remember, only C++ coding I did before was for Source.) If I ever make a LEX2, I'm using this for classes. Thanks!

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

I find it simpler to just do this:

class MySingleton
{
public:
   static void SayHello()
   {
        System:Print( "Hello");
   }
};

 

Now you can use it anywhere in any project file like this

MySingleton::SayHello();

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Yes. That's another way to achieve same effect.

 

However, a singleton gives you control over when the class is instantiated. A static class is less controllable and is instantiated before main is called.

 

The sequence in which classes is instantiated can sometimes be critical when the singleton depends on some other class or resource which is not available before main is called.

Roland Strålberg
Website: https://rstralberg.com

Link to comment
Share on other sites

  • 3 years later...

Hello; I am trying to figure out how to use the following in a Windows based environment;
and I think I have gotten down to the code I want activated in/thru the c++
as:
.......................
class MySingleton
{
public:
   static void SetAchiv1()
   {
        set_steam_achievement ("FINISHED_AREA_1");
   }
};
.................
= when I use in Lua the below
..................
MySingleton::SetAchiv1();
..................
I would (example) activate set_steam_achievement ("FINISHED_AREA_1")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
However I am a bit confused in how to set up the C++ bit as it seems to involve several files.
However as this is a Linux based discussion im wondering if someone could give me a pointer toward how to find information on 
making the same (but in Windows) as in the first input above by pracedru
..............................

 

"I work on Ubuntu 15.04. Here the tolua++ libs are installed with:

 

 

 

sudo apt-get install libtolua++5.1-dev
 

 

 

The tolua++ program needs a pck file which shall contain the c++ like seudo code that is translated into lua classes."

 

 

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...