Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a individual code move library efficiently?

Asked by 4 years ago
Edited 4 years ago

Hey! I'm working on a project for myself revolving around turn-based combat. I was wondering what's the best way to select an attack through several different moves library?

All moves are different so the code has to be individual. What I'm doing right now works but I'd like to learn to be more efficient or this is the only way, which I doubt.

To put it brief, here is what I am doing:


local function MOVE_LIBRARY(ATTACK_NAME) if ATTACK_NAME == 'Attack1' then print('ATTACK 1!') -- Every move is different which is why they need to be done individually. elseif ATTACK_NAME == 'Swipe2' then print('Swipe 2....!') elseif ATTACK_NAME == 'Punch0' then print('Punch 0..?') end -- Imagine a bunch more of these "elseifs" end MOVE_LIBRARY('Attack1') -- It will get the attack (of course this process is much longer finding which specific attack you'd want to select.)

I don't believe tables would work to this degree since again, they are specific.

To me this seems very messy and inefficient, is there any more efficient way to do this or am I overthinking this? If I am being to unspecific just let me know! Thank you!

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

You could keep a module with all of the attacks as function names.

Module:

local Module = {}

function Module:Punch()
    -- punch that thing
end

function Module:Attack()
    -- attack ok
end

return Module

Script:

local Module = require(Module)
local ATTACK_NAME = 'Punch'

Module[ATTACK_NAME]()
0
Are modules called from a local script client-sided? And thank you! rareheaddress 74 — 4y
0
Yes they are client sided if called from a local script. Nickuhhhhhhhhhhhhhhh 834 — 4y
Ad

Answer this question