Problem with require, one collection, two individual characters

this is a problem I have been trying to fix for several days, but I cannot find a solution to it…

I am making a fighting game, where the player characters are represented by one universal collection, which contains two instances of the same collection (representing the characters). Inside is the ‘Universal’ code which applies to all characters, and a lua file which contains functions dedicated to how each individual character operates.

Each character calls a third object which then returns a require address directly depending on player type, which then is executed on an individual basis, allowing unique stats from the init function.

The functions have all the same names between versions, and In normal gameplay, I can distinguish base functions like Init and Update between the two characters. However with non-local functions (remember these are LUASCRIPTS), are triggered on both entities, regardless if only one instanced is ‘hooked’ on the require.

Is there any way around this?

What does the code in the modules look like?

function GetPropertiesInit()
    return stats.HEALTH, stats.MOVESPEED, stats.DASHSPEED, stats.JUMPHEIGHT, stats.FALLSPEED, stats.RISESPEED
end

function GetStateMachineInit()
    return StateList
end

function GetFreeMove()
    return FreeMove
end

function check()
    print("checkfunc")
end

function CharSpecStateExit(self)

end

function CharSpecStateEnter(self)
    if self.State == "GroundLight" then
        --	function HitboxSpawn(x, y, size, duration, delay, type, status, damage, stun, xVel, yVel, self)
        HitboxSpawn(130, 0, 120, 10, 0, 0, 0, 200, 30, 1, 1, self)
        if self.FacingLeft == true then
            self.Velocity.x = self.Velocity.x - 500
        else
            self.Velocity.x = self.Velocity.x + 500
        end
    end
end

stuff like this mostly, using global funtions in lua scripts, called by the state machine

Using global functions like that is a bad idea, since it leads to exactly this problem where loading a new module anywhere in the game overwrites the functions of whatever module was loaded before. Instead, store the module’s code in a table like is shown in the module manual:

local M = {}

function M.GetPropertiesInit()
    return stats.HEALTH, stats.MOVESPEED, stats.DASHSPEED, stats.JUMPHEIGHT, stats.FALLSPEED, stats.RISESPEED
end

function M.GetStateMachineInit()
    return StateList
end

-- etc...

return M
2 Likes

How do you use these functions in specific?