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

Small lua improvements #15088

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 70 additions & 35 deletions source/psychlua/FunkinLua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ class FunkinLua {
var runningScripts:Array<String> = [];
for (script in game.luaArray)
runningScripts.push(script.scriptName);

for (script in game.hscriptArray)
runningScripts.push(script.origin);

return runningScripts;
});
Expand Down Expand Up @@ -349,12 +352,20 @@ class FunkinLua {
}
}
});*/
Lua_helper.add_callback(lua, "isRunning", function(luaFile:String) {
var foundScript:String = findScript(luaFile);
if(foundScript != null)
for (luaInstance in game.luaArray)
if(luaInstance.scriptName == foundScript)
return true;
Lua_helper.add_callback(lua, "isRunning", function(file:String, ?checkForHaxe:Bool = false) {
var foundScript:String = findScript(file, (checkForHaxe ? '.hx' : 'lua'));
if (foundScript != null) {
if (checkForHaxe) {
for (hscriptInstance in game.hscriptArray)
if (hscriptInstance.origin == foundScript)
return true;
}
else {
for (luaInstance in game.luaArray)
if (luaInstance.scriptName == foundScript)
return true;
}
}
return false;
});

Expand All @@ -365,6 +376,9 @@ class FunkinLua {
Lua_helper.add_callback(lua, "getVar", function(varName:String) {
return MusicBeatState.getVariables().get(varName);
});
Lua_helper.add_callback(lua, "removeVar", function(varName:String) {
return MusicBeatState.getVariables().remove(varName);
});

Lua_helper.add_callback(lua, "addLuaScript", function(luaFile:String, ?ignoreAlreadyRunning:Bool = false) { //would be dope asf.
var foundScript:String = findScript(luaFile);
Expand All @@ -383,9 +397,9 @@ class FunkinLua {
}
luaTrace("addLuaScript: Script doesn't exist!", false, false, FlxColor.RED);
});
Lua_helper.add_callback(lua, "addHScript", function(luaFile:String, ?ignoreAlreadyRunning:Bool = false) {
Lua_helper.add_callback(lua, "addHScript", function(haxeFile:String, ?ignoreAlreadyRunning:Bool = false) {
#if HSCRIPT_ALLOWED
var foundScript:String = findScript(luaFile, '.hx');
var foundScript:String = findScript(haxeFile, '.hx');
if(foundScript != null)
{
if(!ignoreAlreadyRunning)
Expand Down Expand Up @@ -491,30 +505,35 @@ class FunkinLua {
});

//shitass stuff for epic coders like me B) *image of obama giving himself a medal*
Lua_helper.add_callback(lua, "getObjectOrder", function(obj:String) {
Lua_helper.add_callback(lua, "getObjectOrder", function(obj:String, ?group:String = null) {
var split:Array<String> = obj.split('.');
var leObj:FlxBasic = LuaUtils.getObjectDirectly(split[0]);
if(split.length > 1) {
leObj = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}

if(leObj != null)
{
return LuaUtils.getTargetInstance().members.indexOf(leObj);
if(leObj != null) {
var groupObj:Dynamic = LuaUtils.getObjectDirectly(group);
if (groupObj == null) groupObj = LuaUtils.getTargetInstance();

return groupObj.members.indexOf(leObj);
}
luaTrace("getObjectOrder: Object " + obj + " doesn't exist!", false, false, FlxColor.RED);
return -1;
});
Lua_helper.add_callback(lua, "setObjectOrder", function(obj:String, position:Int) {
Lua_helper.add_callback(lua, "setObjectOrder", function(obj:String, position:Int, ?group:String = null) {
var split:Array<String> = obj.split('.');
var leObj:FlxBasic = LuaUtils.getObjectDirectly(split[0]);
if(split.length > 1) {
leObj = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}

if(leObj != null) {
LuaUtils.getTargetInstance().remove(leObj, true);
LuaUtils.getTargetInstance().insert(position, leObj);
var groupObj:Dynamic = LuaUtils.getObjectDirectly(group);
if (groupObj == null) groupObj = LuaUtils.getTargetInstance();

groupObj.remove(leObj, true);
groupObj.insert(position, leObj);
return;
}
luaTrace("setObjectOrder: Object " + obj + " doesn't exist!", false, false, FlxColor.RED);
Expand All @@ -531,22 +550,23 @@ class FunkinLua {
if(tag != null)
{
var variables = MusicBeatState.getVariables();
tag = 'tween_' + LuaUtils.formatVariable(tag);
var originalTag:String = tag;
tag = LuaUtils.formatVariable('tween_$tag');
variables.set(tag, FlxTween.tween(penisExam, values, duration, {
type: myOptions.type,
ease: myOptions.ease,
startDelay: myOptions.startDelay,
loopDelay: myOptions.loopDelay,

onUpdate: function(twn:FlxTween) {
if(myOptions.onUpdate != null) game.callOnLuas(myOptions.onUpdate, [tag, vars]);
if(myOptions.onUpdate != null) game.callOnLuas(myOptions.onUpdate, [originalTag, vars]);
},
onStart: function(twn:FlxTween) {
if(myOptions.onStart != null) game.callOnLuas(myOptions.onStart, [tag, vars]);
if(myOptions.onStart != null) game.callOnLuas(myOptions.onStart, [originalTag, vars]);
},
onComplete: function(twn:FlxTween) {
if(twn.type == FlxTweenType.ONESHOT || twn.type == FlxTweenType.BACKWARD) variables.remove(tag);
if(myOptions.onComplete != null) game.callOnLuas(myOptions.onComplete, [tag, vars]);
if(myOptions.onComplete != null) game.callOnLuas(myOptions.onComplete, [originalTag, vars]);
}
}));
}
Expand Down Expand Up @@ -679,20 +699,23 @@ class FunkinLua {
Lua_helper.add_callback(lua, "setScore", function(value:Int = 0) {
game.songScore = value;
game.RecalculateRating();
return value;
});
Lua_helper.add_callback(lua, "setMisses", function(value:Int = 0) {
game.songMisses = value;
game.RecalculateRating();
return value;
});
Lua_helper.add_callback(lua, "setHits", function(value:Int = 0) {
game.songHits = value;
game.RecalculateRating();
return value;
});
Lua_helper.add_callback(lua, "getScore", function() return game.songScore);
Lua_helper.add_callback(lua, "getMisses", function() return game.songMisses);
Lua_helper.add_callback(lua, "getHits", function() return game.songHits);

Lua_helper.add_callback(lua, "setHealth", function(value:Float = 0) game.health = value);
Lua_helper.add_callback(lua, "setHealth", function(value:Float = 0) return game.health = value);
Lua_helper.add_callback(lua, "addHealth", function(value:Float = 0) game.health += value);
Lua_helper.add_callback(lua, "getHealth", function() return game.health);

Expand Down Expand Up @@ -784,11 +807,11 @@ class FunkinLua {
Lua_helper.add_callback(lua, "setCharacterX", function(type:String, value:Float) {
switch(type.toLowerCase()) {
case 'dad' | 'opponent':
game.dadGroup.x = value;
return game.dadGroup.x = value;
case 'gf' | 'girlfriend':
game.gfGroup.x = value;
return game.gfGroup.x = value;
default:
game.boyfriendGroup.x = value;
return game.boyfriendGroup.x = value;
}
});
Lua_helper.add_callback(lua, "getCharacterY", function(type:String) {
Expand All @@ -804,17 +827,26 @@ class FunkinLua {
Lua_helper.add_callback(lua, "setCharacterY", function(type:String, value:Float) {
switch(type.toLowerCase()) {
case 'dad' | 'opponent':
game.dadGroup.y = value;
return game.dadGroup.y = value;
case 'gf' | 'girlfriend':
game.gfGroup.y = value;
return game.gfGroup.y = value;
default:
game.boyfriendGroup.y = value;
return game.boyfriendGroup.y = value;
}
});
Lua_helper.add_callback(lua, "cameraSetTarget", function(target:String) {
var isDad:Bool = false;
if(target == 'dad') {
isDad = true;
switch(target.toLowerCase()) {
case 'gf' | 'girlfriend':
if (game.gf != null) {
game.camFollow.setPosition(game.gf.getMidpoint().x, game.gf.getMidpoint().y);
game.camFollow.x += game.gf.cameraPosition[0] + game.girlfriendCameraOffset[0];
game.camFollow.y += game.gf.cameraPosition[1] + game.girlfriendCameraOffset[1];
game.tweenCamIn();
return false;
}
case 'dad' | 'opponent':
isDad = true;
}
game.moveCamera(isDad);
return isDad;
Expand All @@ -830,13 +862,13 @@ class FunkinLua {
LuaUtils.cameraFromString(camera).fade(CoolUtil.colorFromString(color), duration, false,null,forced);
});
Lua_helper.add_callback(lua, "setRatingPercent", function(value:Float) {
game.ratingPercent = value;
return game.ratingPercent = value;
});
Lua_helper.add_callback(lua, "setRatingName", function(value:String) {
game.ratingName = value;
return game.ratingName = value;
});
Lua_helper.add_callback(lua, "setRatingFC", function(value:String) {
game.ratingFC = value;
return game.ratingFC = value;
});
Lua_helper.add_callback(lua, "getMouseX", function(camera:String) {
var cam:FlxCamera = LuaUtils.cameraFromString(camera);
Expand Down Expand Up @@ -890,20 +922,22 @@ class FunkinLua {
Lua_helper.add_callback(lua, "getScreenPositionX", function(variable:String, ?camera:String) {
var split:Array<String> = variable.split('.');
var obj:FlxSprite = LuaUtils.getObjectDirectly(split[0]);
var cam:FlxCamera = LuaUtils.cameraFromString(camera);
if(split.length > 1) {
obj = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}
if(obj != null) return obj.getScreenPosition().x;
if(obj != null) return obj.getScreenPosition(cam).x;

return 0;
});
Lua_helper.add_callback(lua, "getScreenPositionY", function(variable:String, ?camera:String) {
var split:Array<String> = variable.split('.');
var obj:FlxSprite = LuaUtils.getObjectDirectly(split[0]);
var cam:FlxCamera = LuaUtils.cameraFromString(camera);
if(split.length > 1) {
obj = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}
if(obj != null) return obj.getScreenPosition().y;
if(obj != null) return obj.getScreenPosition(cam).y;

return 0;
});
Expand Down Expand Up @@ -1114,11 +1148,12 @@ class FunkinLua {
return (obj != null && Std.isOfType(obj, FlxSprite));
});
Lua_helper.add_callback(lua, "luaTextExists", function(tag:String) {
var obj:FlxSprite = MusicBeatState.getVariables().get(tag);
var obj:FlxText = MusicBeatState.getVariables().get(tag);
return (obj != null && Std.isOfType(obj, FlxText));
});
Lua_helper.add_callback(lua, "luaSoundExists", function(tag:String) {
var obj:FlxSprite = MusicBeatState.getVariables().get(tag);
tag = LuaUtils.formatVariable('sound_$tag');
var obj:FlxSound = MusicBeatState.getVariables().get(tag);
return (obj != null && Std.isOfType(obj, FlxSound));
});

Expand Down
2 changes: 1 addition & 1 deletion source/psychlua/HScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class HScript extends SScript
{
if(funk == null) funk = parentLua;

if(parentLua != null) funk.addLocalCallback(name, func);
if(funk != null) funk.addLocalCallback(name, func);
else FunkinLua.luaTrace('createCallback ($name): 3rd argument is null', false, false, FlxColor.RED);
});
#end
Expand Down
6 changes: 5 additions & 1 deletion source/psychlua/LuaUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class LuaUtils

public static function getLuaTween(options:Dynamic)
{
if (options == null) options = {}
return {
type: getTweenTypeByString(options.type),
startDelay: options.startDelay,
Expand Down Expand Up @@ -496,9 +497,12 @@ class LuaUtils

public static function cameraFromString(cam:String):FlxCamera {
switch(cam.toLowerCase()) {
case 'camgame' | 'game': return PlayState.instance.camGame;
case 'camhud' | 'hud': return PlayState.instance.camHUD;
case 'camother' | 'other': return PlayState.instance.camOther;
}
return PlayState.instance.camGame;
var camera:Dynamic = MusicBeatState.getVariables().get(cam);
if (camera == null || !Std.isOfType(camera, FlxCamera)) camera = PlayState.instance.camGame;
return camera;
}
}
15 changes: 7 additions & 8 deletions source/psychlua/ReflectionFunctions.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ class ReflectionFunctions
Lua_helper.add_callback(lua, "setProperty", function(variable:String, value:Dynamic, ?allowMaps:Bool = false) {
var split:Array<String> = variable.split('.');
if(split.length > 1) {
LuaUtils.setVarInArray(LuaUtils.getPropertyLoop(split, true, allowMaps), split[split.length-1], value, allowMaps);
return true;
return LuaUtils.setVarInArray(LuaUtils.getPropertyLoop(split, true, allowMaps), split[split.length-1], value, allowMaps);
}
LuaUtils.setVarInArray(LuaUtils.getTargetInstance(), variable, value, allowMaps);
return true;
return LuaUtils.setVarInArray(LuaUtils.getTargetInstance(), variable, value, allowMaps);
});
Lua_helper.add_callback(lua, "getPropertyFromClass", function(classVar:String, variable:String, ?allowMaps:Bool = false) {
var myClass:Dynamic = Type.resolveClass(classVar);
Expand All @@ -53,7 +51,7 @@ class ReflectionFunctions
var myClass:Dynamic = Type.resolveClass(classVar);
if(myClass == null)
{
FunkinLua.luaTrace('getPropertyFromClass: Class $classVar not found', false, false, FlxColor.RED);
FunkinLua.luaTrace('setPropertyFromClass: Class $classVar not found', false, false, FlxColor.RED);
return null;
}

Expand Down Expand Up @@ -149,7 +147,7 @@ class ReflectionFunctions

if(myType == null)
{
FunkinLua.luaTrace('createInstance: Variable $variableToSave is already being used and cannot be replaced!', false, false, FlxColor.RED);
FunkinLua.luaTrace('createInstance: Class $className not found.', false, false, FlxColor.RED);
return false;
}

Expand Down Expand Up @@ -189,6 +187,7 @@ class ReflectionFunctions

static function parseInstances(args:Array<Dynamic>)
{
if (args == null) return [];
for (i in 0...args.length)
{
var myArg:String = cast args[i];
Expand All @@ -201,8 +200,8 @@ class ReflectionFunctions
//trace('Op1: $myArg');
var lastIndex:Int = myArg.lastIndexOf('::');

var split:Array<String> = myArg.split('.');
args[i] = (lastIndex > -1) ? Type.resolveClass(myArg.substring(0, lastIndex)) : PlayState.instance;
var split:Array<String> = lastIndex > -1 ? myArg.substring(0, lastIndex).split('.') : myArg.split('.');
args[i] = (lastIndex > -1) ? Type.resolveClass(myArg.substring(lastIndex+2)) : PlayState.instance;
for (j in 0...split.length)
{
//trace('Op2: ${Type.getClass(args[i])}, ${split[j]}');
Expand Down
Loading