List of actors within a range of other actor

frankvanleeuwen

  • Posts: 29
I need to have a behaviour for actor A that defines all actors of actor type B in a specific range around actor A. Best would be it adds all actor B types in that range to a list.

How do I do that?

And after having the list of all actor B types within a specific range of actor A, is it also possible to get the distance between actor A and actor B?

captaincomic

  • *
  • Posts: 6108
The distance between two actors is
sqrt(((x of actor A)-(x of actor B))^2 + (y of actor A)-(y of actor B))^2

You can use a "for each actor of type B"-block and calculate the distance between A and each actor of type B. If the distance is lower than your disired range then the actor is in range and you can do something with it or set an attribute "is within range".

Alexin

  • *
  • Posts: 3127
If you don't need the actual distances but only their relations, then you can remove the square root (computed every frame) and use the powers of the distances only (calculated once).
"Find the fun"
alexin@stencyl.com

captaincomic

  • *
  • Posts: 6108
Yes, good point. When you skip the square root, use (desired range)^2 to determinde if an actor is within range.

frankvanleeuwen

  • Posts: 29
Thanks for the replies. I'm gooing to try that, but how do I get the x and y of actor B inside the bahavior of actor A?

Alexin

  • *
  • Posts: 3127
You'll notice many blocks have dropdowns that display "self". Click it to show other options.
You''l also need to create an Actor behavior attribute to hold a reference to B.

EDIT: Corrected.

« Last Edit: November 30, 2011, 10:28:48 am by Alexin »
"Find the fun"
alexin@stencyl.com

frankvanleeuwen

  • Posts: 29
Can you explain that more detailed please? Cause thats the point I dont get. How do I need to set up a behaviour that can get the x and y coordinates of other actors?

Alexin

  • *
  • Posts: 3127
An Actor attribute. My apologies.
"Find the fun"
alexin@stencyl.com

frankvanleeuwen

  • Posts: 29
I still don't get it, cause when I use an actor attribute I can only assign it to self, last created actor, last colided actor or attribute. So I still can not assign it to actor B.

If I use the actor type attribute I can assign it to the actor B type, but then I can't use it in the 'x of ...' function.

So I still don't know how to work with the x and y of actor B in the bahaviour of actor A.

Hectate

  • *
  • Posts: 4643
I have a very detailed post the describes the exact functionality you need. Auto targeting actors that are within a specific range.

http://community.stencyl.com/index.php/topic,4486.0.html

which references the following post...

http://community.stencyl.com/index.php/topic,2673.msg18385.html#msg18385
:
:
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.

frankvanleeuwen

  • Posts: 29
Thank you very much. I will need some time to figure out how that behaviour ectually works before I will be able to use it I think, but this will help a lot!

captaincomic

  • *
  • Posts: 6108
About getting x and y of actor B in behavior of A:
When you have the "for each actor of type"-block you can use the "the actor"-block (in the palette below the "for each actor of type"-block).
So "self" would be actor A and "the actor" would be actor B (you can drag "the actor" over the "self" dropdown list for the "x of self"-block.

captaincomic

  • *
  • Posts: 6108
I see you use a different method in that post, Hectate: create a circular region around the actor and set the region to follow him, and then use the "for each actor within region"-block.

I wonder though which method would be faster? After all stencyl has to check somehow which actors are inside the region. I guess if you need the distance anyway, its easier (and maybe even faster) to check manually if distance < range (or alternatively distance^2 < range^2).

frankvanleeuwen

  • Posts: 29
Last night I implemented the method of Hectate. It works perfect! Many thanks to you!!

captaincomic, thanks for the information about getting value's from other actors. I think this option is quite difficult in the Design Mode, if you compare it with the most programming languages where you can just use something like actor_name.GetAttribute

coleislazy

  • *
  • Posts: 2607
I think this option is quite difficult in the Design Mode, if you compare it with the most programming languages where you can just use something like actor_name.GetAttribute

You can do this as well. The trick is setting an Actor attribute to the correct actor. You can use the "for each actor of type", or use actor customization (double-click your actor in a scene).