playMaker

Author Topic: float add/subtract every frame + every second not working  (Read 1296 times)

andyandyandy

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 63
float add/subtract every frame + every second not working
« on: April 24, 2021, 11:32:28 PM »
action float add and float subtract with every frame and every second checked would not do anything if the value is over 2million. The value will be stuck and not add/subtract. Why is there a limit it? Anyway around it?

Weak Interactive

  • Beta Group
  • Playmaker Newbie
  • *
  • Posts: 33
Re: float add/subtract every frame + every second not working
« Reply #1 on: April 25, 2021, 03:34:41 AM »
Hey,

I'm not really sure why this is happening, I'm guessing it's got something to do with decimal caps, but I don't really know; a programmer could probably enlighten us there, but I did do some testing.

It seems like if the number you're adding is too small, it doesn't get added. At 2 million, adding 1 doesn't work, but adding 50,000 does work (every frame + per second).

Using every frame without per second ticked doesn't fix the issue (the values at which it breaks are different though).

A possible workaround is to ditch the every frame + per second and just transition to a wait state after adding to the float, waiting 1 second, and then looping back. That will effectively add the same amount to the float, but it isn't as smooth.

I did crash multiple times while testing all of this though, so do proceed with caution, and good luck.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: float add/subtract every frame + every second not working
« Reply #2 on: April 25, 2021, 10:37:07 AM »
Hi.
Float does have limitations :
https://docs.microsoft.com/en-us/cpp/c-language/type-float?view=msvc-160

but im not sure why it breaks, in code i see nothing wrong,
it uses Time.deltatime to calculate the amount to add each frame.

I did a test as well and noticed when reaching 1,000,000 it still added, but it takes to long.
So it starts breaking already from 1,000,000 for me.
Which looks like it that there is indeed a issue with decimal.

andyandyandy

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 63
Re: float add/subtract every frame + every second not working
« Reply #3 on: April 25, 2021, 04:10:20 PM »
I see, thanks for clearing things up. I'll use the wait 1 second action instead then.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: float add/subtract every frame + every second not working
« Reply #4 on: April 26, 2021, 01:51:38 AM »
Hi,

 I just tested and I don't get any of that, I am on mac, are you both on windows?

 This is not right, 1Million is nothing really for computation. I tried adding 1M every frame every seconds, and it works just fine. I can't hit any issue.

The maximum float is 340282300000000000000000000000000000000

after that, you will get "infinity" written in the field.



if you have a repro scene, I'll test.

Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: float add/subtract every frame + every second not working
« Reply #5 on: April 26, 2021, 03:35:12 PM »
Hi.
Yes on windows, but adding 1m every frame/sec does work.
What does not, is have a value above 1m and add 1 every frame/sec

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: float add/subtract every frame + every second not working
« Reply #7 on: April 26, 2021, 05:29:28 PM »
It seems like you may want to use an Integer instead for this design purpose.

The float type is not deterministic on any platform and will not be perfectly accurate. With only 7 significant digits, you will have rounding issues on the whole after reaching millions either positive or negative. You can actually see significant loss in positions in Unity after the reaching 10,000 units in any direction on the transform. This gets magnitudes worse every time you increase to another significant digit. Games with large worlds use an approach called 'floating origin' to compensate for this - and literally every other application where float is used will have their own workaround, or migrate to Integers.

Integers on the other hand don't suffer from this problem, so they're commonly used in fields that require whole numbers while floats are commonly used in places where values need to be decimally precise (like positions) but not necessarily perfect.
« Last Edit: April 26, 2021, 05:32:56 PM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: float add/subtract every frame + every second not working
« Reply #8 on: April 27, 2021, 01:43:51 AM »
Hi,

 yeah, after more testing, I could replicate this, and I can replicate it outside playmaker, so this is a c# thing.

Code: [Select]
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{

    public float value = 1000000f;

    public float add = 1f;
   
    // Update is called once per frame
    void Update()
    {
        value = value + add * Time.deltaTime;
    }
}

Bye,

 Jean