Kid friendly strategy enemy attack

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • All,

    I'm making a basic strategy game for my kids and an working on coding my enemies to select which player to attack. I'd like to set it up so that they attack the closest player that has the most health (this way they don't get too frustrated by dying a lot and giving up). I've figured out how to find the closest hero and highest health independently, but does anyone have any thoughts on how I might go about combining the two? Thanks in advance!

    Sent from my Nexus 5X using Tapatalk

  • Hypothetically, you would find all players within a certain distance and iterate through them. As you iterate through them, you would store the player's HP (and some object ID such as a fixed value) into some variable. This variable only gets overwritten when a higher player HP is found. After you have iterated through them all, you have found the player with the highest HP within a certain distance

    • Please login to see this link.

    Please login to see this picture.

  • Thanks for the advice. I think I've gotten things mostly working. I iterate through all of the players to find those within attacking range. I then iterate through those enemies to seek for the highest HP, and store that player's fixed value in the object id of the enemy. It works, unless multiple heroes gave the same highest health. When that happens, both of them get tagged as potential targets. I assume I can use the pick one of object condition to scope it down to one of them, but it seems to pick the same player every time. Am I misunderstanding the pick one of object condition, or is there a better way?

    Sent from my Nexus 5X using Tapatalk

  • You should be able to use the "Pick one at random" conditions within the object. That being said, there should only ever be one candidate based on my understanding. After you have done all of the iteration, you should one object's fixed value stored. Even if multiple targets have the same HP, only one fixed value is saved after all of the iteration is completed. Then you scope to the object who has that given fixed value and it should target that object.

    • Please login to see this link.

    Please login to see this picture.

  • Instead of using the pick one of condition, use a foreach loop to loop through each one of your enemies. Then you can test each player with another foreach loop.

    Please login to see this link.

    This thread had a similar question. The zombies will attack the closest player if it's overlapping their line of sight area. You can modify it to check their HP values and pick the one with the highest health that's nearby.

    Custom A* Pathfinding in MMF2: Please login to see this link.
    Random Tile World Generation: Please login to see this link.

  • Thanks again fellas. The zombie example is similar, but slightly different. My game is really more like a board game, so on the enemy's turn, each one takes a turn, moving towards a hero and attacking it. I start with the nearest object iteration, like in the zombie example, checking to see if any of the heroes are reachable. However, after that I scope them down to whichever one has the highest health. It then makes its way over to that particular hero and makes its attack.

    I'm pretty satisfied with the result. The pick one of random object condition continues to confuse me. When both of my heroes share the glory of having the most health, the code manages to select the exact same hero every time. I've tested it several times before deciding to move on and finish other areas of the engine.

    If anyone has an example of the pick one of random object condition in action, I'd greatly appreciate it, since I'm not quite able to figure out how to properly use it.

    Sent from my Nexus 5X using Tapatalk

  • Oh Clark, I think I know another reason why it is choosing the same one each time.

    As I mentioned, if you are using the method I suggested, then you are update some variable with the fixed variable (or other object ID) of the object with the highest HP within a certain range.

    If it seems like it is always picking the same object, it could be because the same object is being iterated last. By what it sounds like, it sounds like you have it such that the object ID is saved when the algorithm finds an object >= than the current saved highest HP. So if you have Object A and Object B which fit the criteria you have, Object B will be ultimately selected if Object A is iterated first (since Object B will overwrite the value of Object A after Object A is iterated) and vice versa. If this is the case, then you can resolve this by changing the order in which the objects are iterated. If you are using a ForEach loop, you can change this by changing the object's ordering during runtime. Backmost objects are iterated first while frontmost objects are iterated last (or maybe vice versa).

    Do this sound like what is happening?

    • Please login to see this link.

    Please login to see this picture.

  • This sounds like this could very well be the case. I am indeed using foreach loops and storing values. In a nutshell, I'm: (1) iterating through each hero with a foreach loop and storing their HP inside of a global variable; (2) Checking whether or not each hero's HP >= to the global variable and updating it accordingly; (3) Looping through each hero to check whether or not the hero's HP is equal to the global variable; (4) Picking one of those heroes as the target. The conditions where I'm using the pick one of object look something like this (I'm at work right now so this is off the top of my head):

    On each "Hero", loop name "HighestHP"
    "Hero" HP = Global Variable
    Pick one of "Hero"

    I'll attach a picture later tonight when I'm at home. I'm unfamiliar with changing the object's ordering at runtime. How would I do that? As always, thanks for your help!

  • Ok. In this case of "Pick One of 'Hero'" it is always choosing the same object because you have already scoped the object under the ForEach loop. The condition tries to pick one object from the current list of scoped objects, but there is only one object since it is under a ForEach loop. So basically the Pick one of condition is not doing anything. If you were to use it outside of the ForEach loop, you would see random Hero being picked.

    To change the ordering of the object, use one of the actions within the order submenu of the within the object.

    • Please login to see this link.

    Please login to see this picture.

  • Ok. In this case of "Pick One of 'Hero'" it is always choosing the same object because you have already scoped the object under the ForEach loop. The condition tries to pick one object from the current list of scoped objects, but there is only one object since it is under a ForEach loop. So basically the Pick one of condition is not doing anything. If you were to use it outside of the ForEach loop, you would see random Hero being picked.

    To change the ordering of the object, use one of the actions within the order submenu of the within the object.

    Sorry I didn't get a chance to reply last night. I got it working. I had to rearrange some of the ordering of my events and replaced the ForEach loop with a standard loop and everything works like it should. I forget that the ForEach loop scopes to each object individually vs the loop, so that was throwing me off. Rookie mistake :). BTW, the tutorials on your website are very helpful. I noticed that you haven't written any in quite some time. Do you plan on writing any more? Thanks again for your help!

  • Glad to hear you got it working! Also glad to hear you say my tutorials are helpful! At one point, I was going rewrite all the PDF tutorials in Word instead of another software I was using, but other life things came up and I never got around to it. I still intend finish the last thing I was working on and continue on with a breadth of introductory knowledge, but who knows when that would be haha.

    • Please login to see this link.

    Please login to see this picture.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!