Does this code snippet do anything?

Post Reply
Eran
Member
Posts: 123
Joined: Fri May 03, 2019 16:46

Does this code snippet do anything?

by Eran » Post

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?

User avatar
duane
Member
Posts: 1703
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: Does this code snippet do anything?

by duane » Post

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.
Believe in people and you don't need to believe anything else.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests