X and Y positions of Enemy group[Solved]

chaosoul

  • *
  • Posts: 260
Right now I have a behavior that detects when a single enemy comes within 100 pixels of my actor (a tower) and then turns towards them. The problem with this behavior is it only takes works with one type of enemy and I don't know how to change that besides duplicating the bahavior and changing a few attributes. I also want it so it targets the closest enemy actor possible because currently it targets a single actor until it dies and won't target anything else.


*Note the xswordman and yswordman are global game attributes which are linked to the x and y positions of enemy actor

« Last Edit: August 14, 2011, 04:24:11 pm by chaosoul »
COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESS

Hectate

  • *
  • Posts: 4643
I've got such a behavior, but in my case it checks for actors within a region and then checks if hey ae part of my collision group "enemies".
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

chaosoul

  • *
  • Posts: 260
Hmm that sounds like it could work also but I'm worried about lag won't that many regions on the scene at once lag?
COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESS

Hectate

  • *
  • Posts: 4643
I don't know yet, I've not scaled it up to multiple actors at this time. It's not too hard to do, if you need help though, just let me know. One thing - there's no create circular region block yet so you'd need to put in a code block with the below line - be sure to use numbers inside the parenthesis or at least number attributes.

createCircularRegion(locationX, locationY, radius);
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

chaosoul

  • *
  • Posts: 260
I'm sorry my coding knowledge is -4 So please give me some more detail so I copy and paste the code into a code block which is the one where its Code:text or is it the next one where theres a huge box some yellow and a #1. As for the numbers do I replace the x and the y in the code block is that what you mean? Thanks for all the help you've given so far Hectate
COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESS

Sunflower

  • Posts: 591
Since there are no circular regions, I would give up the idea of regions, anyway, and base on distances and such. (maybe that's because I'm not really experienced with regions, but don't tell anyone!)

First of all, how to count the actual distance between the tower and the enemy:
Dist = sqrt[[XDist*XDist] + [YDist*YDist]]
You can find sqrt block in Numbers&Text >> Exponents. I would recommend making a custom block for that, actually, so you enter only actors as arguments and it counts the distance between their coordinates. This block would look something like that:
Code: [Select]
distance between [Self] and [Other] //both arguments are actors
  set XDist to [ [X of [Self]] - [X of [Other]] ]
  set YDist to [ [Y of [Self]] - [Y of [Other]] ]
  return [ sqrt[[XDist*XDist] + [YDist*YDist]] ]

Now then, make two attributes: "Min Distance" (number) and "Closest Actor" (actor). Then:
Code: [Select]
set Min Distance to [Range] //or 100
set Closest Actor to [Self] //it'll be needed for other checks
for each actor of group [Enemy Group]
  if <[distance between [Self] and [member of group]] < [Min Distance]>
    set Min Distance to [distance between [Self] and [member of group]]
    set Closest Actor to [member of group]
This way you can determine which actor is the closest one. If no actor is within radius of swordsman, the "Closest Actor" will remain "Self" (so yes, we not only have finding the closest actor, but also determining if it's within the range at all!). If you want to refer to the closest actor later, don't forget to check if it's not Self!
(and don't even try setting "Min Distance" to "distance between [Self] and [Self]", for quite obvious reasons ;) )

Now, rotating:
Code: [Select]
if <[Closest Actor] not= [Self]>
  point [Self] towards [/*count the angle between Self and Closest Actor here*/]

I hope that it's helpful. ;)

Hectate

  • *
  • Posts: 4643
Sunflower: I use both region and distance in my behavior to get any enemy actors within range (the region) and also to target the currently closest member of that group.

chaosoul: I'll try to upload an image of the behavior for you with an explenation. I'm on my phone at the moment, so it won't be right away.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

chaosoul

  • *
  • Posts: 260
Sunflower:SO MUCH CODING CANNOT COMPUTE! *Activate self destruct after eating a cookie*
I'm sorry I don't quite understand it very clearly. I've never made a custom block but seems easy enough is the return type a number? The rest after the whole sqrt block thing I get but can I get more help on the sqrt block. Umm also do I put the below blocks in the when created or always part?
If this works out I might use this depending which one lags less Hectate's or Sunflower's. Either way I like to see that behavior Hectate Hope its userfriendly as I hope to make a tower defense kit out of this.

« Last Edit: August 13, 2011, 08:27:38 am by chaosoul »
COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESS

Sunflower

  • Posts: 591
Yes, you should create custom block with two actor arguments (from which one is added by default), and return type of number. Then you put there the blocks I wrote there. "sqrt" is square root, and the whole formula is actually Pythagorean theorem, and in specific can be used to count distance between two points.

And where should you put these blocks? First, use "Create Custom Block" thing, then input all the necessary parameters (including additional "Actor" argument and return type of Number), and move all these pieces into this "Custom Block" thing. In fact, custom blocks are like message blocks, but more advanced (because you can define arguments and return types).

Generally, these "code" things, which are representations of design blocks, should be put:
1st one: in custom block (this "distance between [Self] and [Other]" is a design mode description, you should put it there as "distance between %0 and %1", after adding another actor argument as %1)
2nd one and 3rd one: in always block, 3rd after 2nd.

---EDIT---
Anyway, I've made a behavior and shared it on Forge as "Rotating Tower". I hope you can figure things out from that!

« Last Edit: August 13, 2011, 10:41:39 am by Sunflower »

Hectate

  • *
  • Posts: 4643
OK, I've attached an image of the behavior. Give me a moment to type up an explanation.

Edit: Here we go.

The two main sections that you'd be interested in are labeled A and B.

Section A: This section is important because it removes a target when it leaves the range. If you don't do this, your target can be "out of range" but your tower is still focused on it. It basically checks if either the "Target" attribute has value (have we given it a target yet?) and removes it as a target if the target 1. Dies, 2. Leaves the Range, or 3. is "dead" but still exists (if a corpse animation is playing in my case - we don't want to target a dead body ;) )

Section B: This section actually SETS the target. So, for each actor inside my range region, that is not "dead" (a corpse), and IS part of the group "Enemies" - we check the distance. If it's the FIRST enemy it runs across, it becomes the target - but if there's later a second enemy actor within the region that has a distance that is less - that becomes the target instead.

After that, everything else is just shooting bullets at the current target based on my timer.

« Last Edit: August 13, 2011, 11:11:17 am by Hectate »
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

chaosoul

  • *
  • Posts: 260
IS this okay Sunflower I cant seem to do the <[ ]not=[ ]> Since self and closest actor arent numbers but I think I got everything else right please correct if not. Hectate thanks for showing I'll see later plus explanation I gotta go soon.

COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESS

Sunflower

  • Posts: 591
Uh, well, have you seen the behavior I uploaded to Forge? ^^' I mean, "Rotate Tower" one. It contains the correct version (for example, "for each member of [Group]" part and so).

And this <[]not=[]> thing for every type was added in 1.1 preview. If you don't want to change yet, you can use <not<[ anything ]=[ anything ]>>

chaosoul

  • *
  • Posts: 260
I'll take a peek later but I'm kinda busy right now so maybe at night. Do I need to update to 1.1 to use your behavior? Hectate I think your's would work great but I'm not sure yet as I have no time to test it. I'm most likely to use the more user friendly one as I said I'm going to use this to make a tower defense kit.
COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESS

Sunflower

  • Posts: 591
No, you don't need 1.1. In fact, in my behavior I used <not<[]=[]>> as well, because I want to wait for the official release. ^^'

chaosoul

  • *
  • Posts: 260
I've checked your behavior Sunflower looks pretty nice and simple though I'm having a bit of problems with the origin point though I'm sure I can work that out later. I'm now trying out Hectate's behavior though I'm not sure what you reference by <get actor value dead for target=1> since I don't have this actor value. I'm also not sure what you meant by the custom block in part B since you didn't tell me the preferences of the custom block. So far this is what I got of Hectate's though I am using sunflower's custom block so thats probly making the difference. Sunflower I dont understand the whole draw circle as radius I mean I set the coordinates to x of self and y of self yet it doesn't work very well
COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESS