How do I close the GUI by distance?

Place to get help with not working mods / modding interface.
User avatar
yaim904
Fast Inserter
Fast Inserter
Posts: 153
Joined: Wed Nov 17, 2021 11:26 pm
Contact:

How do I close the GUI by distance?

Post 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?
Solo entiendo español, pero si tu también lo entiendes, escríbeme
:D
Everything i write in English is translated by Deepl.
:D
User avatar
Osmo
Fast Inserter
Fast Inserter
Posts: 243
Joined: Wed Oct 23, 2024 12:08 pm
Contact:

Re: How do I close the GUI by distance?

Post 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.
User avatar
yaim904
Fast Inserter
Fast Inserter
Posts: 153
Joined: Wed Nov 17, 2021 11:26 pm
Contact:

Re: How do I close the GUI by distance?

Post 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
Solo entiendo español, pero si tu también lo entiendes, escríbeme
:D
Everything i write in English is translated by Deepl.
:D
User avatar
Osmo
Fast Inserter
Fast Inserter
Posts: 243
Joined: Wed Oct 23, 2024 12:08 pm
Contact:

Re: How do I close the GUI by distance?

Post by Osmo »

I don't see what the issue is
User avatar
yaim904
Fast Inserter
Fast Inserter
Posts: 153
Joined: Wed Nov 17, 2021 11:26 pm
Contact:

Re: How do I close the GUI by distance?

Post 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.
Solo entiendo español, pero si tu también lo entiendes, escríbeme
:D
Everything i write in English is translated by Deepl.
:D
User avatar
Osmo
Fast Inserter
Fast Inserter
Posts: 243
Joined: Wed Oct 23, 2024 12:08 pm
Contact:

Re: How do I close the GUI by distance?

Post by Osmo »

You are adding 1 to dX and dY for some reason, which makes distance calculation wrong.
User avatar
yaim904
Fast Inserter
Fast Inserter
Posts: 153
Joined: Wed Nov 17, 2021 11:26 pm
Contact:

Re: How do I close the GUI by distance?

Post 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.
Solo entiendo español, pero si tu también lo entiendes, escríbeme
:D
Everything i write in English is translated by Deepl.
:D
Post Reply

Return to “Modding help”