SanderMertens / flecs

A fast entity component system (ECS) for C & C++
https://www.flecs.dev
Other
6.47k stars 454 forks source link

delta_system_time is too faster #931

Closed Esp9527 closed 1 year ago

Esp9527 commented 1 year ago

Describe the bug delta_system_time is too faster

To Reproduce

#include <iostream>
#include "flecs.h"
#include <vector>
#include <map>
#include <chrono>

int64_t GetTimestamp()
{
    return std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::system_clock::now().time_since_epoch())
            .count();
}

struct LifeTime {
    float time;
    int64_t begin = 0;
    int count = 0;
};

float TIME_INTERNAL = 0.1;
void LifeTimeSystem(flecs::iter it, int index, LifeTime& lt )
{
    if(lt.begin == 0){
        lt.begin =  GetTimestamp();
    }
    lt.count ++;
    lt.time -= it.delta_system_time();

    if(lt.time < 0){
        std::cout << "cost " << GetTimestamp() - lt.begin << "ms" << std::endl;
        std::cout << "count " << lt.count << std::endl;

        it.entity(index).destruct();
    }
}

int main() {

    flecs::world world ;

    world.system<LifeTime>()
            .interval(TIME_INTERNAL)
            .each(LifeTimeSystem);

    auto id = world.entity();
    id.set<LifeTime>({ 6});

    world.app().run();

    return 0;
}

console print cost 5493ms count 56

Expected behavior cost 6000ms count 56

SanderMertens commented 1 year ago

Yup! Found a bug here, where the time that was returned to the system didn't correct for an overshoot value. This caused the reported time to be too high, which meant that the system seemed to run faster than it had to.

Let me know if you're still having issues!