Skip to content

Creating a Time-Based Effect

A Timed Effect is an effect that runs for a specific amount of time. For this example, we will be creating an effect that will invert the player’s controls for a specific amount of time.

Inside of the "CC Effects" folder, create a class called "CCInvertControls". Open up the class. Make sure we are using the WarpWorld.CrowdControl and CrowdControlSampleGame namespaces. The initial class should look like this:

using WarpWorld.CrowdControl;
using CrowdControlSampleGame;

public class CCInvertControls : CCEffectTimed {
    protected override EffectResult OnStartEffect(CCEffectInstanceTimed effectInstance) {
    }

    protected override bool OnStopEffect(CCEffectInstanceTimed effectInstance, bool force) {
    }

    protected override void OnPauseEffect() {
    }

    protected override void OnResumeEffect() {
    }

    protected override void OnResetEffect() {
    }

    protected override bool RunningCondition() {
    }

}

A class that inherits from CCEffectTimed has additional functions that may be overriden:

  • OnStartEffect: Ran when the effect starts.
  • OnStopEffect: Ran when the effect ends, either forcefully or by the timer expiring
  • OnPauseEffect: When the effect is paused. The timer becomes halted
  • OnResumeEffect: Resuming an effect after it's been paused. The timer resumes where it left off.
  • OnResetEFfect: The effect's timer gets reset to the beginning.
  • RunningCondition: Whether this effect can be ran or not.

For OnStartEffect, we want to invert the player's controls when the time begins. So we'll add this code into the OnStartEffect function:

if (Player.Instance.SetInvertControlState(true)) {
    return EffectResult.Success;
}

return EffectResult.Retry;

SetInvertControlState accepts a boolean as an argument to whether to turn on or off the inverted control state. The function also returns true if the change was successful or not.

For OnStopEffect, we want to change the player's movement state back to normal. We will also return whether the attempt to stop the effect was a success or not. Add this line into the OnStopEffect function:

return Player.Instance.SetInvertControlState(false);

Next, we want to take in account when the effect pauses or not. Like with the two previous effects, we will add the following line of code, but to RunningCondition instead.

If this returns false, the effect will automatically pause. If it becomes true while it's paused, the effect will resume.

return !Player.Instance.Dead;

Adding anything to OnPauseEffect is not necessary in this case, but we want to add a line of code to OnResumeEffect in case the player instance changed properties while it was paused.

Player.Instance.SetInvertControlState(true);

We should also add this line to OnResetEffect, to ensure we have the desired effect once the effect is reset. The final code should look like this:

using WarpWorld.CrowdControl;
using CrowdControlSampleGame;

public class CCInvertControls : CCEffectTimed {
	protected override EffectResult OnStartEffect(CCEffectInstanceTimed effectInstance) {
		if (Player.Instance.SetInvertControlState(true)) {
			return EffectResult.Success;
		}
	return EffectResult.Retry;
	}

	protected override bool OnStopEffect(CCEffectInstanceTimed effectInstance, bool force) {
		return Player.Instance.SetInvertControlState(false);
	}

	protected override void OnPauseEffect() {
	}

	protected override void OnResumeEffect() {
		Player.Instance.SetInvertControlState(true);
	}

	protected override void OnResetEffect() {
	}

	protected override bool RunningCondition() {
		return !Player.Instance.Dead;
	}
}

Now that we’ve completed our timer function, click on the Crowd Control Manager Prefab and expand it.

Click on "CC Effects" and attach CCInvertControls to it. The new class display is identical to the first effect, but it has an additional property for timed classes.

  • Duration: How long this effect should last

Name the effect "Invert Controls" and fill out the other fields appropriately.

Alt

Order the effect after starting the session and loading up the menu. Selecting the effect will begin the timer.

If it's chosen again while the timer is running, the second effect instance will be queued until the first one has completed, then run it right afterwards. While playing the game, you can see the timer pause and resume automatically when the player dies and when a new game starts, respectively.

Congratulations! You've just added an effect that gives the player options to send through.