Page 1 of 1
How do I close the GUI by distance?
Posted: Tue Mar 03, 2026 10:59 pm
by yaim904
I have created a mod that opens a GUI for an entity, but the game does not close the GUI when the player moves away from the entity.
For now, I am using the on_tick event and Player.build_distance, but it is not working well.
Some MODs change the interaction area for players, so I can't just assign a number.
Does anyone have any idea how to make it work?
Re: How do I close the GUI by distance?
Posted: Wed Mar 04, 2026 10:35 pm
by Osmo
I think reach_distance would be more appropriate (but its not documented what the differences are). If player.reach_distance doesn't return a value affected by bonuses, you can multiply it yourself by
https://lua-api.factorio.com/latest/cla ... ance_bonus.
Re: How do I close the GUI by distance?
Posted: Fri Mar 06, 2026 12:52 am
by yaim904
Osmo wrote: Wed Mar 04, 2026 10:35 pm
It's not the value I'm looking for.
With both data sets, you should be within range to open the GUI.
Code: Select all
--- Close-up data
[1] = {
['pPos'] = {
['y'] = 8.99609375,
['x'] = -8.69921875
},
['ePos'] = {
['y'] = 5.5,
['x'] = -4.5
},
['Distance_max'] = 10,
['character_reach_distance_bonus'] = 0,
['distance'] = 6.8736260168226
}
--- Distant data
[1] = {
['pPos'] = {
['y'] = 17.30859375,
['x'] = -8.69921875
},
['ePos'] = {
['y'] = 5.5,
['x'] = -4.5
},
['Distance_max'] = 10,
['character_reach_distance_bonus'] = 0,
['distance'] = 13.82360117563
}
The following is the code I am using. It
only works on my MOD and is for reference only.
Code: Select all
function This_MOD.validate_distance(Data)
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- Renombrar
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
local pPos = Data.Player.position
local ePos = (Data.GUI.entity or Data.Entity).position
local Distance_max = Data.Player.build_distance
local Bonus = Data.Force_player.character_reach_distance_bonus
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- Calcular la distancia entre el jugador y la entidad
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
local dX = math.abs(pPos.x - ePos.x) + 1
local dY = math.abs(pPos.y - ePos.y) + 1
local Distance = math.sqrt(dX * dX + dY * dY)
GMOD.var_dump({
pPos = pPos,
ePos = ePos,
Distance_max = Distance_max,
character_reach_distance_bonus = Bonus,
distance = Distance
})
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
--- Cerrar el GUI si el jugador está lejos de la entidad
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
if Distance > Distance_max + Bonus and Data.GUI.frame_main then
Data.GUI.action = This_MOD.action.close_force
This_MOD.toggle_gui(Data)
return false
end
return true
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
end
Re: How do I close the GUI by distance?
Posted: Fri Mar 06, 2026 2:47 am
by Osmo
I don't see what the issue is
Re: How do I close the GUI by distance?
Posted: Fri Mar 06, 2026 9:20 pm
by yaim904
Osmo wrote: Fri Mar 06, 2026 2:47 am
I have two elements on the map, the entity and the player.
The player has a radius of action where interaction with the entity is allowed.
When the player opens an entity's GUI and moves away, at the limit of interaction with the entity, the GUI closes.
When you hover the mouse over the entity, the indicator appears in yellow or red depending on the distance.
I want my GUI to respond to distance. If it moves too far away, and if it is close, it lets you open the GUI, taking into account changes from other MODs.
The problem: how do you calculate the player's range of action?
Above is the code I'm using, but it's not giving the desired results. The maximum distance is less than the player's range, both data sets are within the player's range of action, but only the nearby one is considered valid.
I don't know how, or if it's possible, to calculate that range.
Re: How do I close the GUI by distance?
Posted: Sat Mar 07, 2026 1:13 am
by Osmo
You are adding 1 to dX and dY for some reason, which makes distance calculation wrong.
Re: How do I close the GUI by distance?
Posted: Sat Mar 07, 2026 4:38 am
by yaim904
Osmo wrote: Sat Mar 07, 2026 1:13 am
Maybe, but I don't think that makes much difference after a radical change.