Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

FPSC Classic Product Chat / Script Tutorial: using state, flow control, creating IF/THEN/ELSE functionality

Author
Message
Crelus
20
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 15th Feb 2005 14:41 Edited at: 15th Feb 2005 14:50
Tutorial #1: state and flow control

In FPSC script, which I'll just call script from now on, there is a value called state. While it is listed as a "variable" in the manual it is only useful for managing flow control and acts as a true only test condition for executing the current line of code. Something like:



Script DOES NOT need to use state as the condition for a line of code. It is very common to see scripts start with a state comparison, but is by no means mandatory.

For example, if I wanted a line of code to execute no matter what I could use the "always" condition like so:



I could also use one or more of the built in functions to do a condition test that would execute the line without needing the state value. For example, I could tell the AI for the NPC to lay down prone every time the divine presence of the player gets within 200 units in the following code:



NOTE: When you have more than one in a condition they are "and'ed" together (as opposed to being "or'ed"). Example:



In this statement we are saying:

IF PlayerDistance > 50 AND PlayerDistance <= 80 THEN ...

The point here being that the conditions are ANDed together to decide if the result is true.


So what good is the state variable then? It is very useful for creating execution forks in your script function. Here's how it works:

Each time the game engine loops it does a pass through the AI function for each active entity. When the engine enters the "function" it initializes the state value to 0. It doesn't matter what the value was when you exited from the last loop, when you enter again the value will be 0. Each line of code is processed in order and you cannot go backwards nor jump forwards. The lines execute one after another processing the "action" (or right) side of the line of code if the specified condition (left side) is true.

Setting the state value to a specific number and then checking for that value in later lines before executing allows you to use the state value for forking. For example, if I wanted to do the following:

IF PlayerDistance < 50
"punch the player"
ELSE IF PlayerDistance >= 50 AND PlayerDistance =< 80 THEN
"stop and look at the player"
ELSE IF PlayerDistance > 80 AND PlayerDistance <= 600
"run around"
ELSE
"lay down and sleep"
ENDIF


I could code it as follows (code without comments included below):



Here's this code without all the comments.


At this point it might seem easier to just do the following:



This would certainly work. However, there are a couple of advantages to doing it the first way specified above. It allows the code to be broken up somewhat making it a little easier to manage and expand. Also, if you decide that you need to nest another branch within a current branch this can by done using the first method.

For example, in the current AI script we want the NPC to lay down if the player is farther than 600 units away. This is specified using the "ELSE" section of our block. However, what if we wanted the PC to lay down and relax only if the player is 600+ units away AND the NPC has a health lower than 50, Otherwise you want the NPC to keep on roaming? We could do it like this:



IF PlayerDistance < 50
"punch the player"
ELSE IF PlayerDistance >= 50 AND PlayerDistance =< 80 THEN
"stop and look at the player"
ELSE IF PlayerDistance > 80 AND PlayerDistance <= 600
"run around"
ELSE
IF Health < 50
"lay down and sleep"
ELSE
"run around"
ENDIF
ENDIF



I hope this helps. Any questions please let me know.
Thanx
Mr Flowerkohl
19
Years of Service
User Offline
Joined: 19th Jan 2005
Location: Deutschland - Germany
Posted: 15th Feb 2005 15:06
thanks

yeah...thats the ticket !

Login to post a reply

Server time is: 2024-05-02 11:29:57
Your offset time is: 2024-05-02 11:29:57