View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Scripting

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

### #1Strife  Members

Posted 16 October 2000 - 01:59 PM

Does anyone know of a site or book that describes in fair detail how to go about creating and implementing a scripting language? It doesn''t have to be all that complex, I''d just like some basic knowledge (I''d like to be able to make an AI scripting language for a future game I''m planning). Also, I''ve looked at the recent article here, but it''s for VB programmers . It would be nice if any links had examples in C/C++, but if it at least explains everything well enough, that''s not a neccessity. If you code it, they will come... Commander M (a.k.a. Crazy Yank) http://commanderm.8m.com cmndrm@commanderm.8m.com

### #2Anonymous Poster_Anonymous Poster_*  Guests

Posted 16 October 2000 - 02:34 PM

Well, in theory, you''d want to create the "language", which is a bunch of commands like "walk", "jump", "spawn", etc., or whatever commands you''ll have. Each word is associated with a different action. When you write the script, you should parse the lines and convert it into integer tags as opposed to the string tags seen above (easier to work with integers than with strings). Once its in integer form, simply do some logic testing for what it wanted. For example, lets say "spawn" is 10 and "book" is 21. If this is the script:
spawn book

You read that string, and convert it into integers:
10 21

The logic (or lexical, i think, or is it symatic? I dont know the technical terms for the different stages! ) stage looks like this:

  if(sentence[0] == 10) // We know we need to spawn something{ switch(sentence[1]) { case .....: case.......: case 21: { SpawnItem(ITEM_BOOK, x, y); } break; default: {.....} }}

You get the picture. If you have any questions, ill try to answer them.

### #3Zipster  Members

Posted 16 October 2000 - 02:39 PM

Also, to store the string/integer relationships, youd have a structure similar to this:
#define NUM_WORDS 2 // const int NUM_WORDS = 2, for you ''C++''erstypedef struct language_tag{     int id;     char word[20];} language;language LANGUAGE[NUM_WORDS] = {         {10, "spawn"},         {21, "book"},                                };

BTW, that was me above. IE doesn''t like to save my login for my posts!

### #4iwasbiggs  Members

Posted 16 October 2000 - 03:29 PM

for a basic scripting engine you might not need to buy a book.

how basic of a scripting are you thinking?
well, assembly style is about as basic as I can think...
you basically have one command per line and optional parameters after the command, which are separated by commas or spaces.

if you are able to code a program that can read
moveplayer 1 2
then you have a basic scripting engine. Once you think you have a good grasp on such a basic scripting engine, writing a more powerful one suddenly becomes within reach, as you will see.

if you would like psuedo code or have questions, feel free.

### #5MK42  Members

Posted 16 October 2000 - 09:09 PM

Hi!

On www.flipcode.com you''ll find a nice tutorial on creating a scripting language. It covers everything from lexical analysis (looking at the characters in the script), parsing/syntax checking (is the script correct), code generation, virtual machines, etc... really good stuff. Even comes with source code. The author implements a basic STRING language as an example (concatenate and print strings)

He uses some lex (lexical analyzer) and yacc (parser generator) for the tutorial. Those might be a bit complicated for some people, but become important when designing more complex languages, so it''s something you should face (and not run away from).

Hope this helps,

MK42

### #6Taulin  Members

Posted 17 October 2000 - 02:02 AM

One thing I see missing from most, if not all, scripting
articles is the implementation. I see people recomending
such scripting languages such as JScript almost everyday.
If you use a pre-done language such as this, it looks
like its vm works with your engine through call-backs.
I have yet to try this, but am also wondering if anyone has?
The main question is, if you have some VM, how does it interact
Or am I just heading down the wrong path?

Thanks!

### #7Philomath  Members

Posted 17 October 2000 - 04:14 AM

I am a firm believer in not recreating the wheel. If you want scripting in your game just grab a readily available scripting language and plop it in there. I don''t have time to be creating a custom language when I can use one someone else has already spent years of development on. The choice of scripting languages can be a bit tricky though. I go for small footprint, very fast, easy implementation, mature and track record in the game industry. I know of other people who have been very sucessful with other scripting languages but I prefer lua. I can drop it into an existing engine within a few hours and start scritping. Scripts can be precompiled to bytecode for that extra speed advantage in the final version. It is trivial to expose lua functions to C and C functions to lua. It has been used for many years. It has been used for many sucessful commercial games (Baldurs Gate, MDK2, Grim Fandango). It is also very extensable... By no means is lua the end-all-be-all of scripting languages but it works, its fast and its easy to use and implement. Thats good enough for me.

http://www.tecgraf.puc-rio.br/lua/

### #8Taulin  Members

Posted 17 October 2000 - 04:58 AM

I looked at the site real quick, once again it looks like you have to register your functions with Lua for it to call them.
So if I had some class like
class Hey
{
public:
void run();
.
.
void jump();
};

And I had some script like
run 5 times, then jump.

How would Lua work along side this class and script?

Thanks!

### #9Philomath  Members

Posted 17 October 2000 - 08:05 AM

This totaly depends on how you set up you implementation.

Lua's environment is persistant so while you are loading up your level you would register whatever functions you wanted to use while the game is running. Then when a certain objects AI update came around you would run its script using those functions. I would not create a unique function for each object as objects can be created and destroyed. I would create generic functions which would access the base behaviour common to that unit type. The script for each individual units could be kept with the unit so each could behave uniquely.

Thus,
Register functions we are going to use to interface with our objects.
Also register functions used for evaluating objects environment.

Game Loop:
Update Object (Hey):
Hey sets itself as current ai object with ai interface and passes its script to lua.
Possible script
  { local enemy local boss if (enemy = EnemyNear() ) and IAmAwake() then if (IAmHurt() OR EnemyStrength(enemy) > MyStrength() * 1.5) and (IAmNotDrunk() OR IAmEnraged) then RunAway() IAmScared() return else if EnemyWithinRange(enemy) then Attack() else RunTowards(enemy) end return end end if (boss = BossIsNear()) and IAmAwake() then if BossIsMad(boss) then Grovel() return end Patrol() return else if Drunk() > 0.50 and Intelect < 6 then Sleep() else if Intelect < Rand(10) then Drink() return end Patrol() end return end if BossIsNear() and not IAmAwake() then if Luck() > Rand(20) then WakeUp() Patrol() end return end } function Patrol() if (curpt = AtWayPoint()) then if curpt == NumWayPoints then MoveTo(WayPoints[1].x, WayPoints[1].y) return else curpt = curpt + 1 MoveTo(WayPoints[curpt].x, WayPoints[curpt].y) return else if HaveMoveToCommand() then return else curpt = ClosestWayPoint() MoveTo(WayPoints[curpt].x, WayPoints[curpt].y) return end end end

Philo

Edited by - Philomath on October 17, 2000 3:10:23 PM

### #10Strife  Members

Posted 17 October 2000 - 08:20 AM

Thanks for the replies. Zip, I''ll test something like that out as that looks to be about what I wanted (something simple, but not too extremely low-level).

I might even check out that flipcode article more in-depth than I have (I saw some of it last night).

I''ll also probably check out Lua, as I''ve been meaning to look into it anyway (especially for use with ClanLib -- go CL!!! ).

If you code it, they will come...

Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
cmndrm@commanderm.8m.com

### #11Taulin  Members

Posted 17 October 2000 - 08:48 AM

Cool, thanks for the reply, but one more question.

So would you recommend a wrapper class that would
hold the Lua instance, and its functions would be mapped
into Lua. Sort of like the LuaWrapper acts like a V-table?

class LuaWrapper{ Lua* pL (or however it is done ) ICurrentThing* pT (An object being ''ran'') LuaWrapper() { // Set up Lua and map LuaWrapper''s functions, // isDrunk and Jump, into it. } void RunScript( ICurrentThing* pO ) { pT = PO; // RUn the script found in pO into Lua } void isDrunk( ... ) { ... pT->IsDrunk(); } void Jump( ... ) { ,,, pT->Jump() }}

### #12MK42  Members

Posted 17 October 2000 - 09:19 AM

Taulin:

I think with the registered functions Philomath means stuff like EnemyNear() and EnemyStrength(enemy). Those are the hooks back into your engine.

Philomath:

I''ve looked at Lua before for scripting in a programming game (people write AI scripts and they can compete against each other). The project never got beyond the planning stage, but that''s another issue. Nevertheless, I found the Lua documentation to be a bit lacking in terms of ''example code''. I''ve also been looking at stuff like SeeR, Small, EiC and even Python (which was quite amazing in some ways). None of the scripting languages I mentioned above have been able to provide a rather seamless integration of Script-C/C++ and C/C++-Script. Take Lua for example, you still have to write wrappers which push/pop stuff from the stack. Although this in itself is fairly trivial, it''s still a pain in the ass.

OK, I might be a bit lazy here , but I''ve been researching cooler ways for almost seamless integration. For a native DLL, which were to be used by the script, you wouldn''t have to register a thing (just use the exports and unmangle the names). Of course, calling scripted-functions from C/C++ is another issue. But, how about letting the script-compiler generate a C/C++ file which does all the wrapping for you ... that would be a great help and save development time! I know, that some of these features are used in current scripting languages, but I''m still waiting for *the* scripting language ... if I have some spare time, I might have to do my own (I''ve done a C-Parser before).

Please don''t get me wrong about Lua ... it''s really fast and cool, but sometimes I''m just wondering why it''s so hard working with these things...

MK42

### #13Philomath  Members

Posted 17 October 2000 - 09:25 AM

Thats the way I''ve used it. I''m sure that there are other ways of setting it up. In fact as you can have multiple lua "environments" you could have a seperate lua environment for each object with only object specific data and functions available. I have never tried that but it would work as well (unless you hade too many objects or create them on the fly -vs- existing in a pool from level loadup). It really is up to personal preference and the task at hand. I use a seperate lua environment for user interface elements (buttons, pulldown boxes etc...)with its own unique set of C bound functions and data. I also setup and control my particle emmiters with lua scripts (usually precompiled, and no the script does not get called every frame). I figure the more I can accomplish with scripts the more generic and reuasable I can make the engines and tools.

Philo.

### #14Taulin  Members

Posted 17 October 2000 - 09:30 AM

Thanks for the replies. The scripting part
was never a problem. I just never read or saw any
examples of actual code ( pref. some class ) using
a scripting engine or VM. Have you all looked into
the JScript vm? I read on Gamasutra that I think
it was the Vampire:Masq used it.

Thanks!

### #15Philomath  Members

Posted 17 October 2000 - 10:09 AM

MK42:
EnemyNear() and EnemyStrength(enemy) are hooks to the engine, anything that takes extensive calculation (like pathfinding and line of sight stuff) is not suitable for any scriping language(Yea, I know EnemyStrength is just a lookup but hey its an example). I agree with you about the lua documentation, it does not have many examples. However the language itself is simple enough not to need to many (16 odd reserved words for lua3.2). Unfortuantly the implementation side of it could use many more examples and there explanation of tables is horrible (and they are so easy to use.. go figure).

I don't pretend lua is the best language by any strech of the imigination, every language has its pros and cons. Any of the languages you listed could be used effectively. I use lua because of its ease of implementation (one call to lua_open(), register your 'C' functions, and remember to call lua_close() when you are done, 20 min, depending on how many functions you need to register), its speed, I have yet to see a scripting language beat it in a comparison, and its extensability.

As far as registering you functions it can be a bit of work if you are adding it to an existing project. However Tolua is supposed to do this for you from header files (I've never used it though).

VBA with its com interfacing is close to what I think you are asking for, but I won't go there

Philo

Edited by - Philomath on October 17, 2000 5:24:35 PM

### #16MK42  Members

Posted 17 October 2000 - 10:26 PM

Philomath:

I didn''t mean to bash lua ... it''s just that these ''limitations'' don''t make the scripting solution easier to work with. I guess tolua just looks at the PE Image of the LIB file and undecorates the mangled names ...

You are right, though, that lua is still one of the simplest scripting languages to use ... but, wouldn''t it be cool to have a scripting solution, which you could just drop into your project (no matter how complex it is) and it would work ...

VBA is *not* an option ... I refuse to use it

MK42

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.