Gaidlo
(Gaidlo)
1
local String
for x = 1, #Players do
for i = 1, 2 do
String = "Player_" .. x .. "_Card_" .. i
print(String)
end
end
In my game each player has 2 cards, I want to represent these cards via an index in an array.
Player_1_Card_1 = Shuffled_Deck[1]
Player_1_Card_2 = Shuffled_Deck[2]
Is there a way to create these variables during runtime? Creating these variables during runtime would greatly reduce the amount of code needed.
There’s a ‘hacky’ way you can using function environments, as seen in this example:
local function declareVar(key, value)
local env = getfenv()
env[key] = value
setfenv(1, env)
end
for i = 1, 10 do
declareVar("Player"..i, 2)
end
Although I would not recommend it.
However, could you not just use a dictionary to store the player’s cards with the dictionary’s keys being the player instances?
local playerCards = {}
playerCards.C_Sharper = {}
playerCards.C_Sharper.Card1 = Shuffled_Deck[1]
playerCards.C_Sharper.Card2 = Shuffled_Deck[2]
You can also utilize for loops to manipulate the data inside of the dictionary, instead of writing it like this.
2 Likes
Forummer
(Forummer)
3
Just use a dictionary instead.
1 Like
Gaidlo
(Gaidlo)
4
Your right, using dictionaries would be better.