problem with data address

Place to get help with not working mods / modding interface.
User avatar
Savaro
Burner Inserter
Burner Inserter
Posts: 12
Joined: Mon Jul 07, 2014 4:27 pm
Contact:

problem with data address

Post by Savaro »

so making car mod.

target: when car created, save it in list, so i can use it, when player sit in it.
key problem: "event.createdentity" not not equal to "game.player.character.vehicle"

how i try do it:
1. on "car create" i save it link on enity in table

Code: Select all

cars_data.m.add( event.createdentity )

Code: Select all

function	cars_data.m.add(_owner)
	local new_car_data ={}
	new_car_data['owner'] = _owner
2. then when player sit in car check

Code: Select all

		if game.player.character and game.player.character.vehicle 	then
				gui_car = cars_data.m.get( game.player.character.vehicle)
try to find saved data

Code: Select all

function	cars_data.m.get(_owner)	
	local rez=nil
	local cardata= nil
	local t=0
		for t,cardata in pairs(cars_data.data) do
		
			if cardata['owner'] == _owner then
				rez = cardata	
				game.player.print( 'rez = cardata' )
				
			end
		end
	return rez
end
sooo it find nothing.
"event.createdentity" and "game.player.character.vehicle" are valid, have same types, work same and ets.
but they are not equal.

I'm doing something wrong?

Dark
Long Handed Inserter
Long Handed Inserter
Posts: 83
Joined: Wed May 07, 2014 12:45 pm
Contact:

Re: problem with data address

Post by Dark »

Tables in LUA (and entities are tables) are considered different even if both contain same keys and values, condition like 'if table1==table2 then' is generally meaningless.
So, the table that was returned by createentity may not be the same as one returned in event or the one that currently used by game.player.character, but they can contain the same entity.
However, API allows you to use

Code: Select all

entity.equal(other_entity)
to check that 'entity' and 'other_entity' are the same entity regardless of origin of their tables.

User avatar
Savaro
Burner Inserter
Burner Inserter
Posts: 12
Joined: Mon Jul 07, 2014 4:27 pm
Contact:

Re: problem with data address

Post by Savaro »

Dark wrote:Tables in LUA (and entities are tables) are considered different even if both contain same keys and values, condition like 'if table1==table2 then' is generally meaningless.
So, the table that was returned by createentity may not be the same as one returned in event or the one that currently used by game.player.character, but they can contain the same entity.
However, API allows you to use

Code: Select all

entity.equal(other_entity)
to check that 'entity' and 'other_entity' are the same entity regardless of origin of their tables.
I thought that since they refer to the same data - tables and addresses are the same, because there is no point in making any duplicates, since if the address of table is different, then two different tables, although with identical data sets.
all of the above - not a problem. but wildly wondering, if functions return different data types, then, perhaps there are ways to bring them together, otherwise - event.createdentity returns data that does not have any sense.

thanx! miss thiss one

Code: Select all

 entity.equal(other_entity)

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: problem with data address

Post by FreeER »

Savaro wrote:I thought that since they refer to the same data - tables and addresses are the same, because there is no point in making any duplicates.
Tables are the only type in lua (as far as I am aware) that are passed by reference (meaning == compares address not values), perhaps with an exception for string but since they are unchangeable they aren't affected as much. There are occasionally reasons to duplicate tables (sometimes you want a 'new' table with the same values as the original that you can modify independently of the original).
Savaro wrote:if functions return different data types, then, perhaps there are ways to bring them together, otherwise - event.createdentity returns data that does not have any sense.
tables are the only data structure in lua, so... that would be the only way to 'bring them together', however lua does allow multiple return values of different types (unlike the vast majority of programming languages).
what happens (from my understanding) with event.createdentity (and other Factorio functions) is that the entities are all created by C++ and when you ask for a reference to one, C++ creates a new table and pushes a 'lightuserdata' (a pointer to a C++ object) inside of that table, sets a metatable to the table so it will transfer accesses to the C++ object, and then returns the table to you. Due to it creating a new table every time, the address of that table is different every time, even though the entity it pushes inside the table may be the same.

User avatar
Savaro
Burner Inserter
Burner Inserter
Posts: 12
Joined: Mon Jul 07, 2014 4:27 pm
Contact:

Re: problem with data address

Post by Savaro »

FreeER wrote:what happens (from my understanding) with event.createdentity (and other Factorio functions) is that the entities are all created by C++ and when you ask for a reference to one, C++ creates a new table and pushes a 'lightuserdata' (a pointer to a C++ object) inside of that table, sets a metatable to the table so it will transfer accesses to the C++ object, and then returns the table to you. Due to it creating a new table every time, the address of that table is different every time, even though the entity it pushes inside the table may be the same.
sounds realistic, thanx)

Post Reply

Return to “Modding help”