1 decade ago by orecrush
I have noticed quite a few threads wondering why getEntityByName always seems to return undefined.
I recently just went through this frustration when I needed to get an entity by name inside of my player entity class. The problem was that I was doing it in the init function. The entity always returned undefined even though when I console.logged the namedEntities variable I could see my entity in the list.
I didn't actually trace back the namedEntities process but I am guessing that since I had put the code in my init function that Impact wasn't done populating that object and so was still being called by a separate function. In my scenario I needed to get a entity to warp my player to a location when he changed levels. The solution was to put it in the update function instead since all of the entities had been instantiated.
Long story short: don't expect getEntityByName to work inside an entities' init function or for namedEntities to be 100% accurate.
I recently just went through this frustration when I needed to get an entity by name inside of my player entity class. The problem was that I was doing it in the init function. The entity always returned undefined even though when I console.logged the namedEntities variable I could see my entity in the list.
I didn't actually trace back the namedEntities process but I am guessing that since I had put the code in my init function that Impact wasn't done populating that object and so was still being called by a separate function. In my scenario I needed to get a entity to warp my player to a location when he changed levels. The solution was to put it in the update function instead since all of the entities had been instantiated.
Long story short: don't expect getEntityByName to work inside an entities' init function or for namedEntities to be 100% accurate.