Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hscript-improved #13304

Closed
Prev Previous commit
Next Next commit
HScript.hscriptTrace
errors on hscript now traces to console
also removed useless trace
  • Loading branch information
TheLeerName committed Sep 19, 2023
commit 4e83741cce72af7ba3d8072534c8a0401eb845d2
27 changes: 18 additions & 9 deletions source/psychlua/HScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class HScript extends Interp
public static function initHaxeModule(parent:FunkinLua)
{
if(parent.hscript == null) {
trace('initializing haxe interp for: ${parent.scriptName}');
parent.hscript = new HScript(parent);
trace('initialized hscript interp successfully: ${parent.scriptName}');
}
}

Expand All @@ -34,6 +34,11 @@ class HScript extends Interp
parent.hscript.executeCode(code);
}

public static function hscriptTrace(text:String, color:FlxColor = FlxColor.WHITE) {
PlayState.instance.addTextToDebug(text, color);
trace(text);
}

public var origin:String;
public function new(?parent:FunkinLua, ?file:String) {
super();
Expand Down Expand Up @@ -161,15 +166,15 @@ class HScript extends Interp
}

public function executeFunction(?funcToRun:String, ?funcArgs:Array<Dynamic>):Dynamic {
if (funcToRun != null && active) {
if (variables.exists(funcToRun)) {
if (funcArgs == null) funcArgs = [];
try {
return Reflect.callMethod(null, variables.get(funcToRun), funcArgs);
}
catch(e)
exception = e;
if (funcToRun == null || !active) return null;

if (variables.exists(funcToRun)) {
if (funcArgs == null) funcArgs = [];
try {
return Reflect.callMethod(null, variables.get(funcToRun), funcArgs);
}
catch(e)
exception = e;
}
return null;
}
Expand All @@ -179,6 +184,7 @@ class HScript extends Interp
#if LUA_ALLOWED
funk.addLocalCallback("runHaxeCode", function(codeToRun:String, ?varsToBring:Any = null, ?funcToRun:String = null, ?funcArgs:Array<Dynamic> = null):Dynamic {
initHaxeModule(funk);
if (!funk.hscript.active) return null;

if(varsToBring != null) {
for (key in Reflect.fields(varsToBring)) {
Expand All @@ -201,6 +207,8 @@ class HScript extends Interp
});

funk.addLocalCallback("runHaxeFunction", function(funcToRun:String, ?funcArgs:Array<Dynamic>) {
if (!funk.hscript.active) return null;

var retVal:Dynamic = funk.hscript.executeFunction(funcToRun, funcArgs);

if (funk.hscript.exception != null)
Expand All @@ -211,6 +219,7 @@ class HScript extends Interp
// This function is unnecessary because import already exists in hscript-improved as a native feature
funk.addLocalCallback("addHaxeLibrary", function(libName:String, ?libPackage:String = '') {
initHaxeModule(funk);
if (!funk.hscript.active) return null;

var str:String = '';
if(libPackage.length > 0)
Expand Down
10 changes: 4 additions & 6 deletions source/states/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ class PlayState extends MusicBeatState
}
catch(e:Dynamic)
{
addTextToDebug('ERROR ("Set Property" Event) - $e', FlxColor.RED);
HScript.hscriptTrace('ERROR ("Set Property" Event) - $e', FlxColor.RED);
}

case 'Play Sound':
Expand Down Expand Up @@ -3156,7 +3156,6 @@ class PlayState extends MusicBeatState
newScript.destroy();
newScript = null;
hscriptArray.remove(newScript);
trace('failed to initialize hscript interp!!! ($file)');
}

try
Expand All @@ -3165,15 +3164,15 @@ class PlayState extends MusicBeatState
hscriptArray.push(newScript);

if (newScript.exception != null) {
addTextToDebug('ERROR ON LOADING - ${newScript.exception.message}', FlxColor.RED);
HScript.hscriptTrace('ERROR ON LOADING - ${newScript.exception.message}', FlxColor.RED);
makeError(newScript);
return;
}

if (newScript.variables.exists('onCreate')) {
var retVal:Dynamic = newScript.executeFunction('onCreate');
if (newScript.exception != null) {
addTextToDebug('ERROR (onCreate) - ${newScript.exception.message}', FlxColor.RED);
HScript.hscriptTrace('ERROR (onCreate) - ${newScript.exception.message}', FlxColor.RED);
makeError(newScript);
return;
}
Expand All @@ -3183,7 +3182,7 @@ class PlayState extends MusicBeatState
}
catch(e)
{
addTextToDebug('ERROR - $e', FlxColor.RED);
HScript.hscriptTrace('ERROR - $e', FlxColor.RED);
if (hscriptArray.length > 0)
makeError(hscriptArray[hscriptArray.length - 1]);
}
Expand Down Expand Up @@ -3257,7 +3256,6 @@ class PlayState extends MusicBeatState
try
{
returnVal = script.executeFunction(funcToCall, args);
trace(returnVal, script.exception);
if (script.exception != null) {
FunkinLua.luaTrace('ERROR ($funcToCall) - ${script.exception}', true, false, FlxColor.RED);
}
Expand Down