Jump to content

ToLua++ Implementation


Josh
 Share

Recommended Posts

I'm confused about how ToLua++ works. So it wants a "cleaned" header file. Is there some executable that parses this and outputs C++ binding code?

 

I am not sure what I am supposed to compile. Do I just include all source files, or is this the source to the aforementioned glue code generation tool?

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

ToLua++ is a preprocessor, which takes your original C++ headers and converts them to Lua binding headers. There should be some external program to run and start the preprocessor included.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Yes Josh. ToLua++ is a program that parses 'cleaned' header files and creates Lua-code from them.

Does sound scary. It's a bit more work but not much. The cleaned files often just needs some very small corrections

if any at all. One nice thing is that you can add extra things is those files to solve some things that is hard to do

in a type LuaBind solution. The price to get more control is just the fact that you have to have a 'cleaned' header copy

of the C++ header file.

 

It's now a long time since I used toLua++, but either Lua or toLua++ seems to have changed to much since then.

My son and I was writing a 3D Engine and like you studied various script languages and came to the solution that

Lua was the way to go. It was fast, it was used as industry standard by many companys, it was stable.

 

Then we tried LuaBind and came to the conclusion that it did not work as we intended when it came to C++.

I now have forgotten the details, but it produced unnecessary code among other small things. There was something

about the stack usages also which I can't remember right now. Anyway, after having struggling with LuaBind for a while

we decided to have a look at toLua++ and was very satisfied with the result. It produced faster and simpler code, and also

gave the opportunity to solve many things that was tricky with LuaBind.

 

I don't have any samples now. That project died 4 years ago when my son got his own kids and did not have the time for

the project any more. But our experience was that although LuaBind was very simple to use and produced what it was designed for,

it did not quite got there when used for game code and C++. Of course this is just my personal view of it but thats was we

came up to.

 

I could have a look at produce a simple sample if you need that. Feel free to contact me in such case.

AV MX Linux

Link to comment
Share on other sites

I know Josh wont drop Lua anytime soon, but for those who are interrested in some alternatives, I encourage you to take a look at the Squirrel scripting.

 

It's based on the Lua stack code but has a jscript/c/c++ syntax ( personaly I'm more at home with it ) and support native classes and has reference counting ( you can choose to not use a GC ).

 

Binding to C++ seems easier albeit I've only used it with C.

 

People using Squirrel ( including its author of course ) claim it's better suited than Lua for large and heavy code bases ( more efficient in this case ).

 

Squirrel Language

 

Albeit this language is not very well known it is stable and supported and is also used in commercial applications and games.

 

I invite you to take a look at it.

 

Guy.

guy rabiller | radfac founder / ceo

Windows 7 / 64bits | Intel i7x980@3.33GHz | 12 GB RAM | NVidia GTX 480

Link to comment
Share on other sites

I don't know about all those yet another basic languages. I think there needs to be some real evolution in computer languages, and I have thought that it wouldn't be too difficult to make a human english language for computers. It doesn't even need a parser, since it is very simple, because we already think like we program in C++. You just say what you want, and the language does it.

 

For example, instead of typing some archaic code to define a class, you would just say:

Make an object which is like a wheel, but it has additionally also a valve and a rubber contact surface.

The language would then already know where the valve is located usually, and actually make it like the user wanted. So the language needs a huge knowledge database to actually "understand" what the user says. A pure one-way language with no background knowledge would not work.

 

Most expert systems in the past have failed because they try to use logic to understand humans. But humans don't work with logic, but with a huge knowledge database from which reverse search results might look like logic sometimes, but they are not (unless you count the reverse word+context weight as a logic). In very rare situations where humans really use logic is when they are geniuses or got an inspiration from who knows what spirits. In 99.99% of cases humans just copy/paste what they been spammed into their brains.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

That does look interesting Guy ... I'm taking a look as I've not decided what scripting language to implement yet in my Leadwerks based engine. Thanks for the recommendation.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Here's what the console displays when the -h switch is used:

tolua++.
exe -h

usage: tolua++ [options] input_file

Command line options are:
 -v       : print version information.
 -o  file : set output file; default is stdout.
 -H  file : create include file.
 -n  name : set package name; default is input file root name.
 -p       : parse only.
 -P       : parse and print structure information (for debug).
 -S       : disable support for c++ strings.
 -1       : substract 1 to operator[] index (for compatibility with tolua5).
 -L  file : run lua file (with dofile()) before doing anything.
 -D       : disable automatic exporting of destructors for classes that have
            constructors (for compatibility with tolua5)
 -W       : disable warnings for unsupported features (for compatibility
            with tolua5)
 -C       : disable cleanup of included lua code (for easier debugging)
 -E  value[=value] : add extra values to the luastate
 -t       : export a list of types asociates with the C++ typeid name
 -q       : don't print warnings to the console
 -h       : print this message.
Should the input file be omitted, stdin is assumed;
in that case, the package name must be explicitly set.

pause
Press any key to continue . . .

So I try putting this in a .bat file:

tolua++.exe -h
pause

And this is displayed:

tolua++.
exe -H "entity.h"

** tolua internal error: [string "tolua embedded: src/bin/lua/doit.lua"]:34: #no
package name nor input file provided.


stack traceback:
       [string "tolua: embedded Lua code 23"]:6: in main chunk

pause
Press any key to continue . . .

It looks like the input file is not being read?

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

You might actually want to keep the output filename the same, and only put them into a different folder, so that you don't have to change any source code, but can control it with compiler include paths.

tolua++ -H tolua/entity.h entity.h

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Actually...

-The output file is a .cpp file.

-The input file HAS to use the .pkg file extension. Don't know why, I guess it made sense to the programmer. When I use an .h file, an output file is generated, but it doesn't contain any classes or constants from the header file.

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

That does look interesting Guy ... I'm taking a look as I've not decided what scripting language to implement yet in my Leadwerks based engine. Thanks for the recommendation.

 

I've used it for a few years and I can testify that it's quite nice to work with. It's considerably easier to bind C++ classes and functions to Squirrel counterparts when using sqbind. It also has a syntax perhaps a little more compatible with game engines (as they tend to be OOP in nature). Lua was never a particularly good fit for that, it was just extremely easy to embed.

There are three types of people in this world. People who make things happen. People who watch things happen. People who ask, "What happened?"

Let's make things happen.

Link to comment
Share on other sites

Does ToLua++ support std::vectors and lists, so I can do stuff like this?:

 

C++:

class Entity

{

std::list<Entity*> kids

};

 

Lua:

local child

for child in entity.kids do

--do stuff

end

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

That solution is just like using GetChild() and CountChildren() methods, which isn't nearly as cool. It also means you will never have stuff like this:

 

a = surface.vertex[4].position.x

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

It's actually better, since in LE2 the way how BlitzMax can access the members directly leads to feature differences between languages, and to a mess when converting code from BlitzMax to other languages.

All languages should have access to the same engine features.

 

You can still do this Lua, which is actually easier to remember than those wierd "in" statements:

for local i = 0 to CountChildren()-1
    local e = GetChild(i)
end

And the member access gets a lot cleaner too:

local a = surface:GetVertex(4):GetPosition():GetX()

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

I have it working, but ToLua's namespace system is really obnoxious. :huh:

 

By default, the command namespace is included, so all functions are like this in the Lua state:

le3.LoadModel()

 

Why is everything I didn't write so badly designed?

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

It's actually better, since in LE2 the way how BlitzMax can access the members directly leads to feature differences between languages, and to a mess when converting code from BlitzMax to other languages.

All languages should have access to the same engine features.

There I go, breaking my own design parameters. You're probably right about that.

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

Haha, ToLua works! The code below will print out "No Name, Joe, Joe", as it should. :huh:

 

Thanks Mika for investigating this months ago, but the issue with Luabind is so obscure I can't blame you for not finding that. Its documentation and presentation really does appear better, but I think ToLua wins.

	for i=0,self:CountChildren()-1 do
	child = self:GetChild(i)
	if child.name==nil then
		Print("No Name")
		child.name="Joe"
		Print(child.name)
	else
		Print(child.name)
	end
end

for i=0,self:CountChildren()-1 do
	child = self:GetChild(i)
	if child.name==nil then
		print("No Name")
	else
		Print(child.name)
	end
end

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

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