Jump to content

How to find an entity in the game's world


motorsep
 Share

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.

 Share

  • Recent Status Updates

    • peter_spy

      It's funny how perception changes over the years. I've been replaying Bioshock in its Remastered version, and I'm surprised how bad the level design actually is. It's just a series of abstract corridor mazes that you can't really map out in your head. There's nothing that would ground them in any kind of reality, fictional or otherwise, no sense of place people could live in. It's almost like Wolfenstein 3d with cool art deco assets.
      · 4 replies
    • STiFU

      Anyone here played Inscryption? At first, I was like "no, I am not going to play a stupid card game", but this game is so much more than that. It is so meta, full of 4th wall breaks, and feels so damn spooky and weird because of it. Absolutely recommended, if you can stand the visuals. I recommend not reading up too much on it, as that could easily spoil half the incredible surprises. Just know that what you see in the beginning is by far not everything.
      · 4 replies
    • freyk

      Tried to make a tdm advertisement commentpost at one of civvie11 youtube videos about T2. Post got marked as spam. His problem (to not discover TDM for himself),..not my problem.
      But some help of some fellow TDM yt-videocomment posters would be nice. To ask him and others, to play TDM. To get more players/creators. 
      · 5 replies
    • datiswous

      Currently Profile Information has 3 fields, these are shown in forum posts under your avatar:
      1. Gender
      2. Location
      3. Interests
      I think that it could be useful to have an extra field called "Operating system" (under location). It can be useful for tech support and to see what people use.
      Alternatively it could be a more general term, like PC system, so that you can for example state that you use an AMD gpu.
      · 2 replies
    • OrbWeaver

      Greetings fellow kids.
      · 11 replies
×
×
  • Create New...