1 decade ago
by ape
Let's say I create a entity called EntityParent, then another entity that extends EntityParent called EntityChild.
When I call ig.game.getEntitiesByType(EntityParent), I get both EntityParent and EntityChild instances. I was expecting to get only EntityParent instances.
Assuming that that is normal behavior, how does one get just EntityParent instances?
1 decade ago
by dominic
Yes, that is expected behavior. getEntitiesByType()
uses JavaScript&039;s #instanceof
operator – so an instance of EntityChild
is actually of the type EntityParent
, ig.Entity
and Object
as well. I should really have made this more clear in the documentation. Sorry.
There is no built-in function that does what you want, though. You'll have to check for a certain property (that only one of those two classes has) by yourself.
Depending on how many entities you have, it would maybe also make sense to have several arrays of entities in your Game and add entities to these lists in their init()
method.
1 decade ago
by ape
I ended up sorting the array of parent entities by their id's, then grabbing the first.
I suspect this might be brittle, but from what I can see my parent entity always has the lowest numbered ID. If that fails, I'll consider detecting a unique-to-the-parent property.
For those curious, here's how I grab the first one:
function compare(a,b) {
return (a.id - b.id);
}
var parentEntities = this.getEntitiesByType(EntityParent);
this.parent = parentEntities.sort(compare)[0];
1 decade ago
by dominic
The IDs of entities are assigned in their order of creation. So if you spawn an EntityChild first and an EntityParent after that, the EntityChild will have the lower id.
If you just need to get one entity anyway, consider giving this entity a name and use the getEntityByName()
method.
1 decade ago
by ape
I actually tried using getEntityByName() first without success. Giving an entity a property of name, either in the constructor or after instantiation didn't change what ig.game.getEntityByName returned (which was always undefined).
Can you verify that it's working?
1 decade ago
by dominic
Assigning a name
after instantiation will have no effect, because it is only looked at once, during
spawnEntity()
. So either set the name as a class property (if you only ever will have one single instance of that entity) or set in the entity&
039;s #init()
method.
There was also a bug in 1.14 causing it to ignore an entity&
039;s name completely when it was not set in Weltmeister (i.e. through the #settings
object). See the
changelog. Again... sorry for that :/
1 decade ago
by ape
There was also a bug in 1.14
That explains a lot. I'm still on 1.14. I'll grab the latest and use getEntityByName().
1 decade ago
by ape
I've upgraded to 1.15 and the only time I'm able to get getEntityByName to return anything is when I spawn an entity with a settings object like so:
So this works:
ig.game.spawnEntity(EntityNpc, 24,24, {name: "first_npc"});
ig.game.getEntityByName("first_npc"); // returns what we just spawned
getEntityByName
doesn&
039;t return objects with a class property with a certain name. #getEntityByName
doesn&
039;t return objects after adding a #name
property to the
settings
object within an entity&
039;s #init
method.
I don&
039;t mind class properties not playing nice, but setting the name within init would be quite nice. Especially since a lot of my NPCs are loaded as part of a level and all the entity spawning is happening deep within #loadLevel
.
Could you show me a working example of setting the name within
init
just in case I'm doing it wrong?
1 decade ago
by dominic
Are you sure you are using the 1.15 sources? It sounds like you&
039;re still having the bug from 1.14. Please check if your #lib/impact/game.js
file reads the following on lines 86-91:
var ent = new (entityClass)( x, y, settings || {} );
this.entities.push( ent );
if( ent.name ) {
this.namedEntities[ent.name] = ent;
}
return ent;
There's not much to do wrong when setting the name. Something like this for your entity should be perfectly fine:
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.name = 'first_npc';
},
1 decade ago
by ape
Well, I thought I had the 1.15 source, but I had a git configuration issue and hadn't pulled the latest game.js for some reason. I'm up to date now and all is well.
Page 1 of 1
« first
« previous
next ›
last »