I’ve been programming for around 15 years as a hobby, and over the years I’ve picked up a lot of useful things that can be applied to your workshop rules to make your work flow better and more organized, and help reduce bugs in your workshop code.
How and when workshop rules are triggered
(Click to expand)
The workshop rules function in a specific way, so the first step to have your code work the way you want it to is to understand these things:
- A rule will only perform the actions one time when all conditions become true, and will not activate again unless at least 1 condition has at some point been false.
For example, if you have an “ongoing - each player” rule with the condition “is alive(event player)”, it will NOT trigger over and over as long as the player is alive. It will only activate the moment the player becomes alive, and after that it will do nothing until the player at some point has been dead since the last time the rule activated.
However, if you have more than one condition, only at least one of them has to have been false at some point, so you can combine “is alive” with “is firing primary” and it will still activate every time the player fires their weapon, even tho “is alive” hasn’t changed.
- A rule will not activate a second time if it is still performing actions, even if the conditions are met.
For example, if you have a wait action in your rule, it will prevent the rule from activating a second time until the wait is over.
Import code: 58F9A
(demonstration of how the wait action prevents rule activation)
This is why the “wait” action can be dangerous to use if you want a player action to trigger a buff or debuff for a certain duration.
Import code: 1M7RA
(Incorrect way using “wait” VS Correct way using a timer variable)
But also, this means that you can put the wait action at the very end of a rule as an easy way to put it on cooldown!
Now that we know the fundamentals of how rules are triggered, we will have less unexpected behaviors and bugs in our custom modes!
General tips about variables:
Tip 1 - Use variables to keep track of things you change
Use variables to store every value you change, unless there is a built-in check where you can get the information
For example, if you kill a player, you do not need to store the number of living players in a variable, because there is a built-in “number of living players” number you can check.
But, if you change the player’s movement speed, their gravity, aim speed, or any other value that you can only set, but not get the value for… Store your changes in a variable! The reason for this is that as your custom game mode grows, you will realize that you might want to change a value from different places in your code, but then you’ll have no way to check if it was already changed by something else.
But isn’t that a waste of variables? We only have a limited amount!
No, you can use a single variable for this by “setting variable at index”, which allows you to store multiple values in a single variable (array). Just make sure you note down what each index means in a text document (for example, “A5 = player gravity”).
Import code: T5649
(setting movement speed to 150 or 200, but only if you won’t overwrite it with a lower speed)
Import code 2: K2TAM
(same as above, but with the timer bug fixed. I didn’t want to over complicate the example above, but still want to show how it was fixed)
Tip 2 - Using variables as triggers
Use player variables to trigger a set of actions
This is a bit situational, but instead of putting all your actions in a rule with a set of complicated conditions, just set a variable to true in that rule, and create a 2nd rule where you place all the actions that actually do something. This allows you to easily trigger the set of actions from anywhere in your code, which allows you to re-use it easily, and keeps things clean and tidy.
Import code: HF58S
(Frost nova and burn jump skills, using the method explained above to trigger them)
Tip 3 - Toggling variables
An easy way to toggle variables
I’ve seen it be done like this:
But you can toggle it between true/false with one action by setting it to “not itself”:
Similarly, you can toggle a number between 0 and another number like this:
Import code: 19ZYK
(example of efficient toggling of variables)
Tip 4 - Write down what each variable does
Create a text document for your workshop creation and note down what you’re using each variable for
This may seem trivial, but it helps a lot when it comes to keeping your game mode organized, and makes it easier to use arrays without eventually forgetting what the heck you’re using each index for.
Tip 5 - Arrays VS Variables
First of all, what’s the difference between variables and arrays?
variables contain a single value:
A = 1
B = false
C = Hanzo
Arrays can contain multiple values:
A,0 = 1
A,1 = false
A,2 = Hanzo
As you can see, you’re storing the same amount of data when using an array, while using up less of the limited amount of variables we have access to, which makes arrays favorable to use.
However, Variables have some advantages; There are some actions available for variables not available to values in an array, such as “chasing” a variable (changing it over time).
So as a conclusion, use arrays whenever you can, and follow tip 4 to write down what you’re using each index for! But if you need to count time or use an action only available to variables, you might find variables the better option.
I might update this in the future with more tips and tricks! Let me know if there’s anything in particular you’re wondering about