playMaker

Author Topic: Get Framerate  (Read 369 times)

Thore

  • Sr. Member
  • ****
  • Posts: 341
    • View Profile
Get Framerate
« on: September 09, 2019, 06:00:17 PM »
I've adapted this script for two nice (advanced) framerate counters.

GetFramerate.cs returns averaged out highest, average and lowest framerates in a certain time range (60 ~1 second; 1 = actual framerate in that frame) you provide.

These values that are returned come both as integers and as strings. The reason for already turning them into strings was given in the example: the strings are cached and thus the integers don't need to be converted anew every frame, which apparently minimizes the footprint. You can then use the strings to feed them into an UI element, for instance and put it anywhere on screen. Of course, whether you use all three framerates, or just the middle average, and how you style them is up to you.

GetFramerateUI.cs is yet more fancy. Here you can also plug in the UI text elements from the canvas directly and it updates them, and also colours the values based on the frame rate. You can configure the colours, so when you don't want them changing, just set them to the same colour.

For Experts
I could not set custom colours to start with. When I initialize (or reset) with public FsmColor = new Color(r,g,b,a) , the color field in PlayMaker behaves weirdly. This may(!) be a bug. Hence, I had to go with the already created Unity color scheme. However, users can customize them.

Another thing that could be more elegant is the UpdateUI method. You see I convert the ints to strings separately, and then feed both into the method, even though it would be more elegant to just provide the ints and text field and then do the conversion inside there in one go. However, because the string is an FsmString, it would not return the updated value, no matter which combination of fpsString or fpsString.Value I used.

Maybe someone can enlighten me how these things would be done.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14684
  • Official Playmaker Support
    • View Profile
Re: Get Framerate
« Reply #1 on: September 11, 2019, 01:56:24 AM »
Hi,

Quick c# tip:

 When you want to format an int into a string with leading 0, you should use string format:
Code: [Select]
int value = 1;
Debug.Log(value.ToString("00")); // outputs "01";

https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros

Bye,

 Jean

Thore

  • Sr. Member
  • ****
  • Posts: 341
    • View Profile
Re: Get Framerate
« Reply #2 on: September 14, 2019, 08:06:50 AM »
Hi,

Quick c# tip:

 When you want to format an int into a string with leading 0, you should use string format:
Code: [Select]
int value = 1;
Debug.Log(value.ToString("00")); // outputs "01";

https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros

Bye,

 Jean

Thanks  :) The author (see here) notes that converting in runtime costs performance which would skew the result, that's why there's the static string list at the end, which -- I think -- is then referenced by index, and without converstion.

Another way where this action could improve (aside of the minor things listed above) is turning the Game Objects for the text fields into proper FsmGameObjects. Normally this should be cake, just with adding the .Value at the end, but in this case, while the script itself looks clean, PlayMaker really doesn't like this for some deeper reason above my expertise.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14684
  • Official Playmaker Support
    • View Profile
Re: Get Framerate
« Reply #3 on: September 17, 2019, 01:40:05 AM »
Hi,

 uhm... I see. fair enough. but at this point if your fps changes because of that line of code, you basically have a hello world scene with nothing much happening :)

 Bye,

 Jean