• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Video Game Timer

12wwood

New member
Joined
Mar 5, 2019
Messages
10
Hey guys, I am trying to create a game that needs to include a timer. I have had trouble trying to write the code for it and I was wandering if anyone could lend a hand. I need to be able to access the current time like its a variable and print it/use it for manipulation other variables in the code.
 
This is an example from the docs under mojo.apps. This should help:)

Cerberus:
Import mojo

Class MyApp Extends App

    Method OnCreate()
   
        SetUpdateRate 60
       
    End
   
    Method OnRender()

        Local date:=GetDate()
   
        Local months:=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
   
        Local day:=("0"+date[2])[-2..]
        Local month:=months[date[1]-1]
        Local year:=date[0]
        Local hour:=("0"+date[3])[-2..]
        Local min:=("0"+date[4])[-2..]
        Local sec:=("0"+date[5])[-2..] + "." + ("00"+date[6])[-3..]
       
        Local now:=hour+":"+min+":"+sec+"  "+day+" "+month+" "+year
       
        Cls
        DrawText now,DeviceWidth/2,DeviceHeight/2,.5,.5
    End

End

Function Main()

    New MyApp
   
End
 
Are you using Mojo/Mojo2? Then Millisecs should do the trick for custom timers or - if you mean like the current system time - GetDate
 
Are you using Mojo/Mojo2? Then Millisecs should do the trick for custom timers or - if you mean like the current system time - GetDate
I am using Mojo, I have a game state called playing and i need the timer to start when that state is entered and end when the state is left.
 
Yeah Millisecs() is the way to go then. Use a variable like tstart that you set to it when entering the playing state. Millisecs() - tstart will then give you the milliseconds the state's been active. Or tend - tstart with tend = Millisecs() on leaving the state if you only need to know the playtime afterwards.
 
I am using Mojo, I have a game state called playing and i need the timer to start when that state is entered and end when the state is left.

Here's a complete example, just wait a while for the output . At some random point the game state will change to FINISHED and print the start and end times and total seconds elapsed but the app will carry on running.

Cerberus:
Strict

Import mojo

Enumerate PLAYING, FINISHED

Class MyApp Extends App


    Field startTime:Int
    Field endTime:Int
    Field state:Int
    
    Method OnCreate:Int()
    
        state = PLAYING
        startTime = Millisecs()
        ' ensure each run is random...
        Seed = Millisecs()
        Return 0
        
    End
    

    Method OnUpdate:Int()
    
        If Rnd() > 0.995 And state <> FINISHED
            state = FINISHED
            endTime = Millisecs()
            Print("Started: " + startTime)
            Print("Ended: " + endTime)       
            Print("That's: " + (endTime - startTime) / 1000.0 + " seconds")
        Endif
        
        Return 0
    End
    

End


Function Main:Int()

    Local a:MyApp = New MyApp()
    return 0
End
 
Back
Top Bottom