Jump to content

A Little C++ that sings the base-chorus



After have been testing a bit in a more 'live'-development scene I found things that needed more attention (what else .. ). These things could maybe have been done in LUA but as I'm a C++ developer since long I moved over some basics to C++ and left some in LUA. During this operation I found some I could make some further improvements. Now my Code schematics looks like this.




The most important here is the GameItem class that all other items that exist in the Scene inherits. The GameItem handles the connection to the LUA-script, can save & load its data in a SQL database and also can send messages to registered message clients as the Player class which needs to be informed about various events.


I have as seen in the diagram following types of items.



This can be either health or energy



This item will react when a trigger is applied to it (example. door is the actor, key is the trigger)



Can be picked and will end up in screen list of trigger items which then can be used to 'trig' Actor events.



Can be picked and will end up in screen list of tools. (example. a flashlight or a hammer).



An invisible area in the scene that will cause some kind of event when entered. Current the types are

'Checkpoint, FPS On, FPS Off and Die'



This is a simple object that can be carried by the player. It remember it position at last checkpoint


The Items described above are all childs of the GameItem class and has a LUA script part. Any LUA scripted item a been set to operate as one of the above item types using a quite simple system.


function Script:Start()
 self.entity:SetKeyValue( "itemtype", "tool" )
 self.entity:SetKeyValue( "class", "Tool" )


This will cause the C++ framework to create a Tool instance connected to the Lua-scripted item. You can still use the LUA script as usual but have the support of GameItems database. Here is an example of a Area item which will force a first person view.



Script.areatype = "fps-on"
Script.visited = false

function Script:Start()
-- C++ GameItem support
self.entity:SetKeyValue("itemtype", "area") -- this is an Area
self.entity:SetKeyValue("class", "Area") -- Create C++ the GameItem derived class Area

-- Make us invisible
local material = Material:Create()



C++ Header

#pragma once
#include "icy.h"

class Area :
public icy::GameItem,
public icy::ClientServer<AreaClient>


enum AreaType

Area(Leadwerks::Entity* e);

//  --- GameItem
virtual void init(icy::LuaBridge& lb); // called when LUA-script is connected
virtual void create_table(icy::DB& db); // called when database table should be initialized
virtual void save(icy::DB& db); // called when database save should be done
virtual void load(icy::DB& db); // called when database load should be done

 //  --- LUA ACCESS
void set_visited(bool visited);
bool is_visited() const;

const AreaType& get_areatype() const;
const std::string& get_message() const;

AreaType _areatype;
bool _visited;
std::string _message;


C++ Source

#include "area.h"
using namespace Leadwerks;
using namespace icy;

Area::Area(Entity* e)
:   GameItem(e)

void Area::init(LuaBridge& lb)
_areatype = str_to_areatype(lb.get_string("areatype"));
_visited = lb.get_bool("visited");
_message = lb.get_string("message");

void Area::create_table(icy::DB& db)
_dbtable = get_suggested_tablename(); // auto-named table = "area"
_dbcols.push_back(DB::Column("name", &_name)); // bind table column "name" to the _name member
_dbcols.push_back(DB::Column("areatype", reinterpret_cast<int*>(&_areatype)));
_dbcols.push_back(DB::Column("visited", &_visited));
db.create(_dbtable, _dbcols); // will create table if needed

void Area::save(DB& db)
db.write(_dbtable, _dbcols);

void Area::load(DB& db)
db.read(_dbtable, _dbcols);

void Area::set_visited(bool visited)
_visited = visited;

bool Area::is_visited() const
return _visited;

const Area::AreaType& Area::get_areatype() const
return _areatype;

const std::string& Area::get_message() const
return _message;


To at least have something that's not code I have started on modeling one of the first items in the first scene "A Field Full Of Secrets". Its a pier that you need to walk on .. can be tricky. I use Blender. Haven't textured it yet. Hmmm.. Maybe I could call the model "Bridge Over Trouble Water" smile.png



1 Comment

Recommended Comments

Very clean and well documented setup Roland. Keep it up!


Also that bridge looks awesome, even without shaders.

Share this comment

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Add a comment...

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

  • Create New...