• Advertisement
Sign in to follow this  

Scripts: The Mystery Revealed?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Zodiak
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++?


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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement