Pointer to LuaEquipmentGrid associated LuaEntity
Pointer to LuaEquipmentGrid associated LuaEntity
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.
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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
Anyway, this show another use case of why this property should be available
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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.
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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:14 am
Then is possible add the entity as param in the events on_player_placed_equipment and on_player_removed_equipment?
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.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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
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.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
In fact I said over: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 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.I think if the player is installing the equipment manually, he have some reference in the GUI.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
I try ur code, this is the result: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
Code: Select all
on_player_placed_equipment (ID 38)
LuaEquipmentGrid doesn't contain key grid.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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
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.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.
Anyway if u want look the code that I post in the previous message, do the work.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Pointer to LuaEquipmentGrid associated LuaEntity
Code: Select all
local gui_type_name = getGUITypeName(event.gui_type)
if gui_type_name == "controller" and game.players[event.player_index] and
Code: Select all
if event.gui_type == defines.gui_type.controller ...
Code: Select all
if (last_open_entity.grid == event.grid) then
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Pointer to LuaEquipmentGrid associated LuaEntity
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.
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.