Community
SCI Programming => SCI Syntax Help => Topic started by: Nostalgic on April 14, 2011, 01:20:41 PM
-
Maybe I missed this somewhere; maybe it should be obvious.
Is there a way to programmatically do something based on the number of turns? (By "turn" here I guess I mean each time the player types something.) For example, I'd like to generate a certain message if the player does a given action on the first turn (or the first couple of turns). After that, however, the response will be different.
-
If you are just looking to count turns based on typing, you could set up a global variable (turns) in your Main.sc script that is initialized to 0, then increment it every time the user inputs something in the room script.
(if(Said('*>'))
++turns
(send pEvent:claimed(FALSE))
)
You could also just put the above logic in your Main.sc & you could count the number of turns throughout the game.
-
Actually, there is extra code in there. You could do it 2 ways, either with the 'no-claim' operator in the said string (>), or by just not claiming the event.
(if(Said('*>'))
++turns
)
(if(Said('*'))
++turns
(send pEvent:claimed(FALSE))
)
-
Thanks. I'm not sure it actually works. After putting in your code, I have code like this:
(if(< gTurns 2)
Print("This is the first turn.")
)
But it always prints the text "This is the first turn."
-
Hmm. I put together a more comprehensive test & it seems to work ok. Here's what I did:
1. Global variable 'turns' in the Main.sc
2. Put the following code at the top of the handleEvent method in either the main.sc or your room script
(method (handleEvent pEvent)
(super:handleEvent(pEvent))
(if(Said('*>'))
++turns
)
(if(Said('look'))
(if(== turns 1)
Print("turn 1")
)
(if(== turns 2)
Print("turn 2")
)
(if(> turns 3)
Print("too many turns to count")
)
)
)
As I put this together, I think I've got an idea as to what might have gone wrong. You have to put the turn increment code before your other said() statements, otherwise it won't execute (if another said() processed the event).
-
Let's see if I can explain how I would do it.
First off you need to determine if this is a variable that needs to be remembered after a room change or if you need it to always start at 0 when reentering room. If you need it after a room change then it needs to be a global variable placed in the main script. If you need it to reset to the beginning at room change, then declaring it as a local variable in the roomscript is the way to go. The only difference is where the variable is declared, so lets throw a name out there.
iSayHello = 0
Now lets put the lil sucker to use. In a case like this with an integer counter, I would strongly suggest using a switch. So now we may as well use it to count and report. So let's take care of whenever the user types "hello"
(if(Said('hello'))
(switch(iSayHello)
(case 0 Print("Well hello to you too!"))
(case 1 Print("Man I heard you, I said hello too, geesh."))
(case 2 Print("I don't know why you say goodby, I say hello"))
(default Print("you are being ignored."))
)// end switch
++iSayHello // increase counter
)// end said helloAnd there you go. simple enough right? Of course, the process would be similar for counting anything.
-
Yeah, a switch makes more sense in this case :P. The other thing that came to me is that my explanation as to why the code needs to be at the top of the method was incomplete. Once a said() statement is processed, no other statements below it will be (unless you un-claim the event), which isn't intuitive at all.
-
Also as a side note here on switches, mainly to catch Robin up but also for you new guys... scistudio used to have a hard time with a lot of case statements. I think after 12 you were pushing your luck. That limitation is not present with scicompanion. for instance in the BC inventory script, studio crashes trying to compile it, companion has no problem. there's one switch in it with 30 or 40 cases.