Jump to content
  • entries
    10
  • comments
    31
  • views
    7,486

LUA CPP bindings "how to" tutorial 1/7


Charrua

3,497 views

 Share

Thnigs i will cover:

 

1) How to get objects created on the editor from Cpp

2) How to send objects created by Cpp code to LUA

3) How to call an entity's script LUA function easely (without parameters)

4) How to call an entity's script LUA function passing parameters

5) How to receive returned parameters from LUA functions (yes one or more)

6) How to expose a Cpp Class to LUA using tolua++

 

 

I will split this post acordingly to the items listed above for easy referencing/reading

 

Here a complete Leadwerks app: cpp source, map and scripts of a working example of the all above.

//no longer valid!

https://dl.dropboxusercontent.com/u/78894295/leadwerks/cppLuaBindings.zip

//2021/12/14 update

https://drive.google.com/file/d/1JdewNls8joRJygZ5zkNnuHrnCqNqDl_S/view?usp=sharing

(see description of what executable does and how to operate it on a note at the end of this post)

 

I'm not English native, so apologies if i do linguistic mistakes, and i'm not a so experienced cpp programmer (ok i'm not a programmer, I'm a technician in electronics, so for any inconsistency or semantic error/mistake pm me and i'll edit/modify the post, thank's)

 

 

This site has many topics about this subject but for someone like me many of them has almost all, but normally there are some or too much details which are not exposed. Normally it takes a while to get to the point:

 

do

try, pray, try again, reread

until ok OR tooLate

 

My intention is to give "allincluded" for new Leadwerks cpp users which is asumed that if they are

cpp programmers normally are advanced ones, but being new to Leadwerks normally the learning

curve is shorten with material like i'll expose.

 

Here some interesting links on the subject (not all of them)

 

http://www.leadwerks.com/werkspace/topic/11007-calling-lua-script-function-from-c-with-arguments/page__hl__callfunction

http://www.leadwerks.com/werkspace/topic/7548-access-cpp-global-via-lua/page__hl__tolua#entry60531

http://www.leadwerks.com/werkspace/topic/8802-tolua-exposure-problems/

http://www.leadwerks.com/werkspace/topic/7866-lua-version-in-30/

http://www.leadwerks.com/werkspace/blog/138/entry-1288-pushing-complex-lua-tables-from-c/

 

there are many references to:

http://www.leadwerks.com/werkspace/files/file/216-tolua/

but this just display: sorry we couldn't find that!

 

so, here is the tolua++ source

https://github.com/LuaDist/toluapp

 

documentation

https://www8.cs.umu.se/kurser/TDBD12/VT04/lab/lua/tolua%2B%2B.html

 

precompiled

http://www.codereactor.net/tolua-windows-precompiled-binary-exe-file-download-and-more-instructions-on-tolua/

 

(note, the link addressed by autors to download:

tolua++ can be downloaded from the sites below:

http://www.codenix.com/~tolua/ is broken! )

 

How hard is to get all the links up to date! isn't it?

 

 

Items coming soon...

 

jio

 

---------------------------------------------------------------

Notes about the executable:

 

In the terminal window, each Lua function and cpp method throws some info, so

move the main window a little to the right, so you can see the terminal window.

 

You'll se something like that, without my comments in italic:

 

app.lua start //things that happens LUA side App.lua Start

c2luaTest not nil //instance of object form c created and not nil

lua is calling me! //calling an object's method from lua OK

3.3561e+008 //calling average: (10+35+15)/3 :(WRONG! see bellow)

 

start scanning.... //this is cpp side on App.cpp, App::Start

//list of objets from the map (world)

Directional Light 4

EditorCamera

findMe //objet searched

found entity findMe !!! // find OK, set a pointer to it for future use.

Directional Light 4

 

EditorCamera //now searching only for cameras

found editor camera //found editor camera, got camera from editor!

 

now, put manin window on focus.

Press a, s, d, f

 

a: calls average(10, 15, 25) and print the results on the terminal window

s: calls average( 5, 10, 4) and print the results on the terminal window

d: calls turnMe(vec3) with a default value of: turn.x=10; turn.y = 20; turn.z = 30;

if ok, you see the red cube turn this amount per axis on each keyhit.

f: calls noParams() and print "executing noParams"

 

As you press each letter, new messages appear on the terminal window confirming functions of lua

correctly called from c++ code.

 

if you press a, s, d, f in this sequence, you should see

 

 

entity has script

average defined

executing average

16.6667

entity has script

average defined

executing average

6.33333

entity has script

turnMe defined

calling me from C

executin noParams

 

 

WRONG display in calling average cpp method from LUA, guess what? a bug.

 

in Start function inside App.lua i do:

 

 

c2luaTest.intA = 10

c2luaTest.intA = 35

c2luaTest.intA = 15

System:Print(c2luaTest:average())

 

so I only initialize intA, leaving intB and intC with garbage, so, expect to see any number

on each run!

Please edit the App.lua for me!

Edited by Charrua
dropbox link is no longer valid

  • Upvote 6
 Share

4 Comments


Recommended Comments

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