I'm creating a multiplayer game that allows multiple characters to be controlled by the same keyboard (Super Smash Brothers, essentially). As a result, I have the same set of variables for each character, and my code looks like this:
player1 = { idle = true; jumping = false; -- Et cetera } player2 = { idle = true; jumping = false; -- You get the idea, hopefully. }
How might I be able to create the same effect without taking up a ton of script space? I would assume that for loops are the way to go, but is there any other way of doing it?
You could do like gskw said or also do
local table1 = { 2, 1 } local table2 = { unpack(table1) }
which is essentially the same thing but stands in a single line.
Note however that if table1 contains non-numerical indexes, they will be lost.
You can do this using a very basic for loop, i.e.
local table1 = {a = 2, b =1}; local table2 = {}; for key, val in next, table1 do -- For every key in table 1 table2[key] = val; -- Copy the key-value pair over to table 2 end