Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a individual code move library efficiently?

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

✓ Accepted answer

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]()

2 comments

0

Are modules called from a local script client-sided? And thank you!

0

Yes they are client sided if called from a local script.

This discussion is preserved as it was. New answers, comments, and votes are closed.