Pointer to LuaEquipmentGrid associated LuaEntity

Things that we aren't going to implement
Post Reply
User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

Hi, try to mod something using the LuaEquipmentGrid, I found a very strange a thing; from the API documentation, inside the LuaObject of the LuaEquipmentGrid, there isn't any reference to the LuaEntity that own the Equipment Grid.

Add a property, "entity" read-only, will improve a and simplify the interaction with the grids and respective entities via scripts, because from the events: on_player_placed_equipment and on_player_removed_equipment is impossible derive this information, is unknown what entity was modified, also I think that inside the Factorio object this property should be already available.

As always, thank you for the attention.
Last edited by Linver on Thu Jul 09, 2020 5:43 am, edited 2 times in total.

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

Oh, I'm sorry, seems that my post it's a duplicate: viewtopic.php?f=65&t=85443

Anyway, this show another use case of why this property should be available :lol:

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

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Rseding91 »

This isn't possible to implement. The equipment grid has no idea what entity currently owns it - and may not even be owned by an entity.

So, there's no value to expose to Lua - it just doesn't exist on the C++ side. Sorry.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

Rseding91 wrote:
Thu Jul 09, 2020 6:04 am
..it just doesn't exist on the C++ side. Sorry..

Then is possible add the entity as param in the events on_player_placed_equipment and on_player_removed_equipment? I think if the player is installing the equipment manually, he have some reference in the GUI.
Last edited by Linver on Thu Jul 09, 2020 6:21 am, edited 2 times in total.

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

Rseding91 wrote:
Thu Jul 09, 2020 6:04 am
..and may not even be owned by an entity..
Also, in what situations is this possible? I can't figure out in what state this is legal for the game engine.

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

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Rseding91 »

Linver wrote:
Thu Jul 09, 2020 6:14 am
Rseding91 wrote:
Thu Jul 09, 2020 6:04 am
..it just doesn't exist on the C++ side. Sorry..

Then is possible add the entity as param in the events on_player_placed_equipment and on_player_removed_equipment?
No, the game has no idea where the equipment grid is when those events fire. It just has a pointer to the grid itself and the player who put or removed equipment from it.
Linver wrote:
Thu Jul 09, 2020 6:18 am
Rseding91 wrote:
Thu Jul 09, 2020 6:04 am
..and may not even be owned by an entity..
Also, in what situations is this possible? I can't figure out in what state this is legal for the game engine.
Armor in any inventory: not owned by the player but still has an equipment grid.
If you want to get ahold of me I'm almost always on Discord.

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by posila »

Linver wrote:
Thu Jul 09, 2020 6:18 am
Also, in what situations is this possible? I can't figure out in what state this is legal for the game engine.
Armor equipment grids are not owned by any entity but the armor item.

What are you trying to achieve? There might be other things we could do to help you achieve your goal, instead of trying to go with this specific solution.
For example, some time ago I had an idea of adding equipment item that would have a trigger that would be invoked periodically (or after some amount of energy consumed). But I didn't have time to think it through properly and implement it (I still don't, but you know ... it's good to know what are people actually trying to use equipment grids for)

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

posila wrote:
Thu Jul 09, 2020 6:23 am
Linver wrote:
Thu Jul 09, 2020 6:18 am
Also, in what situations is this possible? I can't figure out in what state this is legal for the game engine.
What are you trying to achieve?
I'm try to monitor an entity(A), that have installed in equipment grid a precise equipment, and when this entity(A) is in range with another entity(B), do something in the grid, like recharge the batteries. I'm trying to avoid to do the reverse, because in my situation is supposed to exist more B entity than A, also, if I can't know all A entities, I have to use more UPS to check this from the B entity.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by eradicator »

Can't you just read LuaPlayer.opened?

Code: Select all

local entity = player.opened and (player.opened.grid == event.grid) and player.opened or nil
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

eradicator wrote:
Thu Jul 09, 2020 7:14 am
Can't you just read LuaPlayer.opened?

Code: Select all

local entity = player.opened and (player.opened.grid == event.grid) and player.opened or nil
In fact I said over:
I think if the player is installing the equipment manually, he have some reference in the GUI.
I think that in a way is possible. But I have to think better how manage the case where this is done via script by other mods.

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

eradicator wrote:
Thu Jul 09, 2020 7:14 am
Can't you just read LuaPlayer.opened?

Code: Select all

local entity = player.opened and (player.opened.grid == event.grid) and player.opened or nil
I try ur code, this is the result:

Code: Select all

on_player_placed_equipment (ID 38)
LuaEquipmentGrid doesn't contain key grid.
But I don't give up

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

eradicator wrote:
Thu Jul 09, 2020 7:14 am
Can't you just read LuaPlayer.opened?
Looking better in the documentation:

Code: Select all

opened :: LuaEntity or LuaItemStack or LuaEquipment or LuaEquipmentGrid or LuaPlayer or LuaGuiElement or defines.gui_type [Read-Write]
The GUI target the player currently has open; nil if none.

Note: Write supports any of the types. Read will return the entity, equipment, element or nil.
Seems that opened return the LuaEquipmentGrid, so have losee too all connections with the entity. Seems truly impossible but I want try to keep in memory the last on_gui_opened associated entity available and use it later.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by eradicator »

Yes, my bad. LuaPlayer.opened already referse to the grid itself at that point. You could indeed try caching opened, but keep in mind that grids can be changed by mods too.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

Rseding91 wrote:
Thu Jul 09, 2020 6:04 am
..This isn't possible to implement...
Making the impossible possible:

Code: Select all

...

-- Return the gui type name from the identifier number
local function getGUITypeName(given_id)
	for name, id in pairs(defines.gui_type) do
		if given_id == id then 
			return name
		end 
	end
end

-- Called when a player open a gui
local function saveLastOpenInformations(event)
	inizializeGlobalLastOpenDictionary()
	if not global.player_last_open_entity[event.player_index] then
		global.player_last_open_entity[event.player_index] = {nil, nil}
	end
	
	local gui_type_name = getGUITypeName(event.gui_type)
	
	if gui_type_name == "controller" and game.players[event.player_index] and game.players[event.player_index].character then
		global.player_last_open_entity[event.player_index] = game.players[event.player_index].character
	elseif gui_type_name == "entity" and event.entity then
		global.player_last_open_entity[event.player_index] = event.entity
	end
end

...

-- Called when player install a new equip in a grid
local function onInstallingEquip(event)	
	local equipment = event.equipment or false
	if equipment.valid and equipment.name == TRIGGER_EQUIP_NAME then
		local grid = event.grid
		if haveOneTriggerEquip(grid) then
			local last_open_entity = global.player_last_open_entity[event.player_index]
			local player = game.players[event.player_index]
			if last_open_entity and last_open_entity.valid then
				addMonitoredGrid(grid, last_open_entity)
				game.print(last_open_entity.name)
			end
		else
			
		end
	end
end

User avatar
Linver
Fast Inserter
Fast Inserter
Posts: 158
Joined: Wed Jan 09, 2019 2:28 pm
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Linver »

eradicator wrote:
Thu Jul 09, 2020 1:16 pm
Yes, my bad. LuaPlayer.opened already referse to the grid itself at that point. You could indeed try caching opened, but keep in mind that grids can be changed by mods too.
Yeah in fact I'm trying to design a way to understand how catch when a mod do this but is not simply, I will try to ask for this first that make this simple.
Anyway if u want look the code that I post in the previous message, do the work.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by eradicator »

Code: Select all

	local gui_type_name = getGUITypeName(event.gui_type)
	if gui_type_name == "controller" and game.players[event.player_index] and 
	
Iterating defines on every player click is highly inefficient. Just compare the id...

Code: Select all

if event.gui_type == defines.gui_type.controller ...
Also if you don't compare the grid you might end with really annoying bugs if anything interfers with the opening process.

Code: Select all

if (last_open_entity.grid == event.grid) then
But that's all off-topic.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: Pointer to LuaEquipmentGrid associated LuaEntity

Post by Rseding91 »

That logic is also flawed: put armor in a chest, open the chest, then open the armor. Now an inserter comes by and takes the armor. Or another player does.

The chest no longer has the armor so "last opened entity" is going to be completely useless.

The same applies for armor in the player inventory and then a robot takes the armor away while you have it open.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Won't implement”