Tutorial: Random entity triggering Last edited 6 months ago2024-03-12 13:05:56 UTC

Download attachment

Random events are something that may be tricky to create in Goldsource. Of course, you can use timed events to make things seem random, but it's time consuming, and ultimately isn't the same as a real random event. Fortunately there's a trick that allows you to do proper random triggers without any custom code required! This tutorial will explain how to do it, and suggest some uses for the technique.

The Entity Setup

The basic components you need are as follows:
  1. An env_beam entity
  2. A number of info_target entities
  3. A func_button to go along with each info_target
An env_beam entity has a flag called "Random strike" which is what we will be using to achieve the random effect. When this flag is enabled, the beam will, as you would expect, randomly strike the targets.
Editor note
The above information is incorrect. "Random strike" flag randomizes the time of the strike interval before the next "Strike again time".
The env_beam entity always randomizes its start and end entities when more than one entity share the given name. The way to disable this is to just make only one entity have that name.

For the rest of this page instances of this error will be struck over and corrections underlined.
The trick happens in the placement of the entities. An env_beam can be set up to deal damage, and a func_button can be set up to trigger when it is damaged. See where this is going?

Knowing this, the set up is fairly simple. All that needs to be done is to place the func_buttons in the path between the two info_targets that make up the start and end of the env_beam's path.

Take the following steps: This will make env_beam damage the button, which will activate it. Then all you have to do is set up the button to trigger whatever you want it to do!

Screenshots

This is the entity set up:
2D view top2D view top
2D view side2D view side
And here is a 3D image with explanation:
3D view illustrated3D view illustrated
The most important thing here is the "Random strike" flag on the env_beam. It allows it to randomly pick the ending target of the beam.

For example, say there were 4 ending targets for the beam, each target would be decided randomly each time the beam strikes. Of course, you can have as many ending targets as you want!

Take a look at the properties on the env_beam:
env_beam flag propertiesenv_beam flag properties
This is the flags tab, and as we see, the main flag we need, "Random strike", has been turned ON.
Also, in this case, env_beam is set to be ON at all times, but you can toggle it however you want as you normally would.
env_beam class propertiesenv_beam class properties
As we see here, the beam's start end targets are defined as 00bstart00 and 00bend00 (which are the names of the info_targets). The damage is also set to 999999 (be safe and set this value high, as lower values sometimes may not work). The "Life" and "Strike again time" values are set as well.

The "Strike again time" value is the time between each strike of the beam, and the "Life" value is how long each strike lasts (no more than 0.1 seconds is needed for this method).

If you wanted your events to trigger very quickly, you could reduce the time for the "Strike again time" value. Along the same lines, increasing the "Strike again time" value will make your events trigger more slowly.
The "Random strike" flag randomizes the strike again time for each interval, between 0 and the given value.

You can also take advantage of the "Reset time" property on the func_buttons - meaning that you can make sure that the random beam doesn't trigger your button too quickly.

Summary

The env_beam entity uses the "Random strike" flag chooses its targets at random if they're all given the same name, to achieve random activation of buttons, which then go on to trigger another entity. You can control the speed of the events with the "Strike again time" on the beam, as well as the "Reset time" on the buttons.

This technique can be used to trigger anything that a button can trigger. One example is randomly swapping teleport locations in a map, like in this example map.

If you're still confused, the example map contains everything you need to know to find out how to use this setup!

10 Comments

Commented 12 years ago2011-10-11 19:23:58 UTC Comment #101034
Note that while the content of this tutorial was written by Stojke, I have HEAVILY edited the text to make it more readable. I'd fix the typo in the 3D screenshot (it says env_laser instead of env_beam), but I'm too lazy.
Commented 12 years ago2011-10-11 19:29:25 UTC Comment #101033
Thanks Penguinman, it is much better now that you have edited it. I will try to be more better next time.
Commented 12 years ago2011-10-13 06:17:50 UTC Comment #101035
VERY cool sir! Great work! =)
Commented 12 years ago2011-10-13 17:51:35 UTC Comment #101036
Thanks! Err, it could have gone better, but lucky us Penguinboy is here to help. :D
Commented 12 years ago2011-10-30 19:09:44 UTC Comment #101037
But I taught you how to do this D:
Commented 12 years ago2011-11-02 16:24:41 UTC Comment #101038
Excellent!
Commented 12 years ago2011-11-26 11:20:03 UTC Comment #101039
Good job!
Commented 12 years ago2011-12-29 06:54:01 UTC Comment #101040
Just one question: Do the info_targets need a name? Do they have to be all named the same? Or a different name each? Or no name at all? I always wondered this when someone talked about this method.
Commented 12 years ago2012-01-08 15:54:24 UTC Comment #101041
They all need to have a same name.

The central info target has one name (starting entity)
The outer info targets need to have the same name (ending entity)

So, you have 2 entity names, start and end, where all ends are named the same. Thats how the program choses randomly.
Commented 10 months ago2023-12-05 23:42:37 UTC Comment #105702
Instead of manually placing all these entities, I have written an automation tool named MESS that can generate this entity setup from a single mtl_trigger_random template entity. It also uses less entities than the setup in this tutorial by giving the func_buttons carefully placed origin brushes, so there is no need for info_targets.

Note that the 'Random strike' flag is not required for random targeting - it randomizes the time between strikes.

You must log in to post a comment. You can login or register a new account.