Skip to content

Commit e2a3951

Browse files
Transition crash fix again + changeable transition script (#493)
* fix transition crash when skipping * transition crash fix again * rename transition script var * better transition scripting * add more transition script functions * move post create to actually be after * change this * actually destroy the script * add a variable to disable skipping the transition * opa idk felt better like this --------- Co-authored-by: ⍚~Nex <87421482+NexIsDumb@users.noreply.github.com>
1 parent 6e2877c commit e2a3951

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed
Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package funkin.backend;
22

3+
import funkin.backend.scripting.events.CancellableEvent;
4+
import funkin.backend.scripting.events.TransitionCreationEvent;
5+
import funkin.backend.scripting.Script;
36
import flixel.tweens.FlxTween;
47
import flixel.FlxState;
58
import funkin.backend.utils.FunkinParentDisabler;
69

710
class MusicBeatTransition extends MusicBeatSubstate {
11+
public static var script:String = "";
12+
public var transitionScript:Script;
13+
814
var nextFrameSkip:Bool = false;
915

1016
public var transitionTween:FlxTween = null;
1117
public var transitionCamera:FlxCamera;
1218
public var newState:FlxState;
19+
public var transOut:Bool = false;
20+
21+
public var allowSkip:Bool = true;
1322

1423
public var blackSpr:FlxSprite;
1524
public var transitionSprite:FunkinSprite;
@@ -20,16 +29,30 @@ class MusicBeatTransition extends MusicBeatSubstate {
2029

2130
public override function create() {
2231
if (newState != null)
23-
add(new FunkinParentDisabler(true));
32+
add(new FunkinParentDisabler(true, false));
2433

2534
transitionCamera = new FlxCamera();
2635
transitionCamera.bgColor = 0;
2736
FlxG.cameras.add(transitionCamera, false);
2837

2938
cameras = [transitionCamera];
30-
var out = newState != null;
3139

32-
blackSpr = new FlxSprite(0, out ? -transitionCamera.height : transitionCamera.height).makeGraphic(1, 1, -1);
40+
transitionScript = Script.create(Paths.script(script));
41+
transitionScript.setParent(this);
42+
transitionScript.load();
43+
44+
var event = EventManager.get(TransitionCreationEvent).recycle(newState != null, newState);
45+
transitionScript.call('create', [event]);
46+
47+
transOut = event.transOut;
48+
newState = event.newState;
49+
50+
if (event.cancelled) {
51+
super.create();
52+
return;
53+
}
54+
55+
blackSpr = new FlxSprite(0, transOut ? -transitionCamera.height : transitionCamera.height).makeGraphic(1, 1, -1);
3356
blackSpr.scale.set(transitionCamera.width, transitionCamera.height);
3457
blackSpr.color = 0xFF000000;
3558
blackSpr.updateHitbox();
@@ -43,7 +66,7 @@ class MusicBeatTransition extends MusicBeatSubstate {
4366
} else {
4467
transitionSprite.screenCenter();
4568
}
46-
transitionCamera.flipY = !out;
69+
transitionCamera.flipY = !transOut;
4770
add(transitionSprite);
4871

4972
transitionCamera.scroll.y = transitionCamera.height;
@@ -55,41 +78,59 @@ class MusicBeatTransition extends MusicBeatSubstate {
5578
});
5679

5780
super.create();
81+
transitionScript.call('postCreate', [event]);
5882
}
5983

6084
public override function update(elapsed:Float) {
85+
transitionScript.call('update', [elapsed]);
6186
super.update(elapsed);
6287

6388
if (nextFrameSkip) {
64-
finish();
65-
return;
89+
var event = new CancellableEvent();
90+
transitionScript.call('onSkip', [event]);
91+
if (!event.cancelled) {
92+
finish();
93+
return;
94+
}
6695
}
6796

68-
if (!parent.persistentUpdate && FlxG.keys.pressed.SHIFT) {
97+
if (allowSkip && !parent.persistentUpdate && FlxG.keys.pressed.SHIFT) {
6998
// skip
7099
if (newState != null) {
71100
nextFrameSkip = true;
72101
parent.persistentDraw = false;
73102
} else {
74-
finish();
103+
var event = new CancellableEvent();
104+
transitionScript.call('onSkip', [event]);
105+
if (!event.cancelled) {
106+
finish();
107+
}
75108
}
76109
}
110+
transitionScript.call('postUpdate', [elapsed]);
77111
}
78112

79113
public function finish() {
114+
var event = new CancellableEvent();
115+
transitionScript.call('onFinish', [event]);
116+
if (event.cancelled) return;
117+
80118
if (newState != null)
81119
FlxG.switchState(newState);
82120
close();
121+
122+
transitionScript.call('onPostFinish');
83123
}
84124

85125
public override function destroy() {
86-
if (transitionTween != null)
87-
transitionTween.cancel();
126+
transitionScript.call('destroy');
127+
128+
if (transitionTween != null) transitionTween.cancel();
88129
transitionTween = FlxDestroyUtil.destroy(transitionTween);
89-
if (newState == null && FlxG.cameras.list.contains(transitionCamera))
90-
FlxG.cameras.remove(transitionCamera);
91-
else
92-
transitionCamera.bgColor = 0xFF000000;
130+
if (newState == null && FlxG.cameras.list.contains(transitionCamera)) FlxG.cameras.remove(transitionCamera);
131+
else transitionCamera.bgColor = 0xFF000000;
132+
133+
transitionScript.destroy();
93134
super.destroy();
94135
}
95136
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package funkin.backend.scripting.events;
2+
3+
import flixel.FlxState;
4+
5+
/**
6+
* CANCEL this event to prevent default behaviour!
7+
*/
8+
final class TransitionCreationEvent extends CancellableEvent {
9+
/**
10+
* If the transition is going out into another state
11+
*/
12+
public var transOut:Bool;
13+
14+
/**
15+
* The state that is about to be loaded (only on trans out)
16+
*/
17+
public var newState:FlxState;
18+
}

source/funkin/backend/system/MainState.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class MainState extends FlxState {
5353
}
5454
#end
5555

56+
MusicBeatTransition.script = "";
5657
Main.refreshAssets();
5758
ModsFolder.onModSwitch.dispatch(ModsFolder.currentModFolder);
5859
DiscordUtil.init();

source/funkin/backend/utils/FunkinParentDisabler.hx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ class FunkinParentDisabler extends FlxBasic {
1919
var __timers:Array<FlxTimer>;
2020
var __sounds:Array<FlxSound>;
2121
var __replaceUponDestroy:Bool;
22-
public function new(replaceUponDestroy:Bool = false) {
22+
var __restoreUponDestroy:Bool;
23+
public function new(replaceUponDestroy:Bool = false, restoreUponDestroy:Bool = true) {
2324
super();
2425
__replaceUponDestroy = replaceUponDestroy;
26+
__restoreUponDestroy = restoreUponDestroy;
2527
@:privateAccess {
2628
// tweens
2729
__tweens = FlxTween.globalManager._tweens.copy();
@@ -53,6 +55,11 @@ class FunkinParentDisabler extends FlxBasic {
5355
public override function destroy() {
5456
super.destroy();
5557
@:privateAccess {
58+
if (!__restoreUponDestroy) {
59+
for(t in __tweens) { t.cancel(); t.destroy(); };
60+
for(t in __timers) { t.cancel(); t.destroy(); };
61+
return;
62+
}
5663
if (__replaceUponDestroy) {
5764
FlxTween.globalManager._tweens = __tweens;
5865
FlxTimer.globalManager._timers = __timers;

0 commit comments

Comments
 (0)