In article <35b5205
...@news.amaesd.k12.mi.us>, jmanc
...@voyager.net
says...
> I'm very interested in setting up a small scripting language for controlling
> some of my characters in my game. The problem is that I'm not really sure
> where to start.
My advice... save yourself a lot of pain and use Lua. Lua is a small,
powerful, efficient scripting language. It was written specifically as
an extension language and it's very good. I haven't seen any language (
including Python ) which is a better choice for embedding.
For embedding in general, you need to answer some basic questions:
How do I get data from the embedded language into C?
How do I get data from C into the embedded language?
How do I call the embedded language from C?
How do I call C from the embedded language?
Following is some sample code which answers all of the above.
#include <lua.h>
// this function creates a new function in Lua, calls the
// function with 2 numbers from C, and gets the result from
// Lua back into C. It answers the first 3 questions:
void foo( void )
{
lua_beginblock();
// create a new function in Lua. quoted stuff is Lua code
lua_dostring( "function add_nums( x, y ) return x + y end" );
// push 2 numbers onto the lua call stack, call the Lua func.
lua_pushnumber( 5 );
lua_pushnumber( 9 );
lua_callfunction( lua_getglobal( "add_nums" ) );
// get the result back from Lua ( result == 14 )
int result = lua_getnumber( lua_lua2C( 1 ) );
lua_endblock();
}
// To answer the 4th question, here's code which extends Lua
// with a new C function. In this case, I've added the ability
// to call the WinAPI MessageBox from Lua.
void DoMessageBox( void )
{
char * message = lua_getstring( lua_lua2C( 1 ) );
MessageBox( ( LPCSTR ) message, ( LPCSTR ) NULL, ( WORD ) MB_OK );
}
// then, to add the "MessageBox" function to lua, you'd call:
lua_register( "MessageBox", & DoMessageBox );
// and later, in Lua, you could call MessageBox with:
MessageBox( "some message" )
Lua is a truly wonderful language - clean, small, efficient, and free
for commercial use. It's very solid code ( current version 3.1 ), used
in many industrial applications. It's also well supported.
One of the things I like best about it is that I can't think of any way
to improve it. If I had decided to write a scripting language for
myself, I doubt mine would be as good. For the record, the original Lua
code base was generated with flex/bison or lex/yacc, and evolved from
there. So, if you're planning on going the flex/bison route, save
yourself 3 years of refinement and use Lua.
Regards,
ashley
PS: Here's the official blurb on Lua:
-----------------------------
* What is Lua?
------------
Lua is a programming language originally designed for extending
applications,
but also frequently used as a general-purpose, stand-alone language.
Lua combines simple procedural syntax (similar to Pascal) with powerful
data description constructs based on associative arrays and extensible
semantics. Lua is dynamically typed, interpreted from bytecodes, and
has
automatic memory management, making it ideal for configuration,
scripting,
and rapid prototyping.
Lua is implemented as a small library of C functions, written in ANSI
C,
and compiles unmodified in all known platforms. The implementation
goals
are simplicity, efficiency, portability, and low embedding cost.
Lua has been awarded the first prize (technological category) in the
Second
Compaq Award for Research and Development in Computer Science. This
award
is a joint venture of Compaq Computer in Brazil, the Brazilian Ministry
of
Science and Technology, and the Brazilian Academy of Sciences.
* New in version 3.1
------------------
+ NEW FEATURE: anonymous functions with closures (via "upvalues").
+ new syntax:
- local variables in chunks.
- better scope control with DO block END.
- constructors can now be also written: { record-part; list-part }.
- more general syntax for function calls and lvalues, e.g.:
f(x).y=1
o:f(x,y):g(z)
f"string" is sugar for f("string")
+ strings may now contain arbitrary binary data (e.g., embedded zeros).
+ major code re-organization and clean-up; reduced module
interdependecies.
+ no arbitrary limits on the total number of constants and globals.
+ support for multiple global contexts.
+ better syntax error messages.
+ new traversal functions "foreach" and "foreachvar".
and more...
* Availability
------------
Lua is freely available for both academic and commercial purposes and
can be downloaded from the sites below. The current version is 3.1.
Home page: http://www.tecgraf.puc-rio.br/lua/
http://csg.uwaterloo.ca/~lhf/lua/
In Brazil: ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz
In Canada: ftp://csg.uwaterloo.ca/pub/lhf/lua/lua.tar.gz
In Germany: ftp://ftp.uni-trier.de/pub/languages/lua/lua.tar.gz
In Greece: ftp://ftp.ntua.gr/pub/lang/lua/lua.tar.gz
* Contacting the authors
-----------------------
Lua has been developed by TeCGraf, the Computer Graphics Technology
Group
of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro in
Brazil).
TeCGraf is a laboratory of the Department of Computer Science.
Dozens of industrial products developed by TeCGraf use Lua.
Send your comments, bug reports and anything else to l...@tecgraf.puc-
rio.br.
For reporting bugs, try also the mailing list: lu...@tecgraf.puc-rio.br