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 copies only functions IF there are not already present ... may I suggest this version ? #27

Open
LKSLukeStrike opened this issue Jan 3, 2025 · 0 comments

Comments

@LKSLukeStrike
Copy link

Because I my code I also need to have access to variables and be able to "force" even in case of already present this is my version of "implement"

`function Classic:defaultArg(_arg, _default, ...) -- return _arg if _arg exists in allowed ... else return _default
for _, _allowed in ipairs({...}) do
if _arg == _allowed then return _arg end
end
return _default
end

function Classic:implement(_new, _type, ...) -- implement vars or fcts or both (depending of _type) if they exist or not (depending of _new)
_new = Classic:defaultArg(_new, "all", "new", "all") -- new = only if not existing, all = even existing
_type = Classic:defaultArg(_type, "all", "var", "fct", "all") -- only vars, fcts or all
for _, _classic in ipairs({...}) do
for _key, _val in pairs(_classic) do
if self[_key] ~= nil and _new == "new" then break end
if type(_val) == "function" and _type == "vars" then break end
self[_key] = _val
end
end
end
`
Notes:

  • I have changed "Object" into "Classic" ... after all the lib is called "CLassic" isn't it ?
  • defaultArg is just an helper to assign a default value to an arg if it not allowed

Now I can call implement like this:
`local CCharacter = CEntity:extend() -- characters

CCharacter:implement(nil, nil, CSpriteFG)
`
Hope it helps ?
LKS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant