There is no table in .entity

Place to get help with not working mods / modding interface.
Post Reply
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

There is no table in .entity

Post 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?
Last edited by darkfrei on Sun Feb 26, 2017 11:08 am, edited 1 time in total.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: There is no table in .entity

Post 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.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: There is no table in .entity

Post 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.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: There is no table in .entity

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Modding help”