Page 1 of 1

Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Sun Jan 01, 2023 12:59 pm
by KoRaLLL
Hi all, I was just wondering if there was any set variable or entity value for this 'on-hover' state.

I'm talking about the effect that appears when you hover over an entity (for example, an assembly machine) that is part of something else's radius (e.g, a beacon), so that when you hover on the assembly machine, it highlights the beacon that the assembly machine resides in.

Is there a way to replicate this for other entities? E.g, I want to create a custom entity called X, and whenever there's any walls within entity X, I want entity X to be highlighted when the cursor goes over any of those walls.

Is this do-able? Or is this something that lies in the core game functionality?

I couldn't find anything to do with this 'on-hover' state under any Prototypes, so I'm imagining this is something that's built into the beacons/power structures so that they highlight whenever you hover over an entity that's utilizing these things.

Thanks!

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Sun Jan 01, 2023 1:04 pm
by Deadlock989
The on_selected_entity_changed event fires when an entity is "selected" (in Factorio API jargon this means highlighting the entity with a mouse-over):

https://lua-api.factorio.com/latest/eve ... ty_changed

You can use this event to detect when something is high-lit by the player and search for other entities in range that meet your criteria with surface.find_entities_filtered().

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Sun Jan 01, 2023 5:26 pm
by KoRaLLL
Deadlock989 wrote: Sun Jan 01, 2023 1:04 pm The on_selected_entity_changed event fires when an entity is "selected" (in Factorio API jargon this means highlighting the entity with a mouse-over):

https://lua-api.factorio.com/latest/eve ... ty_changed

You can use this event to detect when something is high-lit by the player and search for other entities in range that meet your criteria with surface.find_entities_filtered().
Ah this is perfect! Thank you & happy new year!

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Sun Jan 01, 2023 5:36 pm
by Deadlock989
You're welcome and same to you.

I should also have said that the event also fires when you move the cursor off a selectable entity as well - you can use that to remove things you added on mouseover.

find_entities_filtered() is a moderately expensive call, depending on your use case you might want to consider caching the results (safely in the global table), but for small radii around the selected entity it may not matter much.

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Wed Jan 04, 2023 12:33 pm
by KoRaLLL
I've had success with the above & got the code doing what I need it to do, I'm just on the final hiccup.

I want to replicate this:

Image

But with a custom entity I've created, for storage boxes. So that when I'm holding a storage box, it will highlight the radius of my custom entity (which has a type of 'beacon'), and then highlight the beacon when it's within the beacon's supply area.

Is this do-able? Or is this hard-coded to entities that are power related & beacons/power suppliers?

I know there's the draw_rectangle() function under LuaRendering, would it be a case of calling this function whenever {player} is holding type=container?

And on another note, what is the 'correct' way of calculating the supply radius of an entity? Again, if I take my beacon example, I'm currently doing:

Code: Select all

local entity_radius = entity_prototype.supply_area_distance + entity.get_radius()
But it isn't quite accurate to the radius that appears when you hover over the beacon, most notably in the corners of the supply area aren't being covered. I figured that getting the radius of the entity and then adding that to the supply area distance would work, but it hasn't quite.

Again, any help is always appreciated, thank you!

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Wed Jan 04, 2023 12:43 pm
by Stringweasel
KoRaLLL wrote: Wed Jan 04, 2023 12:33 pm But with a custom entity I've created, for storage boxes. So that when I'm holding a storage box, it will highlight the radius of my custom entity (which has a type of 'beacon'), and then highlight the beacon when it's within the beacon's supply area.
If I understand correcly, you want your beacon to highlight when you hold the box within range? But not placed the box down yet? This is not easily possible because there's no way for you to know the location of the cursor. As far as I know, you'd have to hack something with entities that has this functionality built-in like beacons/electricty - as you mentioned.

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Wed Jan 04, 2023 12:50 pm
by Deadlock989
KoRaLLL wrote: Wed Jan 04, 2023 12:33 pm But with a custom entity I've created, for storage boxes. So that when I'm holding a storage box, it will highlight the radius of my custom entity (which has a type of 'beacon'), and then highlight the beacon when it's within the beacon's supply area.

Is this do-able? Or is this hard-coded to entities that are power related & beacons/power suppliers?
The radius highlight is possible for any entity that supports radius_visualisation_specification, which includes containers, but that's just the pale highlight square.

I would love to be proven wrong but as far as I know the rest isn't possible. The runtime API provides no way of knowing where the cursor is being pointed except for the on_selected_entity event and for some other specific things, e.g. selection areas and throwing capsules. Even if there were, in this case - moving a ghost around with the cursor - it would end up being expensive in Lua to constantly check for other entities in range of the cursor stack.

Your other questions about radius - I don't quite understand what you mean.

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Wed Jan 04, 2023 1:02 pm
by Xorimuth
Deadlock989 wrote: Wed Jan 04, 2023 12:50 pm The runtime API provides no way of knowing where the cursor is being pointed except for the on_selected_entity event and for some other specific things, e.g. selection areas and throwing capsules.
You can now get the cursor position with any CustomInput event. The rest of what you said is true though, and CustomInputs can’t be used in this scenario anyway.

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Wed Jan 04, 2023 1:05 pm
by KoRaLLL
Haha wow I didn't expect so much support and traction so quickly :shock: , thank you so much everyone :D

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Wed Jan 04, 2023 1:37 pm
by KoRaLLL
Deadlock989 wrote: Wed Jan 04, 2023 12:50 pm
KoRaLLL wrote: Wed Jan 04, 2023 12:33 pm But with a custom entity I've created, for storage boxes. So that when I'm holding a storage box, it will highlight the radius of my custom entity (which has a type of 'beacon'), and then highlight the beacon when it's within the beacon's supply area.

Is this do-able? Or is this hard-coded to entities that are power related & beacons/power suppliers?
Your other questions about radius - I don't quite understand what you mean.
So in this instance I'm trying to do the inverse of what I asked before, highlight all containers that are withins the beacons supply range when the cursor is on the beacon.

I am trying to calculate the supply radius of my custom beacon to do something when entities are within that range.

Code: Select all

	-- Start by getting the entities radius & position --
	local surface = entity.surface
	local entity_prototype = entity.prototype
	local entity_radius = entity_prototype.supply_area_distance + entity.get_radius()
	local entity_position = entity.position

	-- Now we need to find all entities within this position by radius --
	local all = game.surfaces[surface.index].find_entities_filtered{position = entity_position, radius = entity_radius, type = 'container'}
This is my current code. If I place a box next to my custom beacon, it behaves as expected and executes the rest of the code. If I place a box on the edge of the supply radius, it doesn't work, because the find_entities_filtered radius calculation is incorrect.

Does this make more sense? Thanks again for the help here

Re: Is there a trigger or variable for the 'highlighting' of certain entities?

Posted: Wed Jan 04, 2023 7:11 pm
by Deadlock989
I haven't looked into that in detail and I think there might be a couple of things going on there but one of them is probably that the radius that find_entities_filtered refers to is a circular radius and the so-called "radius" of beacons is a square. You should probably be using the area filter parameter in find_entities_filtered instead, and use that to specify a square zone that matches the beacon's zone of influence.

I don't have much experience of working with beacons but another issue might be that find_entities_* functions look for the "position" of the entity (usually but not necessarily the centre of the bounding box) being within the radius or area. IIRC that's not how beacons work - any part of the entity's collision box can be within the beacon's zone for it to count. So you might have to search a slightly wider area and then check whether collision boxes overlap with the actual area of interest.