Static properties

Monkey Forums/Monkey Programming/Static properties

Leo Santos(Posted 2015) [#1]
Hi,

I was all happy with myself, having created a super organized little module. Then I created a few functions that I wanted to behave like read-only globals, so I slapped the "Property" keyword after them, just like I'd do with a method. Then I got a "Functions cannot be properties" error. Bummer. Now I'm sad.

Is there a way to create a global read only variable that can be accessed without creating an instance of a class - in other words, a static property?

Not a big deal, I could always just use a function that returns the value. But it would be neat if I could access a global in a read-only way. For example, instead of calling:

Time.Fps()


I wanted to call (in Strict mode):

Time.fps


As if "fps" is a global in the "Time" class.

Cheers.


bitJericho(Posted 2015) [#2]
Can you use a constant for this? If not, perhaps you can use a Getter without a Setter?


Steve Ancell(Posted 2015) [#3]
Going by your question it seems like you're after something that gives FPS (Frames Per Second), is that correct?

There ya go!, I cooked-up a little present for ya. ;-)


Class C_Timer
	Field ticker:C_Ticker
	Field interval:Float
	
	
	Method New(interval:Float)
		Self.SetInterval(interval)
		Self.ticker = New C_Ticker()
		Self.ticker.UnPause()
		
	End
	
	
	Method Reset:Void()
		Self.ticker.Reset()
		
	End
	
	
	Method SetInterval:Void(interval:Float)
		Self.interval = interval
		
	End
	
	
	Method GetTrigger:Int()
		Local flag:Int
		
		
		If Self.ticker.GetTimePassed() >= 0
			If Self.ticker.GetTimePassed() >= interval
				flag = 1
				
				Self.Reset()
				
			Else
				flag = 0
				
			EndIf
			
		EndIf
		
		Return flag
		
	End
	
	
	Method Pause:Void()
		Self.ticker.Pause()
		
	End
	
	
	Method UnPause:Void()
		Self.ticker.UnPause()
		
	End
	
End



Class C_FPS
	Field timer:C_Timer
	Field frameCount:Int
	Field output:Int
	
	
	Method New()
		Self.timer = New C_Timer(1000)
		Self.frameCount = 0
		Self.output = 0
		
	End
	
	
	Method Get:Int()
		Self.frameCount = (Self.frameCount + 1)
		
		If Self.timer.GetTrigger() = 1
			Self.output = Self.frameCount
			Self.frameCount = 0
			
		EndIf
		
		Return Self.output
		
	End
	
End



Steve Ancell(Posted 2015) [#4]
To use it you just create a global object, for example: Global glo_FPS:C_FPS = New C_FPS()
To access it just call Get() from the object by either sticking it into an Integer variable or straight to screen with DrawText.


Leo Santos(Posted 2015) [#5]
Can you use a constant for this?

Nope. The values are set by the class, internally, on every frame.

perhaps you can use a Getter without a Setter?

I'm intrigued. How do you do this in Monkey? Couldn't find any info about it in the docs.

There ya go!, I cooked-up a little present for ya. ;-)

Thanks, I like presents!


bitJericho(Posted 2015) [#6]
Look up properties in the language section of the docs. Also look up private and public. Basically you create a private field, then you create a method with the property keyword to get the field, but you don't create a public method to set the field. This means you can access the field from another class but you cannot modify it.

Class test
   Public
   Method New()
      Self.Testvar = 10
      Debug Self.Testvar
   End
   Method Testvar:Int() Property
      Return Self.testvar
   End
   Private
   Method Testvar:Void(value:Int) Property
      Self.testvar = value
   End
   Field testvar:Int =5
End


Note that according to the docs, the standard is that properties should be Pascal case and fields camel case.


APC(Posted 2017) [#7]
Where can i get the code for Class C_Ticker? it is missing in the above code.