callbacks... events, even.

This commit is contained in:
Yoshman29
2022-10-09 14:47:42 +02:00
parent 44a5f4b1be
commit aa46bc3335
5 changed files with 50 additions and 2 deletions
+2
View File
@@ -1,3 +1,5 @@
var fastCarCanDrive:Bool = true;
function create() {
resetFastCar();
}
+1 -1
View File
@@ -1 +1 @@
267
269
+1 -1
View File
@@ -16,12 +16,12 @@ class HScript extends Script {
var code:String = Assets.getText(path);
var parser = new hscript.Parser();
parser.allowJSON = parser.allowMetadata = parser.allowTypes = true;
// interp.errorHandler = _errorHandler;
try {
expr = parser.parseString(code, fileName);
} catch(e) {
return;
}
interp.errorHandler = _errorHandler;
}
private function _errorHandler(error:Error) {
+16
View File
@@ -1,5 +1,7 @@
package funkin.scripting;
import funkin.scripting.events.CancellableEvent;
class ScriptPack extends Script {
public var scripts:Array<Script> = [];
public var additionalDefaultVariables:Map<String, Dynamic> = [];
@@ -17,6 +19,20 @@ class ScriptPack extends Script {
return null;
}
/**
* Sends an event to every single script, and returns the event.
* @param func Function to call
* @param event Event (will be the first parameter of the function)
* @return Event (modified by scripts)
*/
public function event<T:CancellableEvent>(func:String, event:T):T {
for(e in scripts) {
e.call(func, [event]);
if (event.cancelled && event.__continueCalls) break;
}
return event;
}
public override function get(val:String):Dynamic {
for(e in scripts) {
var v = e.get(val);
@@ -0,0 +1,30 @@
package funkin.scripting.events;
import funkin.scripting.ScriptPack;
@:allow(ScriptPack)
class CancellableEvent {
@:dox(hide) public var cancelled:Bool = false;
@:dox(hide) private var __continueCalls:Bool = false;
/**
* Additional data if used in scripts
*/
public var data:Dynamic = {};
/**
* Prevents default action from occuring.
*/
public function cancel() {
cancelled = true;
}
/**
* Creates a new cancellable event.
* This allows scripts to call `cancel()` to cancel the event.
* @param continueCalls Whenever the `ScriptPack` should continue calling functions from other scripts after a script cancelled the event.
*/
public function new(continueCalls:Bool) {
__continueCalls = continueCalls;
}
}