Problems with basic AI code
by Sandor Moldan · in Technical Issues · 03/13/2007 (3:34 pm) · 3 replies
Hi,
i'm having a bit of a problem with some basic AI code i am running. this is probably a bit of a newbie question, but any help would be appreciated!
i am modifying the server/scripts/aiPlayer.cs file inside starter.fps - i want to set up a few basic AI behaviours and be able to turn them on and off, or switch between them. i have added a variable called aiMode to the datablock that is being used when new AIPlayer() is being called. when i echo that variable in the console, it always shows the correct value - depending on what 'mode' the bot has been set to, however when i use that same variable in an if statement to get the bot to decide whether it should do/continue doing any given action, it completely ignores the if statement and runs the code inside anyway!
i'm at work, so i don't have my code with me, but an example of what is happening is:
on creation the aiMode variable is set to "none".
one basic action i am using is one wher the bot follows the player around. this is set by a function called setFollower(). this sets aiMode to "follower", and calls another function that tells the bot to run towards the player if it is more than 5 units (metres or whatever they are...) away, which then uses schedule() to run that function again every 500ms, but only runs the code inside the function if aiMode = "follower".
the Follower behaviour is working perfectly.
i have another function called stopFollower() which simply sets aiMode back to "none". this theoretically should cause the behaviour function to be called on the next schedule(), fail to meet the requirements of the if statement and not run any of the code inside - meaning schedule() will not be called again.
but this doesn't work. the bot keeps following me around, even though aiMode is set to "none".
i hope i've explained this well enough. thanks in advance if you can shed any light on what i am doing wrong. my best guesses are that i may be misusing variables set in the datablock, or not using schedule() properly, but i'm really not sure.
i'm having a bit of a problem with some basic AI code i am running. this is probably a bit of a newbie question, but any help would be appreciated!
i am modifying the server/scripts/aiPlayer.cs file inside starter.fps - i want to set up a few basic AI behaviours and be able to turn them on and off, or switch between them. i have added a variable called aiMode to the datablock that is being used when new AIPlayer() is being called. when i echo that variable in the console, it always shows the correct value - depending on what 'mode' the bot has been set to, however when i use that same variable in an if statement to get the bot to decide whether it should do/continue doing any given action, it completely ignores the if statement and runs the code inside anyway!
i'm at work, so i don't have my code with me, but an example of what is happening is:
on creation the aiMode variable is set to "none".
one basic action i am using is one wher the bot follows the player around. this is set by a function called setFollower(). this sets aiMode to "follower", and calls another function that tells the bot to run towards the player if it is more than 5 units (metres or whatever they are...) away, which then uses schedule() to run that function again every 500ms, but only runs the code inside the function if aiMode = "follower".
the Follower behaviour is working perfectly.
i have another function called stopFollower() which simply sets aiMode back to "none". this theoretically should cause the behaviour function to be called on the next schedule(), fail to meet the requirements of the if statement and not run any of the code inside - meaning schedule() will not be called again.
but this doesn't work. the bot keeps following me around, even though aiMode is set to "none".
i hope i've explained this well enough. thanks in advance if you can shed any light on what i am doing wrong. my best guesses are that i may be misusing variables set in the datablock, or not using schedule() properly, but i'm really not sure.
#2
use a global variable for aiMode by preceding it with a $
so:
if ($aiMode $= "follower")
{
// yada yada
}
03/18/2007 (5:15 am)
Use $= to compare stringsuse a global variable for aiMode by preceding it with a $
so:
if ($aiMode $= "follower")
{
// yada yada
}
#3
btw, is that supposed to be used as an assignment operator or a comparitive operator or both?
ie. which of these is right (or both)...
%variable $= "a string";
if (%variable $= "a string") {
//
}
^ i'm asking as when i echo()-ed some of these variables in the console it returned an empty string when the variable was set in the datablock. once the variable was set inside a function in the AIPlayer namespace it would echo() correctly.
also, i wasn't using global vars, as i need to have different AIPlayer instances doing different things. i don't want all of them to follow me, only one.
03/18/2007 (7:13 pm)
Thanks for your help, pete. i didn't really know about the $= operator, so that may have been the problem.btw, is that supposed to be used as an assignment operator or a comparitive operator or both?
ie. which of these is right (or both)...
%variable $= "a string";
if (%variable $= "a string") {
//
}
^ i'm asking as when i echo()-ed some of these variables in the console it returned an empty string when the variable was set in the datablock. once the variable was set inside a function in the AIPlayer namespace it would echo() correctly.
also, i wasn't using global vars, as i need to have different AIPlayer instances doing different things. i don't want all of them to follow me, only one.
Torque Owner Sandor Moldan