Started by
znibsss
on
Topic category: Help with modding (Java Edition)
Hello, and it is me again! This time, with another question. How can I make my creature, znibsss, target a player even if the player is behind a wall? Or even if its far away? How can I make it so that my creature will ALWAYS be targeting a player? Yeah that's all I'm gonna ask, for now.
You can make a procedure that runs on your entity's update tick, and checks if a player exists in a desired radius. (Say, 200 blocks, to be generous.) If a player does exist in a 200 block radius, set the attack target of the event/target entity to the nearest player in a 200 block radius.
Both of these functions ('does entity exist in radius' and 'nearest entity of type in radius') can be found in the world menu. It is very important that you make sure there is an entity within the radius before you set the attack target, otherwise nearest entity can return a null entity, which will immediately crash the game.
Thank you! Also another question, how can I make it target a player as I said only if it is not already targetting something? Like not just if it is not already targetting a player, a wolf also maybe a skeleton etc.
This is actually the default; once an entity's attack target is set, it will only choose a different attack target if it is somehow reset. (Either by the original target dying/leaving range, or if the new target deals damage to an entity that has a 'fight attacker mobs back' function.) If you need to specifically stop it from targeting an entity, there are other means to do this, though they're a bit more complicated.
This isn't the default? When I did exactly that it no longer attacked the attackers and it stopped attacking whatever it was attacking and started attacking me.
It's highly dependant on the order of the tasks in the Mob AI. If it's still causing problems, or you need a more complicated system, another thing you can do is forget about attack AI tasks entirely; and just use the mob's update tick trigger to run your own custom procedure that determines what the entity should be targeting, if anything. (The 'does entity exist in radius' 'nearest entity of type' and 'set attack target of entity' functions should all be helpful for this.)
Ok how can I make it so only if my entity isn't targetting anything it will target the player?
Use an if bracket to check that the 'entity (event/target entity) is targeting for attack' is not a subtype of living entity. (Since your mob won't be targeting non-living entities, this will only return true if it has no attack target.)
If the entity is not targeting anything, use the 'does entity exist in radius' function to check if a player exists in your desired radius. If this returns true, use the 'set attack target of entity' function to target the nearest player. (Using the 'nearest entity of type' function.)
Can you send me screenshot of you doing this in a test mod because I cannot quite understand what your telling me
Ok I just did what you did, now ill make another procedure with entity attacked and I'll try to figure something out. What I'll try to do is just attack the attacker and maybe that'll work
The top procedure is an example of what you would do for the 'update tick procedure. (First checking that the
The lower procedure is something you could do using the global 'before entity hurt' procedure, (first checking that the event/target entity is your custom entity.) The way this is configured, the entity will only fight its attacker back if it isn't already targeting an entity. If you remove the if bracket and just use the inner block, it will change its attack target to whatever attacked it last.
Oh well, it worked! Thank you man you're a legend!
Also the procedure before entity hurt is like when an entity is targetting an entity? Or it is when he is gonna receive the damage? Just so I can get some more knowledge.
‘Before entity is hurt’ triggers right before the game calculates damage; which is useful, because you can use the entity, the source entity, damage type, and the amount of damage as dependencies; and can also cancel the trigger. It also works if there is no source entity, so you can use it for any sort of damage. (Though you may still need to check if the source entity exists, I’m not 100% sure)
Oh nice thank you :)