Is there a way to have no lag in haxe?

I wanted to make it force 60fps and not lag using source code. Can you help me out?

If you want your OpenFL project to run at 60fps, then you add this to your project.xml file:

<window fps="60"/>

However, that won’t force 60fps. It’s more like “try to get 60fps, if possible”. If your code is too slow, then you’ll end up with lower fps. No way around that except to optimize the performance of your code that runs every frame. If you want 60fps, then you need to make that code complete whatever it’s doing within the ~16ms available to each frame. Probably even fewer milliseconds are available to your code, to be honest, since OpenFL itself needs to use at least some of that time for its own rendering code.

Yeah, but I’m talking about haxe.

A base haxe application doesn’t have a frame based event loop. It runs until the code finishes executing and exits. If you want to implement a tick or frame rate you need to create an event loop based on timing.

I mean, the answer is the same regardless of what languages and frameworks you want to use. It wouldn’t matter if you were writing code in Haxe, C++, Assembly, JavaScript, Python or Brainf*ck. You can’t just set a flag that forces a specific fps no matter what. You need to ensure that your code is fast enough to fit within your frame budget. For 60fps, that’s 16 milliseconds.

1 Like

What’s an event loop?

What are you actually trying to do? If you want to design an event loop that runs at 16 millisecond intervals, in a base haxe application, then I can explain.

If you are trying to maintain 60 fps in an OpenFL application or similar framework with a built-in event loop then as @joshtynjala pointed out, there is no magic way to force your application to run at a “minimum” of 60fps (or any speed). Your program is executing instructions using your hardware. Each instruction takes processing time limited by how fast your hardware can complete them and the complexity of the instruction. There’s no way around this. If you want an application to run at 60fps, then it can not use more than 16ms to complete a set of given instructions per frame.

For example, if you tell your program to calculate 999 * 999 1000 times linearly, in a row and it takes 1ms on your hardware, than you can reasonably assume that you can only calculate 999 * 999 16000 times per frame without “lag”. In this scenario, if you try to run the code to make the calculation 20000 times, then each frame will no longer complete in 16ms and you will not achieve 60fps.

999 * 999 * 1000 would be 15968016000