NearestToEntity, how access variables? [SOLVED]


(John Andersson) #1

I’m using this line to see the nearest box to an entity.

var near:Entity = world.nearestToEntity("box", this);

Boxes in my game have some variables. The entity is supposed to move if the nearest box has a certain variable activated (which happens when the player touches it).

So I’d want something like this:

var near:Entity = world.nearestToEntity("box", this);
if (near.isTouched) blabla;

But that doesn’t work? I tried changing “Entity” to “Box” (that is the name of the box class), but to no avail.


(Martí Angelats i Ribera) #2

Have you done

type = "box";

inside the box class?


(Justin Wolf) #4
var near:Box = world.nearestToEntity("box", this) as Box;
if (near.isTouched) blabla;

nearestToEntity always returns an Entity regardless of what type you’re checking for. So all of your variables that are relative to your Box class will not be able to be accessed unless you cast the Entity that’s returned as a Box.


(John Andersson) #5

Any idea on how to do that? :stuck_out_tongue:


(Justin Wolf) #6

My post had all necessary code.


(Martí Angelats i Ribera) #7

I don’t know what you may not be understanding.


(B6ka) #8

You can put entities of a given type into an array and check which one is closest. For example, here is a code to get all enemies in a world and do something about them

var _allEnemies:Array = new Array(); //create an empty array

FP.world.getType(‘enemy’, _allEnemies); //put all instances of “Enemy class” into _allEnemies array

for each(var enemy:Object in _allEnemies) {

  trace(enemy.x);

}

I hope this is what you were asking


(Jacob Albano) #9

The nearestToEntity() function does this exact thing (for John’s purposes) but in a more optimized way.


(B6ka) #10

Sure, but he wants to get the nearest box to be activated. Will world.nearestToEntity() necessarily get the box that is activated?


(Zachary Lewis) #11

To make sure the code @justinwolf wrote is bulletproof, you’ll want to add a bit of validation.

// Find the nearest Entity with the type "box" to
// this Entity and attempt to cast it to a Box object.
var near:Box = world.nearestToEntity("box", this) as Box;

// Make sure the found Entity is actually a Box by checking against null.
if (near != null && near.isTouched)
{
  // The found Entity is a Box and isTouched is true.

  // Perform any desired touching logic.
  ...
}

Even with that additional validation, @B6ka is asking the right question. nearestToEntity() finds the closest Entity a specified Entity of a desired type; however, the Entity’s type property doesn’t necessarily imply a specific class.

Consider a game with entities Spike and AeroChaser, both of which have a type of harmful. Whenever the player touches either of them, the player will lose health; however, if the player touches an AeroChaser, the AeroChaser will also take damage.

The following code will result in a crash if the player touches a Spike.

var near:AeroChaser = world.nearestToEntity("harmful", this) as AeroChaser;
if (near != null && near.isTouched)
{
  takeDamage(near.contactDamage);
  near.takeDamage(contactDamage);
}

This leads to the following problem: How can I get the closest entity of a specific type? Should this be an additional argument in this family of functions?

public function nearestToEntity(type:String, e:Entity, useHitboxes:Boolean = false, entityClass:Class = null):Entity

(John Andersson) #12

Thank you all so much for this. You are lovely. Apparently I had typed I didn’t change

“near:Entity” to “near:Box”

Such a rookie mistake :stuck_out_tongue: thank you


(John Andersson) #13

Instead of creating a new thread, I might as well post in this, right?

How would I go about ignoring a specific entity of the same type I want to search for? Example, a soldier which shot a heat-seeking missile. I am setting the specific soldier in the missile code, so it knows who its creator is. Is there a way to ignore the creator as part of its search?


(Jean) #14

Set the type to another value, detect the closest soldier, set the type as "soldier" again. Should work.


(John Andersson) #15

But I can’t change the type of the soldier temporarily, a lot of code in the game depends on the player being of the “soldier” type at all times


(Jacob Albano) #16

Changing it to a new value and then back again in the same frame won’t make a difference.


(Jean) #17

The idea I had in mind was doing something like this

//remove the type
type = "";
//does the search 
world.nearestEntity(bla bla bla);
//get the type back
type = "soldier";

AFAIK, this should work right? (since it will ignore itself in the search, and AFAIK FP is single threaded, meaning no problem with multiple soldiers and doing this in the same frame)


(Jacob Albano) #18

Yes, that will work just fine.