Author Topic: Number of Turns  (Read 7287 times)

0 Members and 1 Guest are viewing this topic.

Offline Nostalgic

Number of Turns
« 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.




Offline gumby

Re: Number of Turns
« Reply #1 on: April 15, 2011, 12:35:47 PM »
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.

Code: [Select]
(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.
« Last Edit: April 16, 2011, 09:27:58 AM by gumby »
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline gumby

Re: Number of Turns
« Reply #2 on: April 16, 2011, 10:50:00 AM »
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.

Code: [Select]
(if(Said('*>'))
     ++turns
)

Code: [Select]
(if(Said('*'))
     ++turns
    (send pEvent:claimed(FALSE))
)
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Nostalgic

Re: Number of Turns
« Reply #3 on: April 16, 2011, 01:01:05 PM »
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."

Offline gumby

Re: Number of Turns
« Reply #4 on: April 17, 2011, 05:27:52 PM »
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

Code: [Select]
(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).
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Cloudee1

Re: Number of Turns
« Reply #5 on: April 17, 2011, 06:45:04 PM »
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.
Code: [Select]
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"
Code: [Select]
(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 hello
And there you go.  simple enough right? Of course, the process would be similar for counting anything.
« Last Edit: April 17, 2011, 06:52:51 PM by Cloudee1 »
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline gumby

Re: Number of Turns
« Reply #6 on: April 17, 2011, 09:52:46 PM »
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.
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Cloudee1

Re: Number of Turns
« Reply #7 on: April 18, 2011, 11:26:05 AM »
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.
« Last Edit: April 18, 2011, 06:12:16 PM by Cloudee1 »
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.074 seconds with 22 queries.