How to start a timer when a certain scene begins?

britto24britto24 Member Posts: 35
Hello,

I am creating a racing game and trying to create a timer that starts when the race begins and the cars move and ends when they cross(collide) with the finish line. I have watched all the timer tutorials, but I'm still super new to GameSalad and can't figure this out. Right now I just have the game.time attribute being displayed. I know I need to create more custom attributes, but I need some help. Any suggestions? I would really appreciate it!

Thanks so much!

Comments

  • BarrytheBraveBarrytheBrave Member Posts: 134
    edited May 2012
    Hey, firstly make the following attributes:

    game.running (boolean), inititally set to FALSE
    game.raceTime (real), initially set to 0

    When you want the timer to start (when the player presses the accelerator for example) have a rule that says:

    change attribute game.running to TRUE

    Have another rule somewhere that says:

    When game.running = TRUE, every 0.1 seconds change game.raceTime to game.raceTime+0.1 (This starts a timer, but instead of using game.time, which starts as soon as the game starts, this will use your custom attribute to store the time).

    On the car actor, have a rule that says:

    When collides of overlaps with (finish line actor) change attribute game.running to FALSE
    (This will stop the game.raceTime from incrementing any higher, so it will keep showing the final time until you tell it otherwise).

    Hope that's good, I'm away from Gamesalad so did that with the power of thought alone :-) it might need a bit of tinkering but that's the logic of how I'd do it.

    PS welcome to gamesalad!
  • britto24britto24 Member Posts: 35
    Thanks ooi! I'm trying it out now. This was super helpful! How do I display the game.raceTime?
  • BarrytheBraveBarrytheBrave Member Posts: 134
    Create another actor, drag in the display text behaviour, and then set the text to display as game.raceTime. Make sure you don't type game.raceTime, but select it from the drop down box. Hope that works :-)
  • britto24britto24 Member Posts: 35
    edited May 2012
    Cool, I followed these steps and now have the game.raceTime being displayed. However, the number stays at 0... Hmm, something's not right. Maybe it's my true/false rules?
  • simo103simo103 Member, PRO Posts: 1,331
    @britto24 .. many of us use the display text for testing/debugging. You can create a spare actor that you can drop the display text behavour in and then select a Game attribute to display and drop the actor in your scene. That way you can check that your attributes are 'firing' at the times that you expect them too.
  • BarrytheBraveBarrytheBrave Member Posts: 134
    Hmm, it's hard to tell without being in front of a copy of gamesalad. I'm sure the logic is good, but it's possible that in it's implementation something is causing a problem.

    I would say that as you are new to gamesalad, make sure that you try to learn the basics at a sensible pace, rather than trying to build an impressive game from day 1. The better grounding you have on the ins and outs of the software, the better equipped you'll be to tackle these sorts of problems.

    However with the problem at hand, perhaps somebody else who has access to GSC might be able to chip in?... If not I can have a play around when I get home, but that won't be for hours yet.
  • britto24britto24 Member Posts: 35
    Thanks simo103! That was helpful indeed. Now I see that my game.running is not turning true when it's supposed to...
  • britto24britto24 Member Posts: 35
    @ooi Thanks so much! I appreciate your help. Don't worry, I'm not making a complicated game...I can't even handle this, lol. It's no rush. I just thought it would add something if there was a race time, and I thought there would be a tutorial somewhere, but I haven't found it yet, haha. I understand the logic that you laid out. I just need to figure out why it isn't turning true. I'll keep messing with it. So close! Thanks again!! I'll let you know if I figure it out...or if I don't! :)
  • britto24britto24 Member Posts: 35
    Awesome! It's working now! Thanks for your help @ooi! You rock :D
  • BarrytheBraveBarrytheBrave Member Posts: 134
    Great @britto24, glad I could be of help :-)
  • MotherHooseMotherHoose Member Posts: 2,456
    the computer monitors/tracks: gameTime; sceneTime; individualActorsTime in scene
    just store the currentValues for those attributes in newAttributes

    Example:
    gameAttribute: real type … name raceTime

    on your carActor:
    selfAttribute: real type … name startTime
    selfAttribute: real type … name endTime

    Rule: when
    Event: touch is pressed … or Attribute: whatever value you use to start race
    -changeAttribute: self.startTime To: self.Time

    Rule: when
    Event: overlaps or collides with finishLine
    -changeAttribute: self.endTime To: self.Time
    -changeAttribute: game.raceTime To: self.endTime-self.startTime

    image MH
  • BarrytheBraveBarrytheBrave Member Posts: 134
    edited May 2012
    That's a much better way of doing it @MotherHoose, it cuts out the need to update an attribute every 0.1 seconds. ^:)^
  • MotherHooseMotherHoose Member Posts: 2,456
    edited May 2012
    +1 @ooi … and those attributes will keep very accurateTime

    forgot that if you are running more than one race in game
    … before you changeAttribute self.startTime behavior
    … add changeAttribute: game.raceTime To: 0

    also use that startTime attribute if you want to display the runningTime
    on displayText actor … unlocked …
    Rule: when
    Attribute: scene.Background,car.startTime > 0
    -displayText … Text: scene.Background,car.Time-scene.Background,car.startTime

    of course if you only want to display 2 decimal points in that one:
    -Functions: precision
    … puts: prec(x,y) in the expressionEditor
    … the x holds the value you select , for y you type in the number of decimals (2)

    IMO … you have a strong grasp of what you want and what will work best
    … and ask the right questions! … which means you are making great progress with GS!

    @};- MH
  • britto24britto24 Member Posts: 35
    @MotherHoose Wow! This worked amazing! Thanks so much!

    I have a few problems though...

    1. My display text shows the race time great, but when the car crosses the finish line the time keeps going. How do I stop the time and just display the final time when the car crosses the finish line?

    Right now on the car actor I have:

    Rule: when, Actor: overlaps or collides with finishLine
    -changeAttribute: self.endTime To: self.Time
    -changeAttribute: game.raceTime To: self.endTime-self.startTime

    2. When the car actor crosses the finish line, it's supposed to spawn the youWin actor. However, right now when the car crosses the finish line nothing happens. Here's the rule for the car:

    Rule: when, Actor: overlaps or collides with finishLine
    -spawn actor youWin

    But if I wrap x for the scene, it will spawn the youWin actor once the car goes back to the start (when it wraps back around). But neither one of these is right. I must be missing a small detail...Do I need to mess with the physics or motion on the finish line or car? How do I get the game to end when the car crosses the finish line?

    Woo, that was a lot! Thanks again everyone for helping! I'm learning a ton and loving it!!!
  • MotherHooseMotherHoose Member Posts: 2,456
    edited May 2012
    perhaps the spawn places the youWin in an offSceneArea??? you can not see it?

    as the youWin is spawning … to X,Y positions defined by the carActor

    if the scene is wrapped …
    set the spawn position X (240) and Y (160) relative to scene

    or on the youWin actor: first behaviors
    changeAttribute: self.Position.X To: 240 (middle of scene on X axis)
    changeAttribute: self.Position.Y To: 160 (middle of scene on Y axis)
    ===
    if not wrapped … then spawn Position > is sceneWidth - 240 and Y of course is still 160
    relative to scene

    @};- MH
  • britto24britto24 Member Posts: 35
    @MotherHoose

    Thank you!!! Yep, that was the problem...it was spawning off screen...duh, lol.
  • britto24britto24 Member Posts: 35
    So now everything is working properly. Thanks everyone!

    Eventually, I will want the final time to be displayed under the you win. It would be like a high score in the level, so if you play it again and beat your old time, the new time would appear. But if you don't beat your time, then the old time appears under the you win as the high score as well as the time you just received. If that made any sense! Anyway, that's probably way over my head at this point, but hopefully in the future I can get that to work. If you have any ideas for that, I'm all ears. Thanks again!!!
  • MotherHooseMotherHoose Member Posts: 2,456
    @britto24 … good that is spawning and you can see it!

    not way over your head … just focus on the getting the other things done now …
    when you want to work on the displaying high-score … @tshirtbooth has excellent video(s) for this … so do those videos when you are ready!

    also, we are getting writable tables … so storing the scene high-score will be simpler then
    (well hopefully! :)

    image MH
  • britto24britto24 Member Posts: 35
    edited May 2012
    @MotherHoose Thanks!!! I watched @tshirtbooth's videos...amazing! But, I see two problems that he doesn't have (of course!)

    1. When I play the scene for the first time, everything works, but when I replay the scene, the raceTime actor does not show up anymore. Thoughts?

    2. So the raceTime actor will display the time as 00:36:11 but the bestTime actor will round down the time and displays the best time as 00:36:00. The milliseconds are always zero for the best time. Any ideas?

    Cool, the writable tables sounds amazing! Would that be coming in the next version? Or how do you get it when it's available?

    Woo, I'm slowly getting there! Thanks sooo much!!!
  • MotherHooseMotherHoose Member Posts: 2,456
    1. when I reset or return to the scene … raceTime actor does show …
    if you can … post a screenshot … please

    2. what type of attribute is the bestTime actor? it should be real type to display the milliseconds

    image MH
  • britto24britto24 Member Posts: 35
    @MotherHoose

    1. Maybe I worded it weird...When I click on the play button to preview my game, I play it and all is working, but if I click the restart button (the circle with the arrow) still in the preview game, the race time will not display. But, if I go back to edit the scene (but don't edit anything) and click on the play button to preview my game again, then like before, everything works and the time is displayed but if I restart the scene to play again, the time doesn't show up...and so on and so on... Did you want a screenshot of the rules for the raceTime actor?

    2. It is a real attribute. I duplicated the raceTime actor, so they should be the same except the bestTime actor displays the bestTime attribute instead of the raceTime attribute.

    Thanks!
  • MotherHooseMotherHoose Member Posts: 2,456
    @britto24 … message in your Inbox at top of this page …

    I will take a look at your project … might be easier to spot the problem

    image MH
  • britto24britto24 Member Posts: 35
    Cool, thanks!
Sign In or Register to comment.