Page 1 of 1

There is no table in .entity

Posted: Sun Feb 26, 2017 11:03 am
by darkfrei
Hi!

I don't know why, but there is nothing in .entity

When I try get all attributes of it, I get only "__self" and nothing else.

Code: Select all

function EntityInfo (entity)
	for i, j in pairs (entity) do
		someFunction(i, j)
	end
end
And SomeFunction(i, j) have nothing, but "__self". Why it's not a table?

Re: There is no table in .entity

Posted: Sun Feb 26, 2017 11:08 am
by darkfrei
But this entity have a lot of parameters, code like this works good:

Code: Select all

local parameters = {"name", "type", "rotatable", "health",	"direction", "orientation"}
	for i, j in pairs(parameters) do
		someFunction (j, entity[j])
	end
I see it's the same result like must be, but I need to define all of parameters what I need.

Re: There is no table in .entity

Posted: Sun Feb 26, 2017 12:03 pm
by Adil
Well, you know the truth now, the entity (and pretty much any other object your script receives from the game) is actually a table with a single field: __self.
That __self is a pointer to the c++ object, that actually holds all the data. Lua does not know how to operate with c++ objects, so programmers implement some C++ to Lua functions that allow Lua to do things. Then functions relevant to an entity object, for example are packed into a table and that table is assigned as metatable to the reference you have.

So, all the entity fields are actually stored in __self, when you type, for example: `print(entity.name)` you are actually calling __index methametod that pulls the needed information from __self. Developers decided not to implement __pairs method, so lua simply gets values in the particular table.

If you want to obtain values of many fields, you will have to explicitly poll those.

addenum: those tables are also kind of protected from tampering, so you won't be able to redefine or add fields if you'd want to.
addenum2: well, they are not actually protected, but close familiarity with lua and factorio modding will be required to do interesting stuff with that, and that still will be needless complication.

Re: There is no table in .entity

Posted: Sun Feb 26, 2017 9:07 pm
by Rseding91
All of the methods/properties for an entity are available here: http://lua-api.factorio.com/latest/LuaEntity.html

There's no point in iterating them in-game.