Advertisement

Scripting a monster (How?)

Started by August 21, 2003 03:03 AM
6 comments, last by VanKurt 21 years, 3 months ago
I would like to add some intelligence to my monsters. Until now I hardcoded their behaviour using zillions of states and lots of ifs and switches. So the code was enormous, every time I changed something it had to be recompiled and they still were STUPID. Because of this I’d like to go for a different, more professional approach: real scripting. I imagine that to be like this: I have a txt file which is used as the script. In this file I can access certain variables and give certain commands. Every script-file has to have certain functions, which will then be called by my engine. Here’s an example: Function HasBeenHurt( CVector3 Origin, DMGTYPE damageType ) { RotateTo( Origin ); If( damageType == PLAYER_SHOT ) AttackPlayer(); } Well, I know that code is stupid, but its just an example ;-) But now comes the part I’m having problems with: How can I implant such a thing? I would have to go through the whole text file every frame, look for keywords (expensive string-compares), replace variables by the real values and and and…. That’s much to much work and would definitely slow down things. Could someone tell me how to do that the RIGHT way? Maybe some kind of script-compilation? Or other techniques? Thanks a lot!!!
Check out the Articles & Resources link at the top of this page. You might also want to check out such things as Sweet Snippets and the featured articles... I seem to recall that there was a series on writing a scripting engine recently.

Timkin

[edited by - Timkin on August 21, 2003 4:20:22 AM]
Advertisement
You can also use one of the existing scripting engines/languages, like Lua or Python.
I''m using Lua in a game engine I''m making and it works really nice. The example you gave would be coded in Lua and run when the event "enemy was shot" occurs. Lua is also quite fast, has an easy to learn syntax and is really small (the interpreter DLL is 35kb and the "liblua" DLL which contains optional functions is 25kb - compared to Python where the interpreter DLL is around 500kb + libs).
You could even make your enemy AI (and possibly other things) user-modifiable to enable them to make mods.
Simkin http://www.simkin.co.uk/ is another scripting language that can be used.

Reality factory http://www.realityfactory.ca/html/ (a free game engine, source available)uses simkin very effectivly. They already have lots of scripts for monsters, flying creatures, racing cars.

People that use reality factory modify these scripts or write their own.

Good luck
Scripting won''t make your AI better. It is still just code. Scripting might make it easier to do updates for your customers but it is still just code. Just the fact you are now gonna use a scripting engine won''t make your AI smarter.

If you wish to allow less-skilled programmers to contribute, scripting can help, and as mentioned above, is nice for sending small updates instead of the entire recompiled executable.

Another option you can try is to use DLL''s for scripting. Put the AI code inside a DLL and then have your main program access the AI code within them. You can gain the same advantages of ease of updates and allows you to technically use C++ to do your scripts. I have had good success doing just that.

Thanks! That were lots of good hints and resources... :-)
Advertisement
to make an enemy smart " " they need to know what they are doing. AI attempts to do that, it doesnt matter what you use
for AI (script, fuzzy logic,etc) so long as the AI is coded
in a manner that makes the enemy behave intelligently. So first off, make a version of you''re game and play it with people instead of monsters and record what things you do and why. Use you''re game logs to make AI Pseudo-Code then decide how best to make the pseudo-code into functional game code.
In your main program just have a liner viewer.

Scan though the first line see whats in it.

eg if currentMonster closeTo monster2
{
attack monster2
}

your program would read the if so it knows theres a decision statement.

then the currentMonster to know to look at the currentMonster in the array, and monster2 so it knows to look at monsters and number 2 in the array.

the {}
show the start and end of an action.

like you can have stored functions for commands like attack, or closeTo which checks the distance between the two monsters, and would return a true of false etc

This topic is closed to new replies.

Advertisement