PDA

View Full Version : Delphi and LUA



jdarling
24-02-2006, 02:49 PM
Ok, I've got to the point in my PGD entry that I need to implement my scripting support. At first I was going to use my home rolled scripting engine, but decided that I just don't have time to debug and get it ready for a production level environment.

So now I am down to trying out different scripting engines. I don't want suggestions on scripting platforms (I know about Pascal Script, DWS, Python, etc...). What I do need is a good tutorial on how to embed LUA into a Delphi Application. I have found quite a few headers for LUA, but can't find a single sample on how to make it work with Delphi :(. Anyone have any sample source.

The route I'm taking is to mod the engine to work with each of 5 scripting engines. I'll then compare the overhead, and other properties to decide what is best. LUA is the last in the list, and the only one that doesn't seem to have any examples :(. Since its used in quite a few games, and touted as the scripting engine to use, I decided it needed a spot on the list so I can check it out.

Any links or tutorials greatly appriciated.

savage
24-02-2006, 03:13 PM
I have not used Lua, but have you looked at this - http://www.noeska.com/dlua/downloads.aspx ?

Which scripting engines did you look at and why did you discount them? Did you try JavaScript? Just look for some insight into your decision etc as scripting is something I have always wanted to implement but never ever go round to doing it.

Legolas
24-02-2006, 05:21 PM
I have not used Lua, but have you looked at this - http://www.noeska.com/dlua/downloads.aspx ?

Indeed very interesting example! I'm wondering if lua is capable to compile scripts and then reuse them :?:

jdarling
24-02-2006, 05:53 PM
I have not used Lua, but have you looked at this - http://www.noeska.com/dlua/downloads.aspx ?

Which scripting engines did you look at and why did you discount them? Did you try JavaScript? Just look for some insight into your decision etc as scripting is something I have always wanted to implement but never ever go round to doing it.

Actually thats the one I'm testing with for LUA. The problem is that there are no samples with it :(.

On the topic of Scripting Engines, here is my findings thus far:
DWS: Very powerful, and overall quite nice. Too slow for games that are heavily scripted :(
Pascal Script for Delphi: Faster then DWS, cleaner and easier to use. Not as many features, and my top choice for Pascal Scripting in Games.
Python: Speed is better then either of the above. Quite fast, some what easy to implement, but I just don't really like it :).
Monkey Script: Too Cish for me
Java Script: Too Cish and slower then anything else, also requires alot of overhead
MS Scripting Engine: Slow piece of $h1T, but easy to add languages too
LUA: Don't know, haven't found any examples of how to use the D@MND thing, but it looks promisiong as its been used alot. I like the no objects thing and that its simple to pickup (hey I can make stuff work with the command line version, just not sure how to embed it yet).
SAL: This is my own scripting engine. It rips the pants off of everything above on speed. Prolly because it compiles and executes as native code (completely). Problem with it is that its basically assembly, plans are to add languages that cross compile to SAL and then compile down. Won't be ready for the compo, and not sure when it will ever be ready :).

Thats what I have thus far. As I said I've worked ALOT with scripting engines over the years, and I've only touched on a few above. I've even written a few for custom applications, but time is against me :(. I origionally thought of using PS for the project, keeping in tune with the Pascal nature, but in the end I want others to be able to mod the game easily. Thus I'm looking into Python or LUA. If I can't get LUA to work, then Python it will be :(, even though I'm not really fond of its syntax.

jdarling
24-02-2006, 05:54 PM
I have not used Lua, but have you looked at this - http://www.noeska.com/dlua/downloads.aspx ?

Indeed very interesting example! I'm wondering if lua is capable to compile scripts and then reuse them :?:

Yes, LUA can compile and re-use scripts. In fact there is a command line compiler (LUAC) that ships with the full distro. There are also lots of articles about it (all towards C/C++) so I guess thats my next step is to read them and see if I can get it up and running in the environment to test it out.

Nitrogen
24-02-2006, 06:01 PM
I've also been looking into using LUA for scripting Castle Adventure,
I've found a delphi header on the net and got a simple script editor and executor to work...
It takes a string, compiles it into a function then calls the function, prints the result out to Delphi.

But there are some things that I'm still not sure on, such as how to get Delphi to call a different function from the last compiled one...
I looked at the example in the reference manual and they were doing very weird stuff with the LUA stack :?: and they basically said Dont do it this way, look at other examples that do it better...

I've made my LUA editor avaliable on my website for you here:
http://www.sulaco.co.za/nitrogen/LUA.zip

AthenaOfDelphi
24-02-2006, 07:24 PM
Hi Jeremy,



DWS: Very powerful, and overall quite nice. Too slow for games that are heavily scripted :(

DWS II is my engine of choice. Its what I used for our web server platform and its what I'm using for our compy entry, so I find this quite interesting. I did some speed tests, comparing DWS II against compiled code and found that there was hardly any slow down, but I'd be very interested to know what kind of tests you did and if you have some example code thats slow.

jdarling
24-02-2006, 08:41 PM
The tests I did were about a year ago, and unfortunately I don't have them around any more (few system crashes and restores since then). Basically what I did was to setup a standard sprite animation using a normal paintbox. I then wrote the same code for Delphi, DWS II, and PS (basically a copy and paste job, as they all are close enough to the same). Used the default importers associated with PS and DWS II to create wrapper units and executed everything using a profiler (don't remember witch one). Comparison of time to call, # of times called, and etc was taken into account and DWS II's implementations (at that time) came up slower then Native or PS.

For the round I'm doing now, I have a simple interface that explains what a scripting engine should look like to my application. I can then create an object that implements that interface and comparasons are done with Eric G's sampleing profiler as well as recording system usage information.

For example with native code I can attain about 120 fps in software rendering (GDI) mode. I can get about 95 fps using PS, Python came in at 98 (not much difference, but had a higher load), MS Scripting Engine came in at a whopping 40 fps (all sarcasym intended), SAL 115 fps (but again too much complexity to use it right now).

This was all done with nothing more then having the scripting engine monitor the keyboard and then move the character appropriately. I also had 100 staged sprites moving on and off screen. Of course none of this will be in the final engine. It just wouldn't make sense to have litterly everything scripted, but I do plan on using the scripting engine for a majority of the work.

The other thing that bothers me about DWS II is the lack of support and updates over the past few years. I have to admit that when it first came out, I used it quite a bit. About a year ago I moved on, and have no plans on looking back unless something in DWS really jumps out at me.

In the end I'm at a split decision. In keeping the compo in mind I would use a pascal derivetive, but as I said I want people to mod it when I'm done. So another language is a must. More gammers are used to Python or LUA then pascal.

Andreaz
16-03-2006, 06:20 AM
There's a quite extensive delphi lua library with debugger and alot of samples at http://kuma.webj.net . In japaneese or something however but still usefull.

jdarling
16-03-2006, 04:14 PM
The project I'm currently using for reference is: http://luaforge.net/projects/luaedit/
Its done completely in Delphi and works quite well. I'll have to take a look at the one you suggested though when I get a bit of time :).