Millisecs() equivalent?

Monkey Forums/Monkey Programming/Millisecs() equivalent?

John Galt(Posted 2011) [#1]
Is there anything in monkey to replace millisecs()?


Canardian(Posted 2011) [#2]
It's called Millisecs() in Monkey.


John Galt(Posted 2011) [#3]
Boy is my face red! Thanks.


Canardian(Posted 2011) [#4]
Got sunburn?


taumel(Posted 2011) [#5]
This is of the things where i think Unity does a better job.

It offers a time class with several variables, a.o. time (time since startup in seconds), deltaTime (time since the last frame in seconds) and frameCount (frames since startup). Why Millisecs? If monkey is still open to a degree that such things can be changed then i would very much welcome such a time class based on seconds.


Canardian(Posted 2011) [#6]
You can easily implement a time class in the monkey language. I think Monkey should use the same principle as MODULA 2, which has no commands which can be implemented with the existing core commands.

Monkey should stay small, fast and clean, and all those classes and game frameworks should be seperate modules.

It would be a good Monkey community project to establish such a game framework, so that Monkey will have much better game commands as Unity.


John Galt(Posted 2011) [#7]
Plain old millisecs is fine for me.


taumel(Posted 2011) [#8]
@Lumooja
I know that i can implement some of those, others won't work due to missing information or different design but i would find it handy if it would be support out of the box and living in a metric more seconds based world, millisecs always was kind of weird.


Canardian(Posted 2011) [#9]
Millisecs() in measurement system independant. Another benefit of Millisecs() is that it's backwards compatible with BlitzMax code, so it's easier to port BlitzMax apps to Monkey, of which I'm quite sure there are a lot of reusable games and code snippets.

I would also prefer that Mark makes the core commands first working on all targets, before doing any kind of high level classes, which the community can do anyway already now.

The biggest show stopper is at the moment the missing sound on Android phones, because I've seen apps on Android which have only sound, no graphics at all, for example that app where you have several pages of simple round buttons, which each make a different sound. It's really useful and cool, and I want to make something similar too. My colleague uses that app all the time at office, and his favorite sound is the Wookie growl. That app is not only a useful office app, but it can be used also with electric cars. For example here in Finland, the government is planning to have somekind of traffic rule that electric cars must emit sound, because they are else too dangerous for deth and other pedestrants. So I could just connect my Android phone to the car's loudspeakers and emit all kinds of funny sounds. Of course it should be 3D surround sound, so I could simulate that my car is actually completely somewhere else than it really is.


taumel(Posted 2011) [#10]
I don't think that porting from milliseconds to seconds would be a showstopper. monkey more is a chance to do things better like Mark has done with a few but great tweaks already. A time class/module in my opinion would be another brick in this more perfect wall.


Canardian(Posted 2011) [#11]
Well, here is a Timer class for you. It works like a real stopwatch:
' MonkeyToolsAdditions.monkey

Import mojo

Class Timer
	Field timer:Float
	Field starttime:Float
	Field stoptime:Float
	Field paused:Bool
	Field stopped:Bool
	Method New()
		stopped=True
	End
	Method Start:Void()
		Reset
		Resume
	End
	Method Pause:Void()
		If Not paused Then
			paused=True
		End
	End
	Method Stop:Void()
		If Not stopped Then
			stopped=True
			stoptime=Millisecs
			If paused Then timer=Millisecs
		End
	End
	Method Resume:Void()
		If paused Then paused=False
		If stopped Then
			stopped=False
			starttime+=Millisecs-stoptime
		End
	End
	Method Reset:Void()
		starttime=timer
	End
	Method GetTime:Float()
		If Not stopped Then
			If Not paused Then timer=Millisecs
		End
		Return (timer-starttime)/1000.0
	End
End

And an app for testing it:
Import mojo
Import MonkeyToolsAdditions

Class Game Extends App
	Field timer:Timer
	Method OnCreate()
		SetUpdateRate 60
		timer=New Timer
	End
	Method OnRender()
		Cls 30,60,30
		DrawText "Timer is "+timer.GetTime(),0,0
		DrawText "F1: start timer" ,0,15
		DrawText "F2: pause timer" ,0,30
		DrawText "F3: resume timer",0,45
		DrawText "F4: reset timer" ,0,60
		DrawText "F5: stop timer"  ,0,75
	End
	Method OnUpdate()
		If KeyDown(KEY_F1) Then timer.Start
		If KeyDown(KEY_F2) Then timer.Pause
		If KeyDown(KEY_F3) Then timer.Resume
		If KeyDown(KEY_F4) Then timer.Reset
		If KeyDown(KEY_F5) Then timer.Stop
	End
End

Function Main()
	New Game
End



taumel(Posted 2011) [#12]
Thanks but regardless of that i know how to implement this on my own there is a difference if you can just use such variables or have to code their behaviours on your own. Dealing with time in video games really is fundamental. I don't wanna know how many different versions of deltaTimes are running around, more if you need smoothed ones. Using just a few variables provided by monkey would be way easier. Beside of this i miss the deltaTime and the frameCount in your example. :O)


Canardian(Posted 2011) [#13]
But if we build a standard library which is always updated by the community, it's the same as if it would be part of Monkey.

I hope Mark would setup a GIT account on some server and allow registered Monkey Pro users write there, where we could have the community library always uptodate. Then you wouldn't need to download it either, since it would be in your Monkey directory.


taumel(Posted 2011) [#14]
Second best solution after that it's supported natively.

A Time class also isn't something you would have to update frequently, unless monkey gets some changes and you want stuff like fixedTime, fixedDeltaTime and so on.


taumel(Posted 2011) [#15]
Hi, i just wrote a little Time Class based on seconds. It doesn't contain everything yet but it's sufficient for what i need right now. You might find it useful.

Saved in a external file called time.monkey
Import mojo.app


Class Time
	Field targetFps:Float
	Field currentFps:Float
	Field deltaTime:Float
	Field deltaTimeFactor:Float
	Field currentTime:Float
	Field timeSinceStart:Float	
	Field framesSinceStart:Float

	Field timeFpsUpdateInterval:Float
	Field oldTime:Float,timeFpsCurrent:Float,framesSinceFpsUpdate:Float

	Method New(fps:Float,upd:Float)
		targetFps=fps
		timeFpsUpdateInterval=upd
		oldTime=Millisecs*.001
		timeSinceStart=0
		framesSinceStart=0
		timeFpsCurrent=0
		framesSinceFpsUpdate=0
	End

	Method New(fps:Float)
		targetFps=fps
		timeFpsUpdateInterval=0
		oldTime=Millisecs*.001
		timeSinceStart=0
		framesSinceStart=0
		timeFpsCurrent=0
		framesSinceFpsUpdate=0
	End
	
	Method Update()
		currentTime=Millisecs*.001
		deltaTime=currentTime-oldTime
		deltaTimeFactor=deltaTime*targetFps
		oldTime=currentTime
		framesSinceStart+=1
		timeSinceStart+=deltaTime
		If timeFpsUpdateInterval>0 Then
			framesSinceFpsUpdate+=1
			timeFpsCurrent+=deltaTime
			If timeFpsCurrent>=timeFpsUpdateInterval Then
				currentFps=framesSinceFpsUpdate/timeFpsCurrent
				timeFpsCurrent=0
				framesSinceFpsUpdate=0
			End
		End
	End
End


Called this way
Import mojo
Import time


Class MyApp Extends App
	Field time:Time

	Method OnCreate()
		fps=30
		SetUpdateRate fps
		time=New Time(fps,.1)	
	End

	Method OnUpdate()
		time.Update()
	End

	Method OnRender()
		Cls 0,0,0
		DrawText "targetFps="+time.targetFps,32,20
		DrawText "currentFps="+time.currentFps,32,20*2
		DrawText "deltaTime="+time.deltaTime,32,20*3
		DrawText "deltaTimeFactor="+time.deltaTimeFactor,32,20*4
		DrawText "currentTime="+time.currentTime,32,20*5
		DrawText "timeSinceStart="+time.timeSinceStart,32,20*6
		DrawText "framesSinceStart="+time.framesSinceStart,32,20*7
	End
End


Global fps:Float


Function Main()
	New MyApp	
End