Blog > Blog > We have Scripting

We have Scripting

Posted by Hamcha on August 18, 2011


Sooner or later when you're developing a videogame you start to feel the need to have something you can use to change the behavior of your engine without having to recompile everything. The simplest solution seems to be Config Files, but they're quite limited.

This is why Scripting languages like LUA were created, they are interpreted or compiled into byte-code and executed under a Virtual machine and while they seem to be totally detached from the game itself, scripts can actually call internal functions and vice versa.

What follows is my experience with setting up a scripting language for my current project.

...


So, if you actually want to use a scripting language, you have probably seen how much LUA is used, and thought "Well, why not?".

I am not going to talk "Why don't use LUA" because, in the end, I am using it too!

The main problem I had with LUA was the fact that it has a C API but cannot be used to bind C++ Classes and Methods to LUA.

You have three options here:

  1. Use a binding lib (or whatever its name is) that works using templates and faking OOP on LUA using meta-tables - OR - use an automatic binding creator that creates intermediary C ANSI files to bind C++ Classes in LUA.
     
  2. Create your own glue code between C++ Classes and LUA.

  3. Find another scripting language with C++ APIs.

 

So, if you want to follow Point 1 like I did, you need to decide what lib to use.

The most common one is luabind, though it could be the ideal solution for everything, it depends on Boost, and that's REALLY not OK for me, but if it works for you, then go use it.

I tried different libs and I am actually using SLB (Simple Lua Binder).
It works, and it doesn't depend on anything.

SLB it's actually the best compromise I have found between Complexity and Ease of use.

It is REALLY easy to use and it has just what I need.

Now, just for par condicio, let's talk about what I think about OTHER scripting languages.

I have seen other Scripting languages with C++ APIs such as Angelscript or Squirrel, but every one of those has some kind of problem within.
Take in mind that I am not saying that those are bad languages, I am just saying that I preferred LUA over them for a reason, and I am trying to explain why in the next paragraph. 

So, Angelscript seemed ideal to me because someone said it is really easy to integrate it with C++ stuff. It is not.
It has a C++ like syntax, which can be either good or bad, I am actually more comfortable with the LUA syntax because AT LEAST for scripting I'd like a simple syntax I can make my friend/teammate understand (which always does his best to help me and I never get to thank him so if you're reading, and you are, because one of your assignments is to spellcheck this article, thank you very much for everything you're doing).

The other choice is Squirrel, which is natively OOP, cool huh? Well guess what, it is slow and manages to have a Certainly-Not-Simple syntax.
There are people who use it, some Valve titles like Portal 2 /L4D 2 actually use Squirrel for scripting.
The comparison on the Squirrel website says Squirrel is faster than LUA,
Benchmarks say the opposite, so I'm sticking with LUA.
Even though it would have been really cool to name files .nut 

Also, a thing that really matters for me is how big is the library.

Lua actually is one of the best here, since it was designed to be small, even if there's still something that beats Lua, it still gets Angelscript and Squirrel out of the list.

So, in the end, if you want a clean solution, get yourself LUA 5.1 + SLB and you will be fine.


EXTRA:

When I said SIMPLE SYNTAX, I mean that adding/editing an unit to the game is as SIMPLEST as it can get, like that:


-- UNIT_NAME_HERE

NAME = "Unit display name here"
UNIT_TYPE = 0
ATK_TYPE = 2
ID = 6

HP = 70

ATK = 20
DEF = 10
DEX = 10
AGI = 5

UnitStat.addUnit(ID, UNIT_TYPE, ATK_TYPE, NAME, ATK, DEF, AGI, DEX, HP)

-- END UNIT_NAME_HERE

Yep, that's actual code.

Grammar Nazis are welcome

English is not my native language and I'm looking forward to improve so if you happen to find some mistakes please point them out!

Comments:

Susy

In awe of that aneswr! Really cool!
Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.