Page 1 of 1
Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 3:40 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 3:52 am
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
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 6:04 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 6:14 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 6:18 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 6:20 am
by Rseding91
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 6:23 am
by posila
Linver wrote: ↑Thu Jul 09, 2020 6:18 amAlso, 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)
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 6:42 am
by Linver
posila wrote: ↑Thu Jul 09, 2020 6:23 am
Linver wrote: ↑Thu Jul 09, 2020 6:18 amAlso, 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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 7:14 am
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
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 8:37 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 8:57 am
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
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 9:13 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Thu Jul 09, 2020 1:16 pm
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Fri Jul 10, 2020 10:09 am
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
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Fri Jul 10, 2020 10:11 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Fri Jul 10, 2020 11:11 am
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Posted: Fri Jul 10, 2020 3:18 pm
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.