Skip to content

Commit

Permalink
Event based dialogue handling (#443)
Browse files Browse the repository at this point in the history
* Event based dialogue handling

* better imo

* typo

* Small script example

new script by NexIsDumb

---------

Co-authored-by: ⍚~Nex <87421482+NexIsDumb@users.noreply.github.com>
  • Loading branch information
Jamextreme140 and NexIsDumb authored Nov 14, 2024
1 parent f8a19b1 commit b1b1663
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
5 changes: 5 additions & 0 deletions assets/songs/test/dialogue.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function next(event)
if (!event.playFirst) trace("-");

function postNext(event)
trace(curLine.char + " says: " + curLine.text);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package funkin.backend.scripting.events.dialogue;

/**
* CANCEL this event to prevent continuing with the next dialogue.
*
* **NOTE**: To access the current, past or next lines, use `curLine`, `lastLine` or also `dialogueLines` in the `DialogueCutscene` class (they're globals and can be accessed at any time as long as the Dialogue Cutscene is active)!
*/
final class DialogueNextLineEvent extends CancellableEvent
{
/**
* If the dialogue has just been opened.
*/
public var playFirst:Bool = false;
}
14 changes: 7 additions & 7 deletions source/funkin/game/cutscenes/DialogueCutscene.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package funkin.game.cutscenes;
import funkin.backend.utils.FunkinParentDisabler;
import funkin.backend.scripting.events.dialogue.*;
import funkin.backend.scripting.events.CancellableEvent;
import funkin.backend.scripting.events.DynamicEvent;
import funkin.backend.scripting.events.dialogue.DialogueNextLineEvent;
import funkin.backend.scripting.Script;
import flixel.sound.FlxSound;
import funkin.game.cutscenes.dialogue.*;
Expand Down Expand Up @@ -138,8 +138,8 @@ class DialogueCutscene extends Cutscene {
public var canProceed:Bool = true;

public function next(playFirst:Bool = false) {
var event = EventManager.get(DynamicEvent).recycle(playFirst);
dialogueScript.call("next", [playFirst]);
var event = EventManager.get(DialogueNextLineEvent).recycle(playFirst);
dialogueScript.call("next", [event]);
if(event.cancelled || !canProceed) return;

if ((curLine = dialogueLines.shift()) == null) {
Expand All @@ -154,7 +154,7 @@ class DialogueCutscene extends Cutscene {
}

if (curLine.callback != null)
dialogueScript.call(curLine.callback, [playFirst]);
dialogueScript.call(curLine.callback, [event.playFirst]);

for (k=>c in charMap)
if (k != curLine.char)
Expand All @@ -167,8 +167,8 @@ class DialogueCutscene extends Cutscene {
dialogueBox.popupChar(char, force);
}

var finalSuffix:String = playFirst && dialogueBox.hasAnimation(curLine.bubble + "-firstOpen") ? "-firstOpen" : dialogueBox.hasAnimation(curLine.bubble + "-open") ? "-open" : "";
dialogueBox.playBubbleAnim(curLine.bubble, finalSuffix, curLine.text, curLine.format, curLine.speed, curLine.nextSound, curLine.textSound != null ? [curLine.textSound] : null, finalSuffix == "-firstOpen" || finalSuffix == "-open", !playFirst);
var finalSuffix:String = event.playFirst && dialogueBox.hasAnimation(curLine.bubble + "-firstOpen") ? "-firstOpen" : dialogueBox.hasAnimation(curLine.bubble + "-open") ? "-open" : "";
dialogueBox.playBubbleAnim(curLine.bubble, finalSuffix, curLine.text, curLine.format, curLine.speed, curLine.nextSound, curLine.textSound != null ? [curLine.textSound] : null, finalSuffix == "-firstOpen" || finalSuffix == "-open", !event.playFirst);

if(curLine.playSound != null) curLine.playSound.play();
if(curLine.changeMusic != null) {
Expand All @@ -178,7 +178,7 @@ class DialogueCutscene extends Cutscene {
curMusic.fadeIn(1, 0, curMusic.volume);
} else if(curLine.musicVolume != null && curMusic != null) curMusic.volume = curLine.musicVolume;

dialogueScript.call("postNext", [playFirst]);
dialogueScript.call("postNext", [event]);
}

public override function close() {
Expand Down

0 comments on commit b1b1663

Please sign in to comment.