Creating a Basic Effect
An effect has two parts: a resource holding what viewers see in the menu (name, price, description), and a script deciding what happens in the game when someone buys it.
Create the Effect List
All effects of a game live in one CCEffectEntries resource.
- In the FileSystem dock, right-click and choose Create New > Resource..., then pick CCEffectEntries.
- Save it as
res://effects.tres, or anywhere else and setcrowd_control/common/effectsto its path.
Write the Effect Script
Create a script that extends CCEffect and give it a class_name:
class_name CCDamage
extends CCEffect
func _trigger(_instance: CCEffectInstance) -> EffectResult:
GameManager.instance.player.damage(1)
return SUCCESS
func _can_run() -> bool:
return GameManager.instance != null and GameManager.instance.player.alive_trigger() is required. _can_run() is optional and is checked first.
Effects are resources, not nodes, so they have no scene tree of their own. Reach your game through an autoload or a static reference, as GameManager.instance does above.
Add It to the List
- Open your
CCEffectEntriesresource in the Inspector. - Add an element to Effects and choose New CCDamage.
- Fill in its properties.
| Property | Use |
|---|---|
id | Unique ID of the effect, for example damageplayer. Letters, numbers, and underscores only. Required. |
display_name | Name shown to viewers. |
description | Description shown to viewers, up to 512 characters. |
price | Default price in coins. Streamers can change it for their channel. |
icon | Icon shown in the effect menu. Exported with your effect pack. |
categories | Categories the effect is listed under in the menu. |
groups | Groups the effect belongs to. |
morality | Whether the effect helps (Good) or hurts (Evil) the player. |
orderliness | Whether the effect is Orderly or Chaotic. |
note | Short note shown next to the name. |
session_max | How often the effect can be bought per session, 0 for no limit. |
pooling_disabled | Viewers may not pool coins for this effect. |
conflicts | IDs of effects this one cannot run alongside, see Controlling the Effect Menu. |
max_retries, retry_delay | How often and how many seconds apart the effect is retried, see below. |
icon_color | For your own overlay; not sent to Crowd Control. |
What the Script Returns
The plugin answers Crowd Control based on the result of _trigger():
| Result | Meaning | Viewer |
|---|---|---|
SUCCESS | The effect happened. | Charged. |
FAILURE | It could not happen right now. | Refunded. |
UNAVAILABLE | It cannot happen at all, for example because the effect is obsolete. | Refunded. |
RETRY | Not now, try again shortly. | Waits. |
When _can_run() returns false or _trigger() returns RETRY, the request is tried again every retry_delay seconds, up to max_retries times (3 times, 5 seconds apart by default). If it still cannot run, the viewer is refunded.
A request is never held for more than 55 seconds. Requests that could not start by then are refunded automatically, so you do not have to answer them yourself.
WARNING
Every effect must implement _trigger() and return one of the results above. An effect without it is answered as unavailable and an error is printed.
The Request
_trigger() receives a CCEffectInstance describing the request:
func _trigger(instance: CCEffectInstance) -> EffectResult:
var viewer: StreamUser = instance.get_user()
print("%s bought %s" % [viewer.name, instance.get_effect().display_name])
return SUCCESS| Method | Use |
|---|---|
get_user() | The viewer as a StreamUser (name, profile_icon). The icon is downloaded in the background; icon_downloaded is emitted on the user when it is there. Anonymous and test requests come from a placeholder user. |
get_effect() | The effect resource. |
get_id() | The ID of this request. |
is_test() | Whether this is a test rather than a purchase. |
Reacting Outside the Effect
The singleton reports every step, which is what an overlay or a log needs:
| Signal | When |
|---|---|
effect_requested(effect) | A request arrived. |
effect_queued(instance) | The request is waiting to start. |
effect_triggered(instance) | An effect ran successfully. |
effect_dequeued(id, result) | The request left the queue, with its result. |
Next Step
Continue with Creating a Parameter Effect.
