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:

01local function MOVE_LIBRARY(ATTACK_NAME)
02  if ATTACK_NAME == 'Attack1' then
03  print('ATTACK 1!') -- Every move is different which is why they need to be done individually.
04  elseif ATTACK_NAME == 'Swipe2' then
05  print('Swipe 2....!')
06  elseif ATTACK_NAME == 'Punch0' then
07  print('Punch 0..?')
08  end -- Imagine a bunch more of these "elseifs"
09end
10 
11MOVE_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:

01local Module = {}
02 
03function Module:Punch()
04    -- punch that thing
05end
06 
07function Module:Attack()
08    -- attack ok
09end
10 
11return Module

Script:

1local Module = require(Module)
2local ATTACK_NAME = 'Punch'
3 
4Module[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