Repeatedly Execute. Question.

Started by Pax Animo, Sun 24/10/2021 18:05:41

Previous topic - Next topic

Pax Animo

Hello,

I'm still learning the ins and outs of the programming language and often wonder how repeatedly_execute works in general.
How much is too much and what should be avoided plus learning better ways to avoid putting too much into repeatedly execute.

For an example of code I often place into repeatedly execute,  labels constantly updating characters stats.

Code: ags
if (gPlayerPortrait.BackgroundGraphic == 239) {
    lPlayerStatsEnergy.Text = String.Format("%d", citizens[0].energy);           
    lPlayerStatsHP.Text = String.Format("%d", citizens[0].hp);
  }


I have quite a few of these running all the time. Is this the correct approach?
Also how much of a impact on performance does too much repeats have on overall game play, am i going to run into problems doing it in such a way.

Another example is from a smooth scroll/parallax module I'm using which constantly updates room camera.

[imgzoom]https://imgur.com/MOP6Vj8.png[/imgzoom]

Will this have some effect on performance and is there a better way of controlling how it works?

Misunderstood

Cassiebsg

Remember that rep_exec runs 40 times a sec (assuming that you left it at default). If you need to check something 40 times in a second, then it's the right place to put it. But if it's something that does not, then there's probably a better place to put the code.

The parallax, is likely the correctly placed, since you need to track player movement.

But the stats, probably not. You are tracking the BG sprite, so after you change that BG image, just update the label?
There are those who believe that life here began out there...

Crimson Wizard

#2
Quote
Also how much of a impact on performance does too much repeats have on overall game play, am i going to run into problems doing it in such a way.

To clarify, it's not exactly too much repeats that have an impact, it's a) frequency of repeats multiplied by b) amount of work done within one repeat.


Anyway, like Cassiebsg meant, it's a good habit to only update things when it's really necessary. To do so you need to answer a question: what does my variable/label/object depend on, and only update when their dependency changes.
The example of this: you are printing "health" on the label, then you might change the label only when the health changes, within same function that changes this variable.

But note that this is not always possible or convenient to merge both variable and GUI update in one function, because it may go against the code organization. Like, you may have your person data handled in one script, and visuals and GUI handled in another. That's perfectly fine. But in such case you will have to check for the change in rep-exec.

Also sometimes you're tracking something in the game that may change anytime, like character's position. It does not necessarily change all the time, but may, so you again have to check for the changes in rep-exec.

In such circumstances, if you're only updating few things, like a label text, you may do that unconditionally, like you're already doing.

If you are updating a lot of things, and begin to worry about perfomance, the usual solution is to save the last state of an object or variable you're tracking, and only update when there's a difference between an old value and a new one.
But I must underline, this is only worth it when you're updating alot of things, or do some time-consuming data processing, like lots of math, generating dynamic sprites, and so on.

In general, point-n-click games rarely have perfomance issues, because they don't have as much to update each game frame. Today computers are very powerful and they likely "sleep" most of the time when you run an adventure game. Several characters on screen, and updating several labels won't cause anything. If they do, there's likely something wrong with the engine, maybe it's not optimized well enough on its own.

It's only worth being concerned when you are doing something unordinary, like perfoming raw drawing operations on a fullscreen dynamic sprite realtime, or doing physics simulation in script.

Also, keep in mind that your script is one thing and the game objects is another. Game itself is updated by the engine, so if you have a lot of these simultaneously, or have alot of high-resolution animations running, or something like that -- the game may become slower even if your script is mostly empty.

In the end (almost) anything could be optimized when you or your players noticed your game has slowdowns.

SMF spam blocked by CleanTalk