Infinite loops

Started by Sam_64 on

Topic category: Help with modding (Java Edition)

Last seen on 22:07, 29. Jan 2024
Joined Jan 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Infinite loops

I'm trying to make an infinite loop that spawns in 10 creepers every 2400 ticks. I've tried stuff like 'while true do' and also 'repeat infinite times' but the game just broke. How can I fix this?

Last seen on 20:37, 26. Jul 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
The problem with your first…
Mon, 01/29/2024 - 23:36

The problem with your first two approaches is that, by default, procedures are supposed to operate during a single tick. The game can only move to on to the next tick once everything has been finished. So if you tell a procedure to loop infinitely, or to run a while loop that cannot finish, it will lag and then crash the game from memory overload. 

What you want to do is trigger something every tick. In your case, you would need some sort of timer, (a status effect, or a player-persistent number variable that counts down every tick), and a procedure that runs on player update tick, checking each tick if the timer is finished. (And if it is, spawning the creepers and resetting the timer.) Depending on how you want the creepers to spawn, there are different ways you would do this, but the basic principle is the same.