Accessing other entities:zn-cn

From Valve Developer Community
Revision as of 09:39, 29 August 2017 by Kalenclark (talk | contribs)
Jump to: navigation, search
English Deutsch

Hammer 中,实体 仅能通过已经定义好的 输入与输出 来交流。有一个好消息是,在C++里,这个限制没有了并且你可以访问一切没有明确说明是“私有”的东西。一个坏消息是你必须再费力一点来顺利开始。


CBreakable* pWall = GetFuncBreakable(); // 预制函数!
if (!pWall) return;



  1. 我们声明了一个指针并且把一个实体赋值给它(一个 func_breakable 实体)
  2. 确认该指针已经成功地被赋值
  3. 访问一个目标实体的成员函数
Tip.png Tip: 给指针名加上前缀“p”使得后面识别它们更加容易,但并非必须这样做。




  1. Pointers are declared like a variable, except for an asterisk before the name (e.g. CBaseEntity* pOther). Don't use the asterisk anywhere else - it's just for declarations.
  2. You must #include the header file of any class you want to create a pointer for.
  3. A pointer can only be assigned an object that matches its class. The compiler will throw an error otherwise.
  4. When accessing members of a pointee, you must use -> instead of the usual period character.
  5. Any attempt to access an unassigned ('null') pointer will immediately crash your mod. Welcome to C++!

These aside, the syntax surrounding a pointer is the same as any other member of the current class.

Tip.png Tip: You can quickly create a pointer for use as a function argument with &. SomeFunc(&MyVar) is the same as SomeType* pMyVar = MyVar; SomeFunc(pMyVar).


Pointees must be of the same class as the pointer. But what about touch functions, which provide the other entity involved as CBaseEntity*? There's no such thing as a pure CBaseEntity, right?

Indeed there isn't. The pointee isn't CBaseEntity, but the pointer has been "cast" to act as if it were one. This is possible when one class inherits from another; since everything inherits from CBaseEntity everything can be cast to CBaseEntity*. Essential knowledge if you don't know what entities your code will be dealing with!

A CBaseEntity* pointer is fine if you want to access something defined in CBaseEntity. But if you need to access something defined further downstream, then the pointer must be cast to a class that has access to it. This is achieved with dynamic_cast.

CMyEntity::Touch( CBaseEntity* pOther )
	CBreakable* pWall = dynamic_cast<CBreakable*>(pOther);
	if (!pWall) return;


We cast pOther to CBreakable*, storing the result in pWall. We can then access the CBreakable function Break().

It's doubly quadruply important to check that the pointer is assigned this time because the cast will be attempted whenever the entity touches something. If pOther isn't actually a CBreakable then the operation will fail, leading to a dangerous null pointer.

Note.png Note: Remember that casting a pointer does not affect the pointee itself.
Tip.png Tip: If the classes of both objects are known, you can perform a time saving static cast like this: CBreakable* pWall = static_cast<CBreakable*>(pOther).


Pointers represent physical locations in system memory. If you want to transfer a pointer between the client and server or store it in a saved game, you must use a CHandle (aka EHANDLE).

Finding entities

There isn't much point in knowing how to create pointers if you don't have an entity to work with in the first place. gEntList is the place to get one. It's a global object available through cbase.h.

gEntList offers various search functions, the names of which all start with Find or Next. Each one will only return one CBaseEntity* at a time however, so if you want to iterate through all the results a bit of padding is needed:

CBaseEntity* pResult = gEntList.FindEntityByClassname(NULL,"npc_*");
while (pResult)
	CAI_BaseNPC* pNPC = dynamic_cast<CAI_BaseNPC*>(pResult);
	if (pNPC)

	pResult = gEntList.FindEntityByClassname(pResult,"npc_*");

Here we:

  1. Call gEntList.FindEntityByClassname() to get the first entity with the Hammer classname "npc_*".
    Note.png Note: The asterisk is a search wildcard in this context.
  2. Enter a loop that ends when pResult becomes invalid.
  3. Cast our CBaseEntity*, pResult, to CAI_BaseNPC* so that we can access its AI-related members.
  4. Confirm that the cast has been successful.
  5. Perform our desired operation.
  6. Call FindEntityByClassname() and search for "npc_*" again, this time starting at pResult's position in the list.
  7. Return to the start of the loop.

Searching the whole entity list all the time is expensive so try to avoid doing it if you can. For example if your class will need to access the other entity again later, keep it stored away in a member pointer.

See also