Page 1 of 1

Does this code snippet do anything?

Posted: Thu May 30, 2019 11:49
by Eran
The pathfinding code of Minetest Defense contains this piece of code:

Code: Select all

local players = minetest.get_connected_players()
	if #players then
		for _,p in ipairs(players) do
			local x, y, z = get_player_pos(p)

			local sector = find_containing_sector(class, x, y, z)
			if not sector then
				Queue.push_back(sector_seeds, {x,y,z, nil,0})
				should_refresh_distances = true
			else
				local distance = sector.distance
				if distance == nil or distance > 0 then
					should_refresh_distances = true
				end
			end
		end
end
(https://github.com/Kalabasa/minetest_de ... finder.lua, line 388+)

From my understanding of lua in the snippet

Code: Select all

if #players then
#players never evaluates to a nil or false and thus will always be interpreted as true. Am I missing something?

Re: Does this code snippet do anything?

Posted: Thu May 30, 2019 15:16
by duane
That looks like an error to me. Zero is not false in lua, making it one of the more sensible languages I've used.

Anyway, the if statement is pointless since the loop will execute zero times if the table is empty.