Qbasicnews.com

Full Version: NPC Scripting
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok well I have my NPC scripts completed and they work fine up to the point of moving each NPC. The way I have it set up is I have tile coodinate positions (starting and an additional coodinate set for each tile movement). So lets say I have the following in a data file
Code:
8,0
9,0
10,0
10,1
10,2
With my setup this should move the NPC to thr right 3 tiles then down 2 tiles in that order. The plan is to have the NPCs follow a set path and when they reach the end they either turn around and go back or start over at the beggining again. In any case I am not looking for help with my code, but I would like to know if anyone has a better way to move NPCs than this, or a simple way to code what I am trying to do, because my current code isn't working =/

The way I am doing it now I compare each x and y coodinate to the current NPC coodinate if the value is larger or smaller it moves in that direction, after it moves to meet that coodinate the next value of data is read into the NPC object.
Can't you just do ummm....At least one full axis at a time? (more than one would be diagonal movements.

Code:
10,0
10, 8
11, 8
14, 11 'Diagonal movement

The basic:

Code:
if script_x > char_x then char_x += speed
if script_x < char_x then char_x -= speed
if script_y > char_y then char_y += speed
if script_y < char_y then char_y -= speed

'Now to check if a script is complete:
if script_x = char_x and script_y = char_y then
'do whatever you want
current_script += 1 'Move to the next script thingy
end if

isn't working for ya?
I'd rather go for a finite state machine. There are several articles about FSMs in QB Express if you don't know about them

The idea is having an "IDLE" state when you actually read the script file. Then, depending on the command read, you jump to the correct state "MOVING", "WAITING", whatever.

In each game loop, you just check which state the NPC is in, and you act accordingly. If it is "IDLE", you read the script file and change state. If it is "MOVING", you move it a step, check if it has reached its destination, and if so you change back to "IDLE"... and so forth.

The thing is having commands which save you time writing the scripts. Instead of describing a single movement every cyle, just do it in commands like this one:

Code:
MOVE 10 0
MOVE 0 -10

which would make the character move 10 tiles right, then move it 10 tiles up.

Running this code, you start on "IDLE". That means that you have to read a new command. You read "MOVE 10 0". Change the state to "MOVE" and "remember" that you should move 10 tiles accross and 0 tiles in vertical.

In every cycle, as the state is "MOVE", you move the character, then check if it has reached its destination (10 tiles right from the tile it was before the script was ran). When it does, you change back to "IDLE".

In the next cycle, now the state is "IDLE", so you read next line from the script. This time it is "MOVE 0 -10", so, again, you change the state to "MOVE" and "remember" you should move 0 tiles accross and 10 tiles up.

And so forth...

FSMs rock!
I was going to read up on finite state machines but man...^_^;; No really good sources. Now I really don't care. Busy on the story of my game.
Well it's working (sort of) the code you posted is more or less what I am doing (though mine is more complex due to more elements in the program), but for some reason the sequence is off, if I have
Code:
1,0
2,0
3,0
3,1
3,2
4,2
5,2
it is moving right to 5 before it is moving down to 2, which is confusing the hell out of me since I am reading it in sequence heh. I like the way I am doing it, makes it easy to create, but its been giving me lots of trouble.

To go into a little more detail: I have the file with all of the coodinates, those numbers are read into a variable set called npc().bounds_x() and npc().bounds_y()

as the coodinates are read in a counter is counting the elements and another variable for the total is given after the last data element is read.

the NPC gets moved at a variable speed (lets use 8 pixels for example) after the NPC has moved the length of one full tile the next set of coodinates are flagged via the counter variable. If the counter reaches the last coodinate then it is optionally reset to the first coodinate set and restart the process.

All of this is done once per main game loop and all of the relevant info is stored and updated in the NPC data type array.

EDIT: @na_th_an, yes that is one of the options I looked at, but I have been trying to think of a good way to use the same system for the scriped movement to allow for random movement range optionally. The way I have it set up I can define every tile the character is allowed to move, thus not restricting to a rectangle only.
Well, you can add as many different commands to the FSM as you like, provided you always have a method to go back to IDLE (if needed, of course).

Good luck with your project, anyway Smile