Why isn't this code more efficient?

I have code set up so that the score is aways shown as 6 digits:

I tried to make my conditions/actions as efficient as possible, and so I tried this:

However, when using the second “more efficient” version, game play is significantly jittery and lots of frame drops/low frame rate. The score works as intended, but gameplay is jittery. (I tried it going the other way, too, starting with 0 at the top). Any idea why this might be?

what makes you think the second version is the more efficient? if the points increase from zero, I think the order isn’t good for the second version. unless it grows very fast. in the second screenshot you are change the text of the text object multiple times even in the same cycle, while in the first screenshot only once.
you only have to pay attention to one thing. whichever you use, pull these events below that event as a sub condition, which modifies the score. that will be efficient.

I was thinking the second version was more efficient because instead of checking for 5 conditions each frame, it only has to check for less the higher the score goes. But I see now that it is executing more actions each frame instead of just one (though I’m still not sure why that would slow down the frame rate so dramatically). Thanks!

Are you sure that it is that code? It shouldn’t be able to cause any lag. Did you verify using the profiler?

I tried it both ways where that is the only difference, and the lag happens. I’ve not tried to figure out the profiler or debugger, perhaps I should give it a try.

I’m pretty sure you can just do this with string manipulations?

StrRepeat("0",(6 - StrLength(GlobalVariableString(Score)))) + GlobalVariableString(Score)

This basically says "Add 0s to the front of GlobalVariableString(Score) based on how many characters less than 6 the score is.

So you’d have 1 event of “Score < 100000” | Change the text of Score: set to (above expression)

Then you’d have one of “Score >= 100000” | Change the text of Score: Set to GlobalVariableString(Score)

Also, the events you tried to make more efficient will conflict and bog down your game. If your score is 1, it is <100000 and <10000 and < 1000

So your events are constantly fighting each other.


Well holy moly. That works like a charm. Thank you so much for your insight!

1 Like

I had going to propose the same algorithm as Silver-Streak
One advice: always think to simplify problem even if GDevelop permit not to really program

1 Like