Started by
potatomancer
on
Topic category: Help with Minecraft modding (Java Edition)
im trying to make a ranged item that has short range projectiles and shoot constantly while active enabled. i have tried a wait __ ticks then on server side on server side do despawn entity, but this causes the game to pretty much die (all mobs despawn and new chunks struggle to load). i have tried adding a cooldown to the ranged item, but i cant have the game run and the ranged item fire fast enough at the same time. is there any way to set a timer thats less asking of the game performance? (turns out it wasn't a lag issue, i just had the wrong entity type so the game was trying to despawn the player, which it can't do)
Edited by potatomancer on Mon, 10/13/2025 - 00:11
You could probably do an nbt number tag on the immediate source entity (the projectile itself) which goes up by 1 on each projectile flying tick, and despawns the immediate source entity once it reaches a certain number (greater than or equal to 20 for example, to make it last for a second).
This works a lot better than the wait _ ticks procedure block, because that procedure block really doesn't work at all for what you are trying to do. (It has a few very specific use cases, but generally, you want to use an alternative timer method, like the one I described)
i havent used nbt blocks before, so im not sure if im doing something wrong, but i got the same results as the previous code. heres the generated code
I would use an if/else block instead of a while block, using the same condition of lifespan <= 2, with the nbt increase inside the if do part, and the despawn in the else do part. The way the projectile flying tick trigger works is it calls it 20 times per second, and the nbt will stay between those times, so the first time the procedure triggers, it will set lifespawn to 1, the second time will add 1 and bring it to 2, and the third time will no longer be <= 2, so it will call the else condition and despawn the immediate source entity.
It would probably look something like (but not exactly like) this:
If nbt number lifespan of immediate source entity <= 2
Do set lifespan of immediate source entity to lifespawn of immediate source entity + 1
Else Do despawn immediate source entity
i tried your new suggestion but got similar results. i understand you specified the code is not exact, but i didn't know that else to add or remove from it. i think all the logic checks out and it would work for a single shot ranged item, but i need it to be lighter on processing power.
heres the code i have right now
I just double checked the code and realized that you are using event/target entity instead of immediate source entity (you can find it under Minecraft Components and drag it over to replace event/target entity in all of the procedure blocks) and swapping it to immediate source entity should help things. Event/target entity is the player, instead of the projectile, which means that it wouldn't be limiting the lifespan of the projectile, only trying to despawn the player, which doesn't work. So that could be part of the problem.
I know you are saying that it might be too complex a procedure to run, but the current version really is extremely lightweight, and I don't really see it causing noticeable lag. Unless you are running it on literally hundreds of projectiles, in which case the sheer number of projectiles could also be a contributing factor for lag regardless of whether or not it is running a procedure. And even then it wouldn't be the game-breaking level of lag that you said was occurring with the initial procedure you made.
My guess is that there is something else causing the lag. Are you running a custom procedure to fire the projectiles? If so, there is a possibility that it is causing some of the lag. If you could share the generated code and how you are triggering it, I could try to figure out if it would be causing the game-breaking lag, because I really don't think that the procedure to despawn the projectile, running every tick on multiple projectiles, should be causing that lag... Unless it is related to attempting to despawn the player (event/target entity), I really haven't experimented with that enough to know if it would cause game-breaking lag, or disconnect you from the internal server (which could maybe cause what you were experiencing), or anything like that.
just switched the entity blocks and it works! thanks for your help!
I'm glad I was able to help.