Skip to content

Creating Our First Effect

We're going to create an effect that forces the player to jump when a stream viewer activates it.

  1. Navigate to the WarpWorld -> Scripts folder.
  2. Inside, create a new folder named CC Effects.
  3. Within the CC Effects folder, create a new class and name it CCJump.

Alt

  1. Open the CCJump class.
  2. Ensure that the class inherits from CCEffect.
  3. Import the following namespaces at the top:
  • WarpWorld.CrowdControl
  • CrowdControlSampleGame
cs
using WarpWorld.CrowdControl;
using CrowdControlSampleGame;

public class CCJump : CCEffect {

}

CCEffect serves as a foundational Crowd Control effect. Its primary role is to trigger a specific action when a viewer submits a request for that effect. While there are various other Crowd Control effect classes to explore, we'll delve into those later.

For now, let's proceed by adding a function to the CCJump class. This function will execute whenever the effect is triggered by a viewer.

cs
EffectResult OnTriggerEffect(CCEffectInstance effectInstance) {

}

OnTriggerEffect is a function that is responsible for activating the desired functionality when this effect is received.

In this case, we want to make the player jump. Fortunately, the player is a singleton instance that has a function to make it jump that we can call from right here. You can open up the Player class to understand the CCJump function. Our code would look like:

cs
protected override EffectResult OnTriggerEffect(CCEffectInstance effectInstance) {
    Player.Instance.CCJump();
}

However, we must return an EffectResult. This is sent back to the Crowd Control server to inform it of how the game handled the state. The following EffectResult types are:

  • Success: The effect was triggered successfully.
  • Retry: We're unable to execute the effect at this moment, so put the effect back to the end of the queue and try again when it's at the front of the queue again.
  • Failure: The effect failed to execute, so let's refund the viewer's coins.
  • Unavailable: The effect is no longer available for execution. Since there are conditions where the player is unable to jump, like if the player's already in the air, we can check the bool value returned from Player.Instance.CCJump(), which returns true if a jump was successful. Using this information, we can correctly tell the server whether the jump was executed properly or not.
cs
protected override EffectResult OnTriggerEffect(CCEffectInstance effectInstance) {
    if (Player.Instance.CCJump()) {
        return EffectResult.Success;
    }

    return EffectResult.Retry;
}

Another condition where we don't want this fun to be ran, is when the player is dead. Instead of checking it inside of the CCJump function, we can add a new override function called CanBeRan to automatically not attempt execution if the player's dead.

cs
public override bool CanBeRan() {
    return !Player.Instance.Dead;
}

The effect will automatically return EffectResult.Retry if CanBeRan() is false. Our completed class should now look like this:

cs
using WarpWorld.CrowdControl;
using CrowdControlSampleGame;
public class CCJump : CCEffect {
    protected override EffectResult OnTriggerEffect(CCEffectInstance effectInstance) {
        if (Player.Instance.CCJump()) {
            return EffectResult.Success;
        }
		return EffectResult.Retry;
    }

    public override bool CanBeRan() {
        return !Player.Instance.Dead;
    }
}