# Scripts: The Mystery Revealed?

This topic is 4681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello! Not sure whether this topic should go here or into Scipting, but nevermind :) My question is... How do you use scripting langs such as Lua, Python etc. in your program to benefit from it? For example, in games? *Where* and *how* would I use, say, Lua and, say, my RTS game? Could anybody please give me a short example of using Lua or Python from within C# (well, I guess C++ would do too since I want to understand the technique itself)? I need all the steps and please explain a lot :) Because I have no idea on how to use these scripts. Any help would be greatly appreciated. Thanks.

##### Share on other sites
Well in general you use a scripting language to customise behaviour of objects within your game. In your RTS example you could have a script for each unit which would define it's behaviour. You could specify what it would do when you tell it attack something, specify its responses to various stimuli (i.e. what does it do if an enemy approaches, a unarmed citizen unit may run, while a tank might shoot). You could use scripts to specify how buildings work as well.

In terms of how you actually use scripts within a programming language the specifics depend upon what language you choose to embed. But normally you'll have some form of API that will allow you to tell the script interpreter to load and run script files, call functions within script files, get/set variables within script files etc. They'll also be API functions to bind function within your program so they can be used within scripts (i.e. in an RTS you could bind a WalkTo function so it can be used within a script, then when say the tank script decides to move the tank somewhere it can use the WalkTo function to do it).

You also mention C#, note that this can be used as a scripting language if you wish.

##### Share on other sites
Another use of scripting would be to create custom scenarios for your game. Think the triggers in Starcraft's editor. You could have a script that defines that after 20 minutes of holding off the enemy's attacks a fresh ship of reinforcements arrives. Another example would be custom victory requirements for a RTS. You could say that victory occurs if all the enemy is wiped out, or if all the enemy's supply depots are destroyed.

I've just recently started playing around with Lua for scripting in C++ so I'll give you an example of the steps.

First you need to think about how your game is going to use the scripting (I guess that was your first question.) Then you'd program the C++ (or C# or whatever) backend for the scripts. Next you'd create the appropriate glue code that allows your scripting language to connect with your C++ code. To do this I am using tolua++ which requires a package file that defines all the elements you wish Lua to see. Then you would have some C++ code that initializes the Lua VM and runs the script files you create.

Of course I'm just starting with scripting as well so if I'm wrong in any regard, anyone feel free to correct me.

##### Share on other sites
Eh, OK, thanks, but why not just program all that stuff in C++? Why make life harder for yourself, use API etc. etc., when I can program that the ship has to arrive in 20 minutes using C++?

##### Share on other sites
Because a scripting language will be simpler to program in than C++, you also won't need to recompile everytime you need to make a script change.

##### Share on other sites
Oh, and YOU mentioned that C# can be used as a scripting language, but how is that since my game is actually written in C#? Could you please explain more, because it sounds cool not having to learn any other language since I already know C#?

##### Share on other sites
Through some clever stuff you can do with the .Net framework you can compile C# code and then call it from your application. There's been a few threads on it see the one here.

##### Share on other sites
Quote:
 ... why not just program all that stuff in C++? ...

Because my level design team don't know C++ (actually, I am my level design team, but that's not the point)
Because I want my end users to be able to make their own maps and rules (suddenly I have a much larger team ;)
Because I don't want to recompile to change one configuration variable.

In your case, say you want to add a new unit with new behaviours to your game. You edit a bunch of sprites so it can be drawn. You edit a data file indicating how it can be constructed in the game, and who by, and how much it costs. You do everything required outside of your code, apart from the bit which indicates what it does, which is a unique, and new, algorithm. If this can be done in script also, it becomes possible to create entire new units with new abilities, even whole new AIs, without access to the source code. It's much tidier that way, and in some ways, it's actually easier to debug aswell. And your game is halfway to being modable.

Having gone to the trouble of adding scripting (I choose game monkey this time round, to see what it's like, and so far it's good) I now use it for everything I need to load. It is possible for a user to write for loops and branch statements in the config file that defines all their keys, which is probably pointless in my case, but required no effort on my part, and in other games would mean a config file could say:
if (class == warrior)    bindkey(x, jump);else    bindkey(x, duck);

or whatever.

##### Share on other sites
Ok, thanks a lot! I got it :)

##### Share on other sites
Quote:
 Original post by ZodiakEh, OK, thanks, but why not just program all that stuff in C++? Why make life harder for yourself, use API etc. etc., when I can program that the ship has to arrive in 20 minutes using C++?

Scripts are designed to make things easier. It is much easier to set up your code to recieve commands and have it react to those commands than to hardcode everything into your game. Plus, your code doesn't have to be recompiled each time you make a change. It's much more dynamic.

For example, let's say you wanted to made a program that just draws a cube and then moves that cube in a predefined path. You could feed it a series of translation calls:

glTranslate3f(1.0f, 0.0f, 0.0f); //move the cube right
glTranslate3f(0.0f, 1.0f, 0.0f); //move the cube up
glTranslate3f(-1.0f, 0.0f, 0.0f); //move the cube left
glTranslate3f(0.0f, -1.0f, 0.0f); //move the cube down

That's not unbearable, but what if you wanted the cube to make 100 different movements? That would be hideous. Now wouldn't it be better if you just did something like this:

char cmd;while(getNextCommand(cmd)){if(cmd == 'u')yPos++;if(cmd == 'd')yPos--;if(cmd == 'l')xPos--;if(cmd == 'r')xPos++;glTranslatef(xPos, yPos, 0);cube()}

Then you just create a text file with u, u, l, d, u, r, etc. You can write 1,000 movements in 2,000 keystrokes (one for the letter, one for the space/tab/return).

The idea behind scripting in games is, you write and compile your engine and you use the scripts to drive that engine. At least that's how I understand it.

Hope this helps!

##### Share on other sites
Oh, now I do get it! Thanks a lot! I do now :) !

Now another question: what is the best scripting language to use with C#? I know there's something done with both Lua and Python to make them work with .NET but it seems to me they are just 1/10000000 of normal Lua and Python... But I hadn't checked them out yet, so I wish I am wrong. But anyway: what is the best, most powerful, and easy scripting language to use with C#?

Thanks!

##### Share on other sites
Quote:
 best, most powerful, and easy scripting language

Define what you mean by best, powerful and easy and then someone can probably give you an answer[smile].

##### Share on other sites
Indeed. The terms flexible and powerful can often be used interchangably in reference to a programming language. It is often the antithesis to the term safe as well.

For example, C/C++ allows you to go beyond the bounds of an array. This:

int a[3];
cout << a[5];

would compile. It's not often you would intentionally do such a thing, but sometimes it is nice to have the option to, should you decide to do so.

A characteristic such as this is often referred to as flexible or powerful, because if you really and truly know what you're doing, it can help you out in certain instances. However it is often referred to as unsafe, dangerous, and 99% of the time buggy, because if you don't know (even if you think you do) what is stored 3 bytes beyond the end of the array, you could be calling down a world of hurt on yourself.

So when you ask for a "powerful" language, you're usually asking for one without the safeguards of languages like Java or ML. Becareful what you wish for [wink].

If you don't know all that much about programming languages (not just how to use them but how they work), I would just google around a bit and see whichever one you can find some good resources for use in C# and roll with that. Perl, python, lua, something like that. Unless of course you know which features you'll be needing, but it sounds like you're just looking for something to work with and/or learn from.

Both perl and python are pretty straight forward from what I've seen (which isn't much), so you won't really go wrong with chosing either. Python's pretty popular these days, too.

Good luck with your decision and project!

##### Share on other sites
Don't use Perl; it's counter-revolutionary. [grin]

My vote goes for Python.

Actually, setting up a scripting engine might well not be worth the effort; if you're posting in For Beginners, your project probably isn't (or shouldn't be!) big enough to justify it.

On the other hand, there's a lot (and I do mean a lot) to be said for writing the program in Python as far as you can; you can always go the other way, by writing optimized C++ bits to handle the serious number-crunching (once you've determined (a) that there is a need; (b) where you will get the most benefit) and calling them from Python. That can be a bit tricky, too, but there are some nice tools available to help you with it (such as Boost::Python).

##### Share on other sites
Quote:
 Original post by ZodiakHello!Not sure whether this topic should go here or into Scipting, but nevermind :)My question is... How do you use scripting langs such as Lua, Python etc. in your program to benefit from it? For example, in games? *Where* and *how* would I use, say, Lua and, say, my RTS game? Could anybody please give me a short example of using Lua or Python from within C# (well, I guess C++ would do too since I want to understand the technique itself)? I need all the steps and please explain a lot :) Because I have no idea on how to use these scripts. Any help would be greatly appreciated.Thanks.

Scripts are only one part of the solution.

Data driven design is the key to flexibility. Scripting combined with data driven design is ultimate power.

In an RTS, you would use scripts combined with data documents to define everything in the world. Scipts combined with a data table can be used to generate maps, units, work with an AI database, etc.

The best part is that data driven design is just simple text files!

Using this approach, you can customize EVERY aspect of your engine, from the pipeline to the user interface. And just about anyone can edit them they are just text files. Meaning while programmers are busy working on something for the art team, artists and map makers can play around and discover new effects and techniques. I would thouroghly begin by understanding how to design a data driven engine and than combining it with a script language. You don't even really need a script language at all, but they are useful for map generators, making scenarios, and other things.

G'luck

##### Share on other sites
There are still a couple of reasons to use scripting languages that nobody seems to have mentioned yet - and those are actually the ones that I myself find the most important :) So here goes...

People in this thread has mentioned benefits such as:

- My leveldesign team doesn't know C++
- I don't want to compile my engine each time I make a gameplay change
- I want other people to create content for my game

These are all very valid points - especially in a large production! But I can understand why a "lone wolf" programmer or a small team would not be completely convinced by these benefits alone. Very often, in such projects, nobody is ever going to create custom content, the main programmer *is* the leveldesigner/scripter and he knows C++ and recompiled the engine all the time anyway. So what other benefits are there? In my personal hobby projects I've found scripting very useful for (listed in order of importance for me):

- "emulating" a (controlled) multi-threaded environment
- being able to "call-by-name"
- Generally a higher level of abstraction when writing gameplay code
- GUI operations (text input and such)

I've mostly worked on a RPG engine so I'll use that genre as reference when arguing for the points above.

Many script languages allows you to write what can actually be thought of as complete little programs that seems to run totally in parallel to the main execution path (your engines game loop for instance). In my RPG engine a script can control a character, control the look and effect of spells, control stuff in the environment that moves and much more. By being able to think of each such entity as a completely seperate "program" I'm able to create very complex behavior with very simple script code. It would have been much harder to "inverleave" this game code in the main game loop of the engine. When I start up a script a behavior begins, when I shut it down it ends. And on top of that the "multi-threading" is controlled! I know that each script will actually have its turn to run after (or before) the game loop - it's not a true C++ thread that can interrupt the main thread at any time.

My RPG engine is obviously data-driven so I use an item editor to set up the various objects (swords, armor etc). Now I want to attach a special scripted behavior to some event - say when an armor is equipped. Because I can "call-by-name" I can just type in a string, the name of the script, in the item editor and the script code will be called at runtime when the event occurs. This allows for more complex behaviour to an object than, say, a +3 modifier to attack and so on (which could be defined by fixed behaviour in the engine and a slot in the item editor for the value).

The last two points are actually connected. By exposing high-level functons to the script engine you're able to express much in few lines (granted, this is also the case with well-designed and well-factured C++ code). I found this, combined with the multi-threading behaviour, to be very useful for inputting text. Getting a string input is always a pain in a real-time application because you have to update the screen each frame and cannot pause until the user has typed in all the string. Therefore you have to "poll" the keyboard once per frame and then "do something" once the user has finished typing. This often leads to annoying states and modifications of the main game loop. By using a script that's run once each frame anyway (that's how they works) I'm able to do a general string input. The keyboard can put its keypressed in a special buffer that's available to the script and the script can then determine when the string is done and what to do with it. Now string input is easy to do almost anywhere in the engine. This last point is probably very specific to both how my engine works and how I like to design things :)

Hope this gave something to think about :)

- Kasper Fauerby