I am developing a turn-based game and I have a player table of the following structure
players = {
["p1"] = Player(),
["p2"] = Player(),
...
["pn"] = Player()
}
What I wanted to do is iterate through each players in the table (after each player played his round) and get back to the first index ("p1" in this case)
So it should do the following stuff when I run the code
function shift()
-- do stuff to shift the player's turn
print(player.name)
end
shift() -- "p1"
shift() -- "p2"
...
shift() -- "pn"
shift() -- "p1"
-- and so on
local index
function shift()
if not index then index = next(players) end
print(players[index].name)
index = next(players, next)
end
That should do what you want, if I understood the question correctly ;)
EDIT:
As Egor Skriptunoff pointed out in his comment, you can also have the function return the key and use and instead of an if:
local index
function shift()
index = next(players,index)
return index or next(players)
end
Your loop should be something along the lines of:
for k, player in pairs(players) do
player:Player()
end
If you want to recall the the first players function then just follow this with:
players[1]:Player()
Hope this helped!
Edit: To make it endless, just put it in a 'repeat until loop' so it would look something like:
repeat
<for loop across all players>
until <condition>
Related
In file1.lua I have something like
require "file2"
outerTable = { ["thing1"] = {"1", "2", "3"}, ["thing2"] = {"4", "5", "6"}}
penultimateThing = callAFunction(outerTable["thing1"])
in file2.lua I have something like
callAFunction(table)
for k,v in ipairs(table) do
print(k, v)
end
end
When I try to pass a nested table like this, it's always nil. What gives?
Maybe another typo, but you are not saying function callAFunction. Without that, I get the error: <eof> expected near end. This means that there is an end where the compiler assumes the file should end. Tracing back we find that we close a for loop and that we want to close the function definition. The function definition, however, is not opened, so cannot be closed either.
Try to change it to:
function callAFunction(table)
for k,v in ipairs(table) do
print(k, v)
end
end
For me, this seems to work
I am getting Components, which are saved in tables(I use a special system for better memory effiency). Then i give them as parameters in a given function(with the unpack statement(I already checked it isnt the reason)). So far so good, I get the value that is saved in the Table. But if I change it the component in the table doesnt change value. So in short: I want to give the component by reference but I am giving it by value. I thought Lua always gives values stored in tables by reference. Any help would be appreciated. If you need any additional resources just ask :). Thankyou in advance
function pool:run(dt)-- runs all active systems
local sprite = self.img
for i, method in ipairs(self.mPool) do
if method[1] then
--finds entities with required components
local matches = {}
for x=1, #self.ePool do
if band(self.ePool[x][1], method[2]) == method[2] then
matches[#matches+1] = x
end
end
--get components of entities
local components = {}
for x=1, #method[3] do
components[x] = {}
local marker=1
local savePosition = 1
for Eid=1, matches[#matches] do-- Eid = entity id
if Eid == matches[marker] then
components[marker][#components[marker]+1] = self.cPool[method[3][x]][savePosition]
marker = marker +1
end
if self.cBool[method[3][x]][Eid] then
savePosition = savePosition +1
end
end
end
--reorder and run as coroutine or function
if method[5] then
for x=1, #components do
coroutine.wrap(method[4])(matches[x], unpack(components[x]), dt)
end
else
for x=1, #components do
method[4](matches[x], unpack(components[x]), dt)
end
end
end
end
end
The problem is that if you set a variable to the value of table content the value not the reference will be set.
I'm trying to create a function that adds unique string to a table. I also wonder how to print the result.
My Code :
local t = {}
function addUniqueString(str)
--what should be here?
end
function printElements()
--what should be here?
end
addUniqueString("apple")
addUniqueString("orange")
addUniqueString("banana")
addUniqueString("apple")
printElements()
The Result I want : (order doesn't matter)
apple
orange
banana
Since the order doesn't matter, you can just add strings as keys to the table:
local t = {}
function addUniqueString(str)
t[str] = true
end
And to list the strings:
function printElements()
for k in pairs(t) do
print(k)
end
end
I made a program on Lua to display a table content, it also display a content of a table that is inside this table
for i in pairs(v) do
if type(v[i])=="table" then
for j in pairs(v[i]) do
if type(v[i][j])=="table" then
print("...")
else
print(i,j,v[i][j])
end
end
else
print(i,v[i])
end
end
My question is, is possible to make a version function of the above that work with undefined number of table inside table like {{{1},1},1} showing something like?
1 1 1 1
1 2 1
2 1
You need a recursive function. See the globals example in the Lua online demo, reproduced below. Call dump with your table instead of _G.
-- globals.lua
-- show all global variables
local seen={}
function dump(t,i)
seen[t]=true
local s={}
local n=0
for k in pairs(t) do
n=n+1 s[n]=k
end
table.sort(s)
for k,v in ipairs(s) do
print(i,v)
v=t[v]
if type(v)=="table" and not seen[v] then
dump(v,i.."\t")
end
end
end
dump(_G,"")
I've started fiddling a lot with lua recently, but I can't for my life figure this out.
Let's say I have a string that looks like this:
s = "a=x a=y b=z a=x"
I want to remove all duplicates and merge the values of duplicate keys into a table, so that I get:
t = {
a = {x,y},
b = {z},
}
I've been pondering about this for way too long. Any help is appreciated!
Try this:
s="a=x a=y b=z a=x"
s=s.." "
t={}
for k,v in s:gmatch("(.-)=(.-)%s+") do
if t[k]==nil then t[k]={} end
t[k][v]=true
end
for k,v in pairs(t) do
for z in pairs(v) do print(k,z) end
end