local num_items = table.getn( self.table ) for i = 1, num_items, 1 do whatever( self.table[i]) end
and
for i in self.table do whatever( self.table[i]) end
?
Posted 28 July 2005 - 10:30 AM
I believe the former will only use items in self.table which have an integer key, whereas the latter will use all items in self.table regardless of what type of key they have.What's the difference between
 local num_items = table.getn( self.table )  for i = 1, num_items, 1 do    whatever( self.table[i])   end
and  for i in self.table do     whatever( self.table[i])   end
?
Edited by Flenser, 28 July 2005 - 10:31 AM.
Posted 28 July 2005 - 12:49 PM
I believe the former will only use items in self.table which have an integer key, whereas the latter will use all items in self.table regardless of what type of key they have.
--run through tactics and remove those that are complete local num_items = table.getn( self.attack_units ) for i = num_items, 1, -1 do if not self.attack_units[i]:IsValid() then Plan.UnlockResource( self, self.attack_units[i] ) table.remove( self.attack_units, i ) end end
Posted 28 July 2005 - 02:28 PM
It does. Integer keys are given by default if no key is specified. Units are added to self.attack_units as follows:-If this is true then this shouldn't work as self.attack_units does not have an integer key, right ?
table.insert( self.attack_units, attack_units[i] )All that does is put the value 'attack_units[i]' at the end of the integer-keyed entries already in self.attack_units. Eg. if self.attack_units had no entries at all, then attack_units[i] would be inserted with a key of 1. LUA begins all tables at 1 and not zero. If self.attack_units had 3 units already added, then the new unit would be added at position 4, and automatically given key 4.
self.attack_units["first_unit"] = attack_units[i]The 'key' here is "first_unit" and the value is "attack_units[i]". (You could change "first_unit" to "1" and you would be expressly using an integer key. Not sure why you would do it this way, but it would work)
table.insert( self.attack_units, attack_units[i] )can be written as
self.attack_units[ ( table.getn( self.attack_units) + 1) ] = attack_units[i]
Edited by Flenser, 28 July 2005 - 02:37 PM.
Posted 28 July 2005 - 02:40 PM
function CpuManager:IsDancer( squad_id ) dbAssert( squad_id ~= nil ) --check if squad is dancing for i in self.no_move_tbl do if self.no_move_tbl[i]:IsValid() and self.no_move_tbl[i]:GetID() == squad_id then return true end end return false end function CpuManager:RemoveDancer( squad_ai ) profile_start("RemoveDancer") dbAssert( squad_ai ~= nil ) --remove dancing squad from table local num_items = table.getn( self.no_move_tbl ) for i = num_items, 1, -1 do if not self.no_move_tbl[i]:IsValid() then table.remove( self.no_move_tbl, i ) --erase invalid from table elseif squad_ai ~= nil and self.no_move_tbl[i]:GetID() == squad_ai:GetID() then table.remove( self.no_move_tbl, i ) --erase specific ID from table end end profile_end("RemoveDancer") end function CpuManager:InsertDancer( squad_ai ) dbAssert( squad_ai ~= nil ) --insert dancing squad table.insert( self.no_move_tbl, squad_ai ) end
Posted 28 July 2005 - 02:59 PM
Posted 29 July 2005 - 08:16 AM
aitimelog("d:\\trace.log", "RemoveDancer 1: "..tostring(num_items)) --TP1 for i = num_items, 1, -1 do dbAssert( self.no_move_tbl[i] ~= nil ) dbAssert( self.no_move_tbl[i]:IsValid() ~= nil ) if not self.no_move_tbl[i]:IsValid() then aitimelog("d:\\trace.log", "Remove Invalid "..tostring(i)) --TP2 table.remove( self.no_move_tbl, i ) --erase invalid from table else aitimelog("d:\\trace.log", "Keep Valid "..tostring(i).." "..tostring(self.no_move_tbl[i]:GetID())) --TP3 end end aitimelog("d:\\trace.log", "RemoveDancer 2: Count") --TP4
Posted 29 July 2005 - 08:46 AM
Posted 29 July 2005 - 09:45 AM
Edited by LarkinVB, 29 July 2005 - 09:47 AM.
Posted 29 July 2005 - 11:10 AM
Posted 09 August 2005 - 11:05 AM
for squad_ai in military_manager:GetUnlockedSquads() do local squad_pos = Vector3f( squad_ai:GetSquad():GetPosition() ) local dist = distance_sqr( squad_pos, some_other_pos )
for squad_ai in military_manager:GetUnlockedSquads() do local squad_pos = squad_ai:GetPosition() local dist = distance_sqr( squad_pos, some_other_pos )
Posted 09 August 2005 - 01:50 PM
Posted 09 August 2005 - 02:05 PM
Posted 09 August 2005 - 02:46 PM
Edited by thudo, 09 August 2005 - 02:47 PM.
0 members, 0 guests, 0 anonymous users