playMaker

Author Topic: Subtract a float consistently over time  (Read 10028 times)

tutxinho

  • Playmaker Newbie
  • *
  • Posts: 20
Subtract a float consistently over time
« on: January 20, 2025, 11:53:48 AM »
Hi I'm making a stamina system and I want that an amount of stamina allow the player to run for a consistent amount of time.

I added a "Float Operator" action to subtract the amount and a "Wait" action specifying the time which would take to substract 1 stamina unit. But this seems to be dependant on the frame rate so I don't get a consistent amount of time if I play the game on different computers.



Is there any way to subtract a value consistently over time on any frame rate?

tutxinho

  • Playmaker Newbie
  • *
  • Posts: 20
Re: Subtract a float consistently over time
« Reply #1 on: January 22, 2025, 12:39:00 PM »
Any advice on this topic?

LordHorusNL

  • Beta Group
  • Sr. Member
  • *
  • Posts: 416
Re: Subtract a float consistently over time
« Reply #2 on: January 22, 2025, 06:02:08 PM »
Interesting question, there doesn't seem to be a perfect way of implementing frame rate independent waiting in Unity. Of the top of my head when using PlayMaker you could try using the "FixedUpdate" component and adding your FSM.

Alternatively you could also try limiting the frame rate of the game with the custom actions from the ecosystem.

TargetFrameRate(lets say 60)
SetVSyncCount(0)

I always just assumed Wait was frame rate independent because it uses DeltaTime.
Are you sure you absolutely need a constant amount of time across systems?



tutxinho

  • Playmaker Newbie
  • *
  • Posts: 20
Re: Subtract a float consistently over time
« Reply #3 on: January 23, 2025, 10:55:24 AM »
Thank you for your tips

Quote
you could try using the "FixedUpdate" component and adding your FSM.

Tried this, but it doesn't seem to make any difference :(

Quote
Alternatively you could also try limiting the frame rate of the game with the custom actions from the ecosystem.

TargetFrameRate(lets say 60)
SetVSyncCount(0)

Yes, I'm already doing this in fact, but I was looking for a more robust solution in case the game doesn't run at 60 FPS on lower spec computers. I'm also experiencing discrepancies between the Unity editor and builds, which makes things more difficult when it comes to work on the game balance directly from the editor.

Quote
Are you sure you absolutely need a constant amount of time across systems?

Yes, I'd like to have a consistent experience in any computer, that float specifies for how long the player can run (the player can pick items which restore the stamina while they're running), the longer the player runs without run out of stamina the more points the player receive, so if the time that an specific amount of stamina is not consistent it would be unfair for some players.

I think that the main problem is to use small time amounts, I'm using 0.05 to subtract 1 stamina unit which makes sense gameplay wise, I've tried 1s. to subtract 20 stamina units and it works consistently in different framerates, but such amount of time doesn't make sense gameplay wise.

LordHorusNL

  • Beta Group
  • Sr. Member
  • *
  • Posts: 416
Re: Subtract a float consistently over time
« Reply #4 on: January 23, 2025, 11:27:52 PM »
Yeah you're right. Waiting for small amounts of time is the problem.

You'll have to redesign your system to get around this Unity limitation if you want to remain frame rate independent.

I've included a UnityPackage. Give it at try.

tutxinho

  • Playmaker Newbie
  • *
  • Posts: 20
Re: Subtract a float consistently over time
« Reply #5 on: January 24, 2025, 11:09:01 AM »
Hey I've actually just found out a solution for this :), but thank you very much anyway for taking your time, I'll take it a look to the package, I always love to see examples and approaches from other people, I always learn something new.

I paste here my solution just in case anyone searching the forum for a similar situation find it useful.



I've just created a Tween float with a negative offset for the amount of the total stamina (100) and the time amount that it takes to deplete the stamina when it is full (5s), being a big amount of time it works super consistent in different frame rates, I made some extreme tests and it runs super solid :)

LordHorusNL

  • Beta Group
  • Sr. Member
  • *
  • Posts: 416
Re: Subtract a float consistently over time
« Reply #6 on: January 24, 2025, 11:24:42 PM »
No problem, glad you got it working right.