Page 1 of 1

Plz help: LUA tables and For Loops

Posted: Thu Feb 25, 2010 7:40 am
by roflamingo
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

Posted: Thu Feb 25, 2010 8:47 am
by Ace Rimmer
GetTeamID (someID)
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

Posted: Thu Feb 25, 2010 9:05 am
by roflamingo
I just want it to loop against the TeamIDs... your code is looping through every city.

I know I have 2 TeamIDs in my allteams{} table, but I don't know how to iterate and present them as the first argument in IsValidTerritory.

Posted: Thu Feb 25, 2010 9:29 am
by Ace Rimmer
Well, why didn't you say so?!

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. :roll: :P

Edit: I did test it and it works, of course ipairs would do it in 'order'.

Posted: Thu Feb 25, 2010 10:00 am
by roflamingo
Does counter ever go over 1? It doesn't seem to :shock:

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! :!:

Posted: Thu Feb 25, 2010 3:48 pm
by Ace Rimmer
roflamingo wrote:Does counter ever go over 1? It doesn't seem to :shock:

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.

Posted: Thu Feb 25, 2010 9:53 pm
by roflamingo
Problem #2

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   
   

Posted: Thu Feb 25, 2010 9:57 pm
by Montyphy
Guessing you want:

Code: Select all

table.insert(MyWaterMap, {x+dx, y+dy})


and not:

Code: Select all

table.insert(MyWaterMap, x+dx, y+dy)

Posted: Thu Feb 25, 2010 10:03 pm
by roflamingo
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.

Posted: Thu Feb 25, 2010 10:12 pm
by Montyphy
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?

Posted: Fri Feb 26, 2010 12:22 am
by roflamingo
Got it figured out...finally. :D

Posted: Fri Feb 26, 2010 12:41 am
by roflamingo
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 :cry:

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

Posted: Fri Feb 26, 2010 12:49 am
by Ace Rimmer
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.

Posted: Fri Feb 26, 2010 12:56 am
by roflamingo
Figured out 4 different things. and just figured this one out. Baaad typo in the code I just posted. LOL! :D