More reusable trigger_counter

Like an e-mail that one sends with the attachment missing, as soon as I posted the previous article on trigger_counter I realised how to solve the conundrum I set. Lets recap what I wanted:

A hacked trigged which
a) On death fires its targets and goes non-solid/non-damageable
b) On use returns back to normal

Now, it’s easy to get part a) using the standard multi_trigger function. Why can’t we use its counterpart multi_wait to restore it to life? It’s all to do with the wait. If we don’t set a high enough wait value then the counter might reset by itself (which we’ve decided we don’t want today). One of the side effects of applying wait is that you can’t retrigger while the entity’s nextthink is in the future, and this is the one side effect that multi_wait doesn’t reset (because it’s supposed to be the think function, we’re taking a shortcut).

However, there’s a clever trick we can pull here, using an extra function called SUB_CalcMoveDone. This function resets the origin of our entity and stops its movement, happily neither of which have any effect today. It also sets nextthink to -1 and then calls the function in think1. If we load multi_wait into think1, then we get all of multi_wait‘s effects, but with a reset of the nextthink time added on, which does just what we wanted. So we can throw out our logic gate and make the trigger_counter out of two entities:

"classname" "info_notnull"
"use" "W_FireLightning"
"ammo_cells" "999999999999"
"targetname" "increase_counter"

(the same lightning shooter as last time)

"classname" "InitTrigger"
"max_health" "120"
"target" "output_counter"
"targetname" "reset_counter"
"th_die" "multi_use"
"think" "multi_wait"
"nextthink" "0.05"
"think1" "multi_wait"
"use" "SUB_CalcMoveDone"
"wait" "999999999999"

(the trigger to go around the lightning)

Eagle-eyed readers will have noticed we’re using multi_use for our death function. This makes sure that the activator is the player, and so allows us to add text messages to the trigger (previously they might be directed at the lightning entity instead, since it did the killing).

Now that the entity set-up is simple and only requires two entities (along with our new-found ability to send messages) we can restore the “Only 1 more to go…” text to our counter. To do this, we make a copy of the counter setup, reduce its health by 30, and instead of giving it a target, just give it that as a message. You can then repeat this process to make “Only 2 more…”, “Only 3…” messages and so on. You can even customise the messages! The important thing is that all these entities must have the same targetname, so that all of them are reset in one go.

Of course, if your counter has a count of 20, you may not want to create 16 separate “There are more to go…” entities to put all the messages back. Can we create a single entity which handles that message until we get close to the count total? Of course we can!

"classname" "InitTrigger"
"max_health" "90"
"targetname" "reset_counter"
"think" "multi_wait"
"nextthink" "0.05"
"think1" "multi_wait"
"use" "SUB_CalcMoveDone"
"wait" "999999999999"
"message" "There are more to go..."
"th_pain" "SUB_UseTargets"
"th_die" "SUB_Null"

The trick here is that we’ve moved SUB_UseTargets from th_die to th_pain, making our message trigger each time the lightning strikes. As it stands, the entity lacks a target, but there’s no reason we couldn’t add one back in. This gives us a counterpart entity to a counter, which acts like a trigger_relay the first n times it is used, but then stays inactive until it is reset. This might have new applications, like a button which can only be used a limited number of times before it needs recharging.

I’ve uploaded an example map demonstrating all of today’s ideas in Have fun!


2 thoughts on “More reusable trigger_counter

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.