Jump to content

More efficient entity timers? [SOLVED]


RInventor7

Recommended Posts

Hi! I have a custom entity and I wish it would run some logic (e.g. play some sound) in every 2 seconds and some other logic in every half a second. For that I’m at the moment using 2 different timers. In entity baseTick function (runs every tick, 20 times per second):

entity.getPersistentData().putInt("timer1", entity.getPersistentData().getInt("timer1") + 1);
if (entity.getPersistentData().getInt("timer1") >= 40) {// evry 2 seconds
	entity.getPersistentData().putInt("timer1", 0);
}
if (entity.getPersistentData().getInt("timer1") == 1) {
	//Run logic 1
}

Then I have second timer for the half second logic. Since both timers logic will run on every second it’s not very efficient. And it gets worse as I add more timers.

Is there a way to replace this timer with something else that would be more efficient? Thanks. 

Edited by RInventor7
Link to comment
Share on other sites

5 hours ago, RInventor7 said:

Hi! I have a custom entity and I wish it would run some logic (e.g. play some sound) in every 2 seconds and some other logic in every half a second.

Just use one timer and check for every 10 ticks (0.5s) and every 40 ticks (2s). Unless there is something that would desync the timers, it makes no sense to do otherwise.

Additionally, just use a field on the entity itself, don't read/write from persistent data every tick.

Link to comment
Share on other sites

  • RInventor7 changed the title to More efficient entity timers? [SOLVED]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.