- Home /
Unknown Identifier in Finite State Machine
Hey all, I'm having trouble implementing a Finite state machine and every piece of documentation I've seen seems deliberately vague. Basically, my code's turning up an Unknown Identifier error in lines 19, 21, 23, and 25(case X). Can someone explain to me why this error is appearing and how to rectify it? Also, how do I go about changing the state from within the functions called from Update?
var speed = 10; var minspeed = 2.5; speed *= Random.value; speed += minspeed; var shipTarget : Transform;
enum state { WAITING, SEEKING, ATTACKING, MOVING }
function Update () { if (Time.timeScale > 0) { if (transform.position.y == 0) { switch (state) { case WAITING: shipWait(5); case SEEKING: facePlayer(shipTarget); case ATTACKING: fire(); case MOVING: moveForward(); }
}
}
}
function shipWait (waitTime : float) { //Waits up to 5 seconds. yield WaitForSeconds(Random.value * waitTime); }
function moveForward () { //Simply moves forward transform.Translate (0, 0, speed * Time.deltaTime); }
function facePlayer (target : Transform) { //Turns to face the player if (shipTarget) { // Look at and dampen the rotation var rotation = Quaternion.LookRotation(shipTarget.position - transform.position); transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime); } }
function fire () { gameObject.SendMessage("Shoot", SendMessageOptions.DontRequireReceiver); }
We can not really see code lines by the way, it's pretty obvious , but in a 3000 line script........ well it's get tricky ;)
Answer by Statement · Dec 22, 2010 at 02:54 PM
2 errors.
- You're trying to switch on a type (state is a type, not a variable).
- You must include the enum type in your case switches. It is not sufficient to write "WAITING". It should be "state.WAITING". In other languages like C++ you dont write the enumeration type strongly like in C#/JS.
You probably also should use break; for each of your cases or your code would "fall through" the next case(s).
enum State { // Renamed type state to State. WAITING, SEEKING, ATTACKING, MOVING }
var state : State; // Need to declare a variable.
function Update () { if (Time.timeScale > 0) { if (transform.position.y == 0) { switch (state) { case State.WAITING: // Need to be written ENUMTYPE.ENUMMEMBER shipWait(5); break; // Probably want to break. case State.SEEKING: facePlayer(shipTarget); break; case State.ATTACKING: fire(); break; case State.MOVING: moveForward(); break; }
}
}
}
HEY HEY HEY ! I was just posting that! +1 for $$anonymous$$d reading :(
Awesome! This fixed everything! Thanks for the help!
$$anonymous$$eh it's even quite a terse statement! $$anonymous$$udos, I was going for my usual wall of text
I usually wait until it seems that noone will contribute a quick answer and then give some sort of kludgy verbose workaround thing. I mostly learn coding practices by looking at others, so most of what I give that is good is ultimately derived from someone else. Not directly, but in a "shoulders of giants" sort of way. Anyway, this code just got added to my mental library, so thanks. Actually, I've picked up quite a few things from both of you, so many thanks.
Answer by Justin Warner · Dec 22, 2010 at 02:31 PM
You need a break statement I believe, that's why they don't let us use switch statements in college... lol.
I'm not sure I understand. Where do I need a break statement?
case Something: Code; Break; (on seperate lines of course)
Just tried that, still giving me the $$anonymous$$ Identifier error.
Your answer
Follow this Question
Related Questions
Getting my object to rotate and move at random 1 Answer
Using coroutines with a Finite State Machine help 2 Answers
Basic AI finite state machine: where to put decision logic? 2 Answers
AI script problems, interaction with grenade not working, need help. 2 Answers
Ai Zombie Melee Attack script. 5 Answers