A new kind of scripting

Blitz3D Forums/Blitz3D Userlibs/A new kind of scripting

Yasha(Posted 2010) [#1]
Tired of painfully-slow scripting languages directly interpreted in Blitz3D? Can't be bothered to write yet another custom VM for your game's unique language?

Tiny C Compiler is the answer!

TCC is a lightning-fast C compiler by Fabrice Bellard, compatible with C89 and most of C99, capable of compiling C to machine code directly in your program's memory, and running it! The compiler is fast enough that there's pretty much no noticeable delay, and the result is moddable game logic running directly on the CPU, rather than being slowed down by interpretation.

Click here to download the B3D wrapper.

Four annotated examples are included. To be called from Blitz3D, C functions must return an int and accept a bank (or type object) - as in the examples, it may be convenient to actually write a C wrapper for functions that don't fit this pattern. The bank can be any size (that's your business), and is supposed to hold parameters or pointers to other data structures. Also note that the DLL needs to be accompanied by the "include" and (I think) "lib" folders in order to be able to supply standard C include files and features.

Function pointers obtained via MikhailV's FastPointer will work correctly from C, as long as they've got the right type signature (at least, simple tests indicated this works). You can either pass them in as parameters, or use the AddSymbol function to make them globally visible in your C code - either way, B3D functions can easily be called from the script.

Finally, if you don't like the C programming language, don't despair - as well as a language in its own right, C is a popular compiler target for higher-level languages (hmmm, have we been here before?). Writing a compiler from your custom scripting language to C is much simpler than a full script compiler and interpreter, and will still let you benefit from TCC's direct execution on the CPU.

Aside from scripting, there are any number of other non-game-related uses for this library. For example, the magic of C pointers makes B3D call stack inspection easy.


TCC is originally licenced under the LGPL, which essentially means you can use and distribute it freely without having to open-source your project.

(Before using this, do consider the downsides of directly-executed scripts: where an interpreted scripting engine can recover gracefully from script crashes, end users can completely crash the game by writing bad C code, or even come up with something malicious as you no longer have full control over how they mod your program... keep these drawbacks in mind!)

Last edited 2010


Charrua(Posted 2010) [#2]
as always, very interesting!
thank's

Juan


Blitzplotter(Posted 2010) [#3]
@Yasha, very interesting post, I'm looking forward to trying this later this week.