Hello. There is such a function:

function s:checkpos(x, y) for _, o in pairs(self.objects) do if ox == x and oy == y then return o else return nil end end end 

where objects is a table with other tables with values ​​under the keys x and y . The for loop for some reason does not go through all the elements of the table, but stops at the first one with coordinates 50, 50. At the same time, another function:

 function s:update(dt) for _, o in pairs(self.objects) do if o.update then o:update(dt) end end end 

It works as it should, i.e. the update function is called for each table element, if it exists. Help please find a mistake!

  • What is the mistake? Please provide her text. Why in the function checkpos cycle? - mymedia

1 answer 1

You write return after the first iteration. If you need to find an object with the necessary coordinates, then the code should be such

 function s:checkpos(x, y) for _, o in pairs(self.objects) do if ox == x and oy == y then return o // если нашли - выходим end end // если дошли сюда, значит пробежали весь цикл и не нашли. Тогда возвращаем nil return nil end 
  • Thank you, wrote a long time, did not notice. - stdio.h