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 10 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
5 changes: 2 additions & 3 deletions source/psychlua/CustomSubstate.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class CustomSubstate extends MusicBeatSubstate
}
}
PlayState.instance.openSubState(new CustomSubstate(name));
PlayState.instance.setOnHScript('customSubstate', instance);
PlayState.instance.setOnHScript('customSubstateName', name);
}

public static function closeCustomSubstate()
Expand All @@ -51,7 +49,6 @@ class CustomSubstate extends MusicBeatSubstate
if(instance != null)
{
var tagObject:FlxObject = cast (MusicBeatState.getVariables().get(tag), FlxObject);
#if LUA_ALLOWED if(tagObject == null) tagObject = cast (MusicBeatState.getVariables().get(tag), FlxObject); #end

if(tagObject != null)
{
Expand All @@ -66,6 +63,7 @@ class CustomSubstate extends MusicBeatSubstate
override function create()
{
instance = this;
PlayState.instance.setOnHScript('customSubstate', instance);

PlayState.instance.callOnScripts('onCustomSubstateCreate', [name]);
super.create();
Expand All @@ -75,6 +73,7 @@ class CustomSubstate extends MusicBeatSubstate
public function new(name:String)
{
CustomSubstate.name = name;
PlayState.instance.setOnHScript('customSubstateName', name);
super();
cameras = [FlxG.cameras.list[FlxG.cameras.list.length - 1]];
}
Expand Down
124 changes: 82 additions & 42 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 All @@ -849,7 +881,7 @@ class FunkinLua {

Lua_helper.add_callback(lua, "getMidpointX", function(variable:String) {
var split:Array<String> = variable.split('.');
var obj:FlxSprite = LuaUtils.getObjectDirectly(split[0]);
var obj:FlxObject = LuaUtils.getObjectDirectly(split[0]);
if(split.length > 1) {
obj = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}
Expand All @@ -859,7 +891,7 @@ class FunkinLua {
});
Lua_helper.add_callback(lua, "getMidpointY", function(variable:String) {
var split:Array<String> = variable.split('.');
var obj:FlxSprite = LuaUtils.getObjectDirectly(split[0]);
var obj:FlxObject = LuaUtils.getObjectDirectly(split[0]);
if(split.length > 1) {
obj = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}
Expand Down Expand Up @@ -889,21 +921,23 @@ 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 obj:FlxObject = 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 obj:FlxObject = 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 @@ -1077,7 +1111,12 @@ class FunkinLua {
return;
}

var poop:FlxSprite = Reflect.getProperty(LuaUtils.getTargetInstance(), obj);
var split:Array<String> = obj.split('.');
var poop:FlxSprite = LuaUtils.getObjectDirectly(split[0]);
if(split.length > 1) {
poop = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}

if(poop != null) {
poop.updateHitbox();
return;
Expand Down Expand Up @@ -1114,11 +1153,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 Expand Up @@ -1149,7 +1189,7 @@ class FunkinLua {
}

var split:Array<String> = obj.split('.');
var object:FlxSprite = LuaUtils.getObjectDirectly(split[0]);
var object:FlxBasic = LuaUtils.getObjectDirectly(split[0]);
if(split.length > 1) {
object = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(split), split[split.length-1]);
}
Expand Down Expand Up @@ -1182,7 +1222,7 @@ class FunkinLua {
return false;
});
Lua_helper.add_callback(lua, "screenCenter", function(obj:String, pos:String = 'xy') {
var spr:FlxSprite = game.getLuaObject(obj);
var spr:FlxObject = game.getLuaObject(obj);

if(spr==null){
var split:Array<String> = obj.split('.');
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;
}
}
Loading
Loading