Scripting

Started by
14 comments, last by Strife 23 years, 6 months ago
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
Advertisement
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.
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!
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.
___________________________Freeware development:ruinedsoft.com
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
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
back to your basic models or entities in your own engine(OOP)?
Or am I just heading down the wrong path?

Thanks!
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/
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!
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,
Load level:
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
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

This topic is closed to new replies.

Advertisement