Jump to content
The Dark Mod Forums

How to find an entity in the game's world


motorsep

Recommended Posts

If you're looking for a specific entity, you can use this in a script:

 

entity e = sys.getEntity("<name of entity>");

 

For example, if you want to find the entity named "John", then use:

 

entity e = sys.getEntity("John");

Link to comment
Share on other sites

I am pretty sure that this can't be achieved using the existing script events. It can easily be done in the game code. The static gameLocal object provides public members idEntity entities* (an array of pointers to map entities) and int numEntities, so code can cycle through the list of pointers and get the classname spawnarg for each entity, filtering out those that you want. There are also linked lists available for spawned entities and active entities (those that Think()).

 

I was thinking of suggesting we add a script event to the game that'll let map scripts do the same thing: cycle through the list of extant entities. This seems as good a thread as any. Would people support that script event? You could use it to do this job, plus lots of other things. I've often found situations in scripting that would be easier to set up if the map script could cycle through the list of entities during initialisation and compile a list of entities that it'll later track or act on. You can then add more affected entities to your map without having to update your script with a hard-coded list of entity names.

Link to comment
Share on other sites

Now that I am thinking about it, script would be slow. So keeping it within SDK code would be the best thing.

 

I was looking into the code last night, and it seems that what's already there is a functionality to pull entity's index. So there is no out of the box function that only polls entities of a certain class, correct?

 

If so, how would I determine what is the class of the polled entity? (entity index 0 is always a worldspawn class, but the rest?)

Link to comment
Share on other sites

Will you have to do this often? If you are only going to search the list of entities in response to something happening in game, for example, you probably don't need to worry about the time taken. If it's going to happen every frame, then yes it's worth doing it in the sdk.

 

I'm not at home so can't check the exact method names, but look for the idEntity::GetSpawnArg() method, or something very similarly named. You can GetSpawnArg("classname") to find out an entity's game class.

Link to comment
Share on other sites

Will you have to do this often? If you are only going to search the list of entities in response to something happening in game, for example, you probably don't need to worry about the time taken. If it's going to happen every frame, then yes it's worth doing it in the sdk.

 

Yeah, probably every tick. Or every so often, but often enough that performance is important.

 

I'm not at home so can't check the exact method names, but look for the idEntity::GetSpawnArg() method, or something very similarly named. You can GetSpawnArg("classname") to find out an entity's game class.

 

Me neither :) so, no rush.. I appreciate your help.

Link to comment
Share on other sites

I've done a few tests on discovering entities in the running map.

 

The world entity turns out not to be entity 0 in the gameLocal.entities list; it's indexed at a constant 8190 (MAX_GENTITIES-2). gameLocal.num_entities holds the max index +1 of entities other than the constant ones like "world" that're stored at the end of the list. There are gaps (null pointers) in the list at positions less than num_entities.

 

You have choices over which list of entities you want to look at. So far I've spotted 4 global lists that you can access during the game:

  • gameLocal.entities
  • active (thinking) entities
  • spawned entities
  • entities defined in the map file
    • ^ Not sure whether this last one is truly accessible during the game. There's at least one way to do it ( gameLocal.GetLevelMap() ) but there's a comment saying that the method "should only be used for in-game level editing"

spawned entities excludes entities defined in the map file that haven't been spawned, e.g. those that got suppressed due to difficulty spawnargs, for example. I'm not sure whether those are included in gameLocal.entities yet.

 

Two examples from my test code for finding entities by classname, (1) from the global gameLocal.entities list and (2) from the spawned entities linked list:

 

 

// find entities by classname
idStr classToFind = "atdm:player_thief";
// 1: find entities by classname in gameLocal.entities
for ( int i = 0; i < gameLocal.num_entities; ++i )
{
idEntity* ent = gameLocal.entities[i];
if (ent != NULL && idStr::Icmp(ent->spawnArgs.GetString("classname"), classToFind) == 0 )
{
	// do something with ent
	gameLocal.Printf("\n %s found, named: %s\n", classToFind.c_str(), ent->GetName());
}
}
// 2: find entities by classname within spawned entities
for ( idEntity* ent = gameLocal.spawnedEntities.Next(); ent != NULL; ent = ent->spawnNode.Next() )
{
if ( idStr::Icmp(ent->spawnArgs.GetString("classname"), classToFind) == 0 )
{
	// do something with ent
	gameLocal.Printf("\n %s found, named: %s\n", classToFind.c_str(), ent->GetName());
}
}

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recent Status Updates

    • OrbWeaver

      Does anyone actually use the Normalise button in the Surface inspector? Even after looking at the code I'm not quite sure what it's for.
      · 4 replies
    • Ansome

      Turns out my 15th anniversary mission idea has already been done once or twice before! I've been beaten to the punch once again, but I suppose that's to be expected when there's over 170 FMs out there, eh? I'm not complaining though, I love learning new tricks and taking inspiration from past FMs. Best of luck on your own fan missions!
      · 4 replies
    • The Black Arrow

      I wanna play Doom 3, but fhDoom has much better features than dhewm3, yet fhDoom is old, outdated and probably not supported. Damn!
      Makes me think that TDM engine for Doom 3 itself would actually be perfect.
      · 6 replies
    • Petike the Taffer

      Maybe a bit of advice ? In the FM series I'm preparing, the two main characters have the given names Toby and Agnes (it's the protagonist and deuteragonist, respectively), I've been toying with the idea of giving them family names as well, since many of the FM series have named protagonists who have surnames. Toby's from a family who were usually farriers, though he eventually wound up working as a cobbler (this serves as a daylight "front" for his night time thieving). Would it make sense if the man's popularly accepted family name was Farrier ? It's an existing, though less common English surname, and it directly refers to the profession practiced by his relatives. Your suggestions ?
      · 9 replies
    • nbohr1more

      Looks like the "Reverse April Fools" releases were too well hidden. Darkfate still hasn't acknowledge all the new releases. Did you play any of the new April Fools missions?
      · 5 replies
×
×
  • Create New...