Skip to content

Utility

GhostglowDev edited this page Mar 21, 2024 · 2 revisions
local utl = require "ghostutil.util"

See source code

Tweens

doTweenNumber(tag:String, from:Float, to:Float, duration:Float, ease:String) -> Void

Tweens a number (Returns the tweened values in a callback)

  • tag: For cancelling the tween and onTweenCompleted
  • from: The number where the tween starts at
  • to: The number where the tweens ends at
  • duration: The time it takes to complete the tween
  • ease: FlxEase

Functions

switch(case:Dynamic, cases:Map<String, Function>)

switch function from Haxe

Example:

local foo = {"curry", "tuna"}

function onCreatePost()
    utl.switch(foo[1], {
        ["curry"] = function()
            debugPrint("I like my tuna with curry")
        end,
        ["soup"] = function()
            debugPrint("I like my tuna with soup")
        end 
    })

    -- prints "I like my tuna with curry"
end

runCodeFromFile(file:String) -> fun():Null

Runs a lua code from a file

  • file: File path (starts from ./mods/), must include the extension

It's not a must for the file to be a lua file. You can also use .txt for this.

Example:

From mods/code/hi.lua:

debugPrint("hello hi")

Script:

function onCreatePost()
    utl.runCodeFromFile("code/hi.lua")
    -- prints "hello hi"
end

toBool(v:Dynamic) -> Bool

Converts any value to a boolean

  • v: The value you want to convert to a boolean

Example:

utl.toBool("true")
-- returns true

utl.toBool(0)
-- returns false

utl.toBool("1")
-- returns true

isBool, isNumber, isString, isTable, isNil/isNaN/isNull(v:Dynamic) -> Bool

Checks if a value is a bool/number/string/table/nil

  • v: Value to check

isOfType(v:Dynamic, type:String) -> Bool

  • v: Value to check
  • type: Type

Example:

utl.isBool("this is totally a boolean")
-- returns false

utl.isNumber(100)
-- returns true

utl.isNil("nil")
-- returns false

utl.isTable({"im", "table"})
-- returns true

utl.isOfType("is this a string", "number")
-- returns false

debugPrint(texts:Array[String], color:String) -> Void

basically debugPrint() but with color

  • texts: The texts you want to print
  • color: The text color (0xAARRGGBB)

Example:

utl.debugPrint({"foo", "tuna", "curry"}, "0xFF00FF00")
-- debug-prints "foo", "tuna" and "curry" in a green text all at once 

quickText(tag:String, text:String, fieldWidth:Float, pos:Array[Float]|Float, ?instantAdd:Bool, ?size:Float, ?alignment:String, ?color:String, ?centerType:String) -> Void

All-in-one makeLuaText()

  • tag: The text's tag
  • text: The text, duh
  • fieldWidth: How long of texts could line of text fit?
  • pos: Text's position. If both values are the same, use Float instead
  • instantAdd: Adds it right after setting it up without having to do addLuaText() manually
  • size: Text's size/scale
  • alignment: Alignment type. center, left or right
  • color: Text's color (0xAARRGGBB)
  • centerType: Leave this as nil if you don't want to center, At what axis do you want it to be centered on? x, y or xy

Example:

utl.quickText("myText", "my cool text", 0, {0, 350}, true, 60, "center", "FFFFFF", "x")

quickSprite(tag:String, filePath:String, pos:Array[Float]|Float, ?instantAdd:Bool, ?inFrontOfChars:Bool, ?scrollFactor:Array[Float]|Float, ?scale:Array[Float]|Float, ?camera:String, ?color:String, ?centerType:String, ?shader:String)

All-in-one makeLuaSprite. made as a lua sprite counterpart to the function 'quickText'.

  • tag: Sprite tag
  • filePath: Sprite's file path, starts in the "images" folder (in assets or in mods)
  • pos: Positions in a table. Example: {100, 300}. If both values are the same, use Float instead
  • instantAdd: Do you want to add it without needing to use addLuaSprite() after?
  • inFrontOfChars: Is the sprite in front of the characters?
  • scrollFactor: Scroll Factor of the sprite. Example: {3, 1}. If both values are the same, use Float instead
  • scale: Scale of the sprite. Example: {2, 2.45}. If both values are the same, use Float instead
  • camera: What camera should the sprite be added on?
  • color: FlxColor (color of the sprite)
  • centerType: xy, x, y. Set it to nil if you don't want it to center.
  • shader: Name of the shader from the shaders folder

Example:

I think you already know how this works..

quickGraphic(tag:String, pos:Array[Float]|Float, dimensions:Array[Float], ?color:String, ?instantAdd:Bool, ?inFrontOfChars:Bool, ?scrollFactor:Array[Float]|Float, ?scale:Array[Float]|Float, ?camera:String, ?centerType:String, ?shader:String)

All-in-one makeGraphic. made as a lua graphic counterpart to the function 'quickSprite' and 'quickText'.

  • tag: Sprite tag
  • pos: Positions in a table. Example: {100, 300}. If both values are the same, use Float instead
  • dimensions: Graphic's width and height. If both values are the same, use Float instead
  • color: FlxColor (color of the graphic)
  • instantAdd: Do you want to add it without needing to use addLuaSprite after?
  • inFrontOfChars: Is the graphic infront of the characters
  • scrollFactor: Scroll factor of the graphic. Example: {3, 1}. If both values are the same, use Float instead
  • scale: Scale of the graphic. Example: {2, 2.1}. If both values are the same, use Float instead
  • camera: "game", "hud" or "other"
  • centerType: xy, x, y. Set it to nil if you don't want it to center.
  • shader: Name of the shader the shaders folder

Example:

I think you already know how this works..

Clone this wiki locally