Login
Create

Create and destroy multiple effects Last updated July 26, 2020

For this tutorial we will set up a simple scenario; A player presses Interact and spawns a bunch of effects at their feet. After 3 seconds these effects need to disappear. For the sake of simplicity, let's keep it to 1 rule.

Setting up the rule

We will create a rule set to the Ongoing - Each Player event so we can track if they press a button.
In this rule we add 1 condition that check if the player holds the Interact button.

  Is Button Held(Event Player, Interact) == True;

Next up we will need 1 variable, which we will use to store our effects in. We will be using a Player Variable for this. Name one of your variables something sensible so we can easily remember which one to use. In my case I will name it "Effects".

We will create 1 action in the rule we just created. We will set our variable to an empty array using Set Player Variable set to Empty Array.

    Event Player.Effects = Empty Array;

In essence an array is a list of values we define. We will use this array to store our effects so we can reference them again later. We set the variable to an Empty Array at first so we can properly add values to this list later on.

Creating the effects

We start off by creating a simple effect using the Create Effect action. The effect can be whatever you want. For this demo we can leave everything at it's default settings.

  Create Effect(All Players(All Teams), Cloud, White, Event Player, 1, None);

Now that we have created the effect we want to store it in our array. For this we will use Append To Array, which adds something to the end of an array. We could also use Set Player Variable At Index, but that is far less versatile and more difficult to maintain when you have a large number of effects you want to store.

We can't just use Append To Array, we first need to use Set Player Variable. Append To Array creates a new list, independent from the first. It doesn't just append a value. So instead we set our value to the new list.

    Event Player.Effects = Append To Array(Event Player.Effects, Last Created Entity);

From this point on we can copy paste our last 2 actions (Create Effect and Storing the effect in the array) to create a bunch more effects. In my case I went with 3 different effects, clouds, sparkles, and a ring. Since we used Append To Array instead of Set Player Variable At Index we don't need to edit this action, as simple copy pasting it is enough.

At this point here's what we got:

Destroying the effects

Let's start off with adding a Wait of 3 seconds to our rule. This is so we wait with destroying our effects. Your use case will likely be different.

We could destroy the effects in multiple ways. Destroy All Effects would certainly do the trick and be super easy, but what if you have other effects you don't want removed? We could create an action for every effect we created in the array and individually target them, but that would suck if we have a large number of effects.
We want our code to be versatile and easy to maintain.

For this purpose we will use a While loop. The While action continously loops and executes what's inside of it while the condition we give it is true.

First up we set up our While action with the condition of Count Of(Event Player.Effects) > 0. This essentially means "If there's anything in our array".

Inside of this While loop we will go over each Effect in our array, destroy it, and remove it from the array. To grab just 1 value in the array we will be using Last Of, which as the name implies, grabs the last values in an array.

Now that the effect is destroyed we need to remove it from the array as well. For this we will be using Remove From Array. This works very similar to Append To Array we used earlier. We set our variable to the new list that is created by Remove From Array. To determine what value to remove we once against use Last Of.

This action looks a little complicated, but go over it properly and I'm sure you'll understand! To put it simply, we set our variable to a new list with the last value removed.

And then we close the While action with End and there we have it!

    While(Count Of(Event Player.Effects) > 0);
        Destroy Effect(Last Of(Event Player.Effects));
        Event Player.Effects = Remove From Array(Event Player.Effects, Last Of(Event Player.Effects));
    End;

To put the finishing touches on this we can add another condition to this rule that makes sure we can only press the button again when the effects have been removed.

    Count Of(Event Player.Effects) == 0;

Final result


variables
{
    player:
        0: Effects
}

rule("Create effect")
{
    event
    {
        Ongoing - Each Player;
        All;
        All;
    }

    conditions
    {
        Is Button Held(Event Player, Interact) == True;
        Count Of(Event Player.Effects) == 0;
    }

    actions
    {
        Event Player.Effects = Empty Array;
        Create Effect(All Players(All Teams), Cloud, White, Event Player, 1, Visible To Position and Radius);
        Event Player.Effects = Append To Array(Event Player.Effects, Last Created Entity);
        Create Effect(All Players(All Teams), Sparkles, White, Event Player, 1, Visible To Position and Radius);
        Event Player.Effects = Append To Array(Event Player.Effects, Last Created Entity);
        Create Effect(All Players(All Teams), Ring, White, Event Player, 1, Visible To Position and Radius);
        Event Player.Effects = Append To Array(Event Player.Effects, Last Created Entity);
        Wait(3, Ignore Condition);
        While(Count Of(Event Player.Effects) > 0);
            Destroy Effect(Last Of(Event Player.Effects));
            Event Player.Effects = Remove From Array(Event Player.Effects, Last Of(Event Player.Effects));
        End;
    }
}
Elo Hell Logo_H-M-Dark
Join the Elo Hell Workshops Discord