Co-op Detection

Aim: to create a trigger which only fires in single-player games. To make things interesting, we will require that the trigger fires on the first frame of the game when in single-player mode.

Build a small box outside of your map, and add the following entity to it

"classname" "CheatCommand"
"nextthink" "0.1"
"think" "W_FireLightning"

Surround this entity with a trigger_once with 1 health, and give it a target. This second entity will trigger only if we are in single player.

The core of this code is in CheatCommand. This function is usually run by the player when they use the “impulse 9” cheat. Today we are using it as a classname instead, all this means is that the function gets called on our entity at the moment it spawns. The useful properties of this function are:

  1. it does nothing in co-op
  2. it gives the entity some cells in single-player

The second half of the trick is the think function, which runs after 0.1 seconds (so on the first frame). W_FireLightning is the function called when players shoot lightning. It checks whether our entity has any cells before firing; the cheat command we ran on spawn gave the entity some if it’s single player, but none in co-op. The entity now only fires in single-player, and so we use the trigger_once to convert the entity firing lightning into a regular triggered event we can use.

The title advertises co-op detection, but we actually seem to have detected single-player instead. It’s easy to switch around though, add this entity:

"classname" "info_notnull"
"think" "SUB_UseTargets"
"nextthink" "0.2"
"targetname" "isCoop"
"target" "coopSetup"

Then alter the trigger_once from the original setup to killtarget isCoop. You should also apply the “not in deathmatch” spawnflag to this info_notnull to prevent it from detecting deathmatch as co-op.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.