Plz help: LUA tables and For Loops
Moderator: Defcon moderators
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
Plz help: LUA tables and For Loops
I cannot figure this out. So maybe one of you can take a second and tell me what the right thing is here.
The specific line below that is grief is the 'IsValidTerritory'. we have the allteams table. the first argument in IsValidTerritory is a teamid. But the line dies and says I am supplying nil.
What am I supposed to put there so that I iterate through all the team IDs to check this particular part of the globe?
[code]
allteams = GetAllTeamIDs()
SendChat( # allteams)
counter = 0
for i, team in next, allteams do
SendChat(i)
if IsValidTerritory (team:GetTeamID(), x+dx, y+dy, True) then
counter = counter + 1
end
end
The specific line below that is grief is the 'IsValidTerritory'. we have the allteams table. the first argument in IsValidTerritory is a teamid. But the line dies and says I am supplying nil.
What am I supposed to put there so that I iterate through all the team IDs to check this particular part of the globe?
[code]
allteams = GetAllTeamIDs()
SendChat( # allteams)
counter = 0
for i, team in next, allteams do
SendChat(i)
if IsValidTerritory (team:GetTeamID(), x+dx, y+dy, True) then
counter = counter + 1
end
end
- Ace Rimmer
- level5
- Posts: 10803
- Joined: Thu Dec 07, 2006 9:46 pm
- Location: The Multiverse
GetTeamID (someID)
cityID:GetTeamID ()
unitID:GetTeamID ()
You are trying to pass TeamID as cityID or unitID, that's the problem. In short:
I'm sure others will come along and give you a real answer, but here goes nothing...
cityID:GetTeamID ()
unitID:GetTeamID ()
You are trying to pass TeamID as cityID or unitID, that's the problem. In short:
Code: Select all
teamID:GetTeamID()
I'm sure others will come along and give you a real answer, but here goes nothing...
Code: Select all
function TestTest2()
allteams = GetAllTeamIDs()
SendChat( # allteams)
counter = 0
cities = GetCityIDs()
for i, city in ipairs(cities) do
SendChat(i)
if IsValidTerritory (city:GetTeamID(), math.random(-180,180), math.random(-90,90), True) then
counter = counter + 1
end
end
SendChat(counter .. " counter")
end
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
- Ace Rimmer
- level5
- Posts: 10803
- Joined: Thu Dec 07, 2006 9:46 pm
- Location: The Multiverse
Well, why didn't you say so?!
Actually, I should have seen that.
Edit: I did test it and it works, of course ipairs would do it in 'order'.
Code: Select all
function TestTest2()
allteams = GetAllTeamIDs()
SendChat( # allteams)
counter = 0
for i, team in next, allteams do
SendChat(i)
if IsValidTerritory (team, math.random(-180,180), math.random(-90,90), True) then
counter = counter + 1
end
end
SendChat(counter .. " counter")
end
Actually, I should have seen that.
Edit: I did test it and it works, of course ipairs would do it in 'order'.
Smoke me a kipper, I'll be back for breakfast...
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
- Ace Rimmer
- level5
- Posts: 10803
- Joined: Thu Dec 07, 2006 9:46 pm
- Location: The Multiverse
roflamingo wrote:Does counter ever go over 1? It doesn't seem to
Ya the proper argument is <team>.
If I am not precise with my terms, I apologize... I guess I am not used to talking about coding!
Well, it will if it (in my code anyway) hits a random spot that meets the criteria. Mine uses math.random because I didn't know what x+dx, y+dy were. It only loops once for each team, so it only get's one shot per team.
I have been able to get the counter up to four with six players.
Smoke me a kipper, I'll be back for breakfast...
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
Problem #2
I build an array called MyWaterMap. In that array I (am hopefully) inserting co-ordinates of my ocean as follows:
Now I want to look at all the points in my array iteratively to evaluate if those points are shared points. How do I write my FOR loop and my IF statement?
Below: FOR/IF not working...
I build an array called MyWaterMap. In that array I (am hopefully) inserting co-ordinates of my ocean as follows:
Code: Select all
MyWaterMap = {}
Outer Loop that does stuff
if IsValidPlacementLocation (x + dx, y+ dy, "Sub") then
DrawWhiteboardCross(x + dx, y + dy, 0.2)
table.insert(MyWaterMap, x+dx, y+dy)
end
Now I want to look at all the points in my array iteratively to evaluate if those points are shared points. How do I write my FOR loop and my IF statement?
Below: FOR/IF not working...
Code: Select all
for i, ocean in next, MyWaterMap do
if IsValidTerritory (team, ocean[1], ocean[2], True) then
counter = counter + 1
end
Multithreading.YieldLongTask()
end
Guessing you want:
and not:
Code: Select all
table.insert(MyWaterMap, {x+dx, y+dy})
and not:
Code: Select all
table.insert(MyWaterMap, x+dx, y+dy)
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
roflamingo wrote:I want each separate line in the table to be x,y co-ordinates...
and then to be able to iterate through each x,y co-ordinate pair to evaluate for a condition. And perhaps after build a new table if that condition is true with the elements that meet the condition.
Yeah, but you're using table.insert wrong. Here are the definitions for it:
table.insert(table, val)
table.insert(table, pos, val)
By giving 3 arguments your x value is setting the position of your y value in the table. What you need to do is insert the values as a table (by enclosing them with curly braces). Your for loop already expects each item in your table to be a table, hence ocean[1] and ocean[2].
May be this will make it clearer:
Code: Select all
ocean = {x+dx, y+dy}
table.insert(MyWaterMap, ocean)
Also, what's the next in the for loop?
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
Ace Rimmer wrote:I have been able to get the counter up to four with six players.
Using this code? My counter is always either 0 or 1
Code: Select all
for i = 1, # MyWaterMap do
counter = 0
for j, team in next, allteams do
if IsValidTerritory (team, MyWaterMap[i].oceanLong, MyWaterMap[i].oceanLong, True) then
counter = counter + 1
Multithreading.YieldLongTask()
end
end
SendChat( i .. " - " .. counter .. " - " .. MyWaterMap[i].oceanLong .. " - " .. MyWaterMap[i].oceanLat)
if counter > 1 then
SendChat("Bonzai")
DrawWhiteboardCross(MyWaterMap[i].oceanLong, MyWaterMap[i].oceanLong, 3)
end
Multithreading.YieldLongTask()
end
- Ace Rimmer
- level5
- Posts: 10803
- Joined: Thu Dec 07, 2006 9:46 pm
- Location: The Multiverse
Thought you had it figured out?
Also, can somebody join and spec? Whomever BOT TestBot is keeps getting kicked/dropped and I want to see if it happens to everybody. Bonus: my fleet movement basic combat code pseudo works. That is, ships react and launch bombers.
Also, can somebody join and spec? Whomever BOT TestBot is keeps getting kicked/dropped and I want to see if it happens to everybody. Bonus: my fleet movement basic combat code pseudo works. That is, ships react and launch bombers.
Smoke me a kipper, I'll be back for breakfast...
- roflamingo
- level3
- Posts: 404
- Joined: Fri Jan 19, 2007 10:25 am
Who is online
Users browsing this forum: No registered users and 4 guests