A logical entity can be used to add functionality to your game, by communicating to other (non-)logical entities. These logical entities have to be triggered in order to do what they are supposing to do. The entity itself will not visible to the player. Logical entities can be dragged into your map in the Hammer level editor. Entities have inputs and outputs, which can be chained together to do certain things. For example: we have a trigger_proximiy entity and a game_text entity on our map. We then can chain an output of trigger_proximity, OnStartTouch, to an input of the game_text entity, like Display to show a message on the screen when we have triggered the proximity (by touching it).
OnStartTouch output is chained to the SetWeapons input of WeaponsDisabler
A logical entity can be created in our Mod as follows. First of all, all these logical entities which dynamically change the game rules are put together in one .cpp-file. This file is called maprules.cpp, so open up the solution explorer again and browse to the Source Files folder in the hl sub-project.
The best thing to do when experimenting with logical entities is trying to clone an existing entity to discover how it is implemented. A good one to clone or use as a skeleton to create your own entities is CGameText. This class corresponds to the game_text entity that we discussed earlier.
Important are the following lines:
//… LINK_ENTITY_TO_CLASS(your_entity_name, CWeaponsEnabler); BEGIN_DATADESC( CWeaponsEnabler ) DEFINE_KEYFIELD( iEnable, FIELD_INTEGER, "enableWeapons" ), DEFINE_INPUTFUNC( FIELD_VOID, "SetWeapons", InputHandler ), END_DATADESC() //…
The first line sets the "connection" between the class and the entity. The DEFINE_KEYFIELD defines that the value, which is filled in the "enableWeapon" field in the Hammer editor, will be copied into the int variable iEnable.
DEFINE_INPUTFUNC defines an input of the entity. When this input is triggered by an output of another entity, the function "InputHandler" of this class will be called. The remainder should be self-explanatory. Check CGameText or CWeaponsEnabler (Appendix B.1) for more information.
When you have finished creating or editing your entity, you also have to create a .fgd file. This file has to be loaded in the Hammer editor (Tools -> options -> Game Configurations Tab -> Game Data Files) before you can use your entity. The .fgd contains an almost identical mapping of the fields and the input of the entity. Check Appendix A.3 for an example of a .fgd file, which we created for the vu_weapons_enabler entity (this entity will be discussed in the following paragraph).
This logical entity will disable or enable weapons of the player who triggered it. This entity is almost identical to CGameText, but without all the unnecessary options. It does have a field "enableWeapons", which in fact is just a boolean. The entity will call the SetWeaponsEnabled function (defined earlier) of the CHL2MP_player class to enable or disable weapons. The player who has triggered the entity can be found by calling ToBasePlayer(pActivator).
The entity is called "WeaponsDisabler" and has a field called "Weapons enable/disable" (name of this field is defined in a .fgd file.
See Appendix A1 for the complete source code.
Earlier on we have created a customized HUD which can display pictures. Now we just need an entity to trigger it. Basically you can clone CGameText again and remove/add fields as you like, just like in the WeaponsEnabler. But to access our customized HUD from this entity, we need to adjust our HudMonitor a little.
See Appendix A2 for the complete source code.