I would like to query my current game (through console) how many ores per type are in the visible area.
I already found some lua commands in the wiki which are probably helpful:
print
game.player.surface.find_entities_filtered{type="resource"})
but I seem unable to wire them up correctly - I don't get something printed, nor do I see error messages. For example
/c print(game.player.surface.find_entities_filtered({type="resource"})[0])
does not output anything. Any ideas?
How do I determine the total resources visible?
Re: How do I determine the total resources visible?
Two isses:
One: LUA uses 1 not 0 as the start of an array.
Two: game.print can only print strings. Find entities returns a table of the found entities (or nil). If you want to print a table you need to run it through serpent.block(table) first, which converts it to a string formatted for easier reading.
Note that attempting to print a LUAEntity isn't very useful - it just has a __self attribute, though it can be useful to check if it's nil. This is normal and you can still access the .name or .amount attributes for what you're trying to do.
will show the first found resource's name and amount of resources.
One: LUA uses 1 not 0 as the start of an array.
Two: game.print can only print strings. Find entities returns a table of the found entities (or nil). If you want to print a table you need to run it through serpent.block(table) first, which converts it to a string formatted for easier reading.
Code: Select all
/c game.print(serpent.block(game.player.surface.find_entities_filtered{type="resource"}[1]))
Code: Select all
/c game.print(serpent.block(game.player.surface.find_entities_filtered{type="resource"}[1].name))
game.print(serpent.block(game.player.surface.find_entities_filtered{type="resource"}[1].amount))
Re: How do I determine the total resources visible?
Just kind of poking around in the API, I came up with this:
I’m not sure if the player’s zoom level is available anywhere. The property I found is write-only for some reason.
Code: Select all
local function viewable_area(player, zoom)
local p = player.position
local r = player.display_resolution
zoom = (zoom or 1) * 64
return {left_top = {x = p.x - r.width / zoom, y = p.y - r.height / zoom},
right_bottom = {x = p.x + r.width / zoom, y = p.y + r.height / zoom}}
end
local function count_visible_resources(player, zoom)
local entities = player.surface.find_entities_filtered{area = viewable_area(player, zoom), type = "resource"}
local resources = {}
for _, entity in ipairs(entities) do
resources[entity.name] = (resources[entity.name] or 0) + entity.amount
end
return resources
end
for name, amount in pairs(count_visible_resources(game.player)) do
game.player.print(name..": "..amount)
end
Re: How do I determine the total resources visible?
Many thanks for your replies, was very helpful. Especially the working code. I meant the whole rendered map when I said "visible area", but I managed to remove the area calculation and constraint. Sorry if you did more work than actually needed, just because I am just too tired this week to clearly state my problem.
Re: How do I determine the total resources visible?
I realized shortly after posting, but I figured it would have that effect. Glad it worked out.