Page 1 of 1

Lua subtility ?...

Posted: Mon Jul 18, 2016 11:33 am
by binbinhfr
Hi,

can someone explain me why this is not working :

Code: Select all

	tab= {}
			
	for _, ent in pairs(surface.find_entities_filtered{area=a, type="assembling-machine"}) do
		tab[ent] = {m=1, n=2 } -- i create a table indexed by entities
	end

	for _, ent in pairs(surface.find_entities_filtered{area=a, type="assembling-machine"}) do -- then I try to access this table I created before
		local ref = tab[ent] 
				-- here, ref contains nil : it does not point to the object I created in the previous loop!
	end
if I use table.insert(tab, {entity=ent,m=1, n=2 }), then I can travel through this table and find back the entity, but it is not really indexed...

Re: Lua subtility ?...

Posted: Mon Jul 18, 2016 12:33 pm
by binbinhfr
Well it seems that I understood. I cannot search by index on an entity "structure", because even if it describes the same factorio object, it is not always the same table/structure behind (here because of 2 different calls to get_entity_filtered)...
So it would be better to index my table by a unique ID that defines the factorio entity. Like a entity.index ? But there is not such things no ? Why not ? Like player.index ?

Re: Lua subtility ?...

Posted: Mon Jul 18, 2016 12:52 pm
by Rseding91
Entities with an owner have entity.unit_number. Things without an owner: trees, resources, doodads have no identifying number associated except the memory address they occupy on the computer.

Re: Lua subtility ?...

Posted: Mon Jul 18, 2016 1:07 pm
by binbinhfr
Great ! thanks rseding, that was what I needed.