cursor position

Place to get help with not working mods / modding interface.
lux
Inserter
Inserter
Posts: 25
Joined: Fri Nov 07, 2014 7:40 pm
Contact:

cursor position

Post by lux »

Hello,

I am working on a mod and that needs to access two things:
a) the position of the player's cursor
b) the placed entities under the player's cursor

I have struggled to get this working.

In regards to a), the player's cursor, I did look at http://lua-api.factorio.com/latest/LuaP ... r_position but this appears to be in screen coordinates, whereas I need the coordinates relative to the world and I don't know how I would translate those.

My current work around is to require the player to place an invisible entity, which triggers a) events.on_built_entity and that immediately destroys the placed item, and b) events.on_put_item which provides an event with a position. The problem is you cannot place items on top of other entities, or in water, so I am kind of getting stuck with that. Is there a cleaner way to do this? I have not fully explored surfaces, which might allow placing the invisible object anywhere, or capsules which might be able to deal with water? Any suggestions?

In regards to b), getting the entity under the player's cursor, I see on_player_selected_area, and player.selected, but I really need some sort of "on_cursor_hover" or some other event. Is there something obvious I am missing?

Any help is appreciated, or examples of other mods.

Thanks,
Lux
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: cursor position

Post by aubergine18 »

Check game.players[playerId].selected.position, for example:

https://github.com/aubergine10/searchin ... ontrol.lua

A gif of what that mod does:

https://github.com/aubergine10/searchin ... /README.md
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: cursor position

Post by Nexela »

You can't get the position of the cursor, cursor_position was used to make the trailers to set the cursor position, however you can get the "selected" entity under the cursor

hover over an entity and type this in the console
/c game.player.print(game.player.selected.name)
game.player.selected is the complete entity.

Also there is no event that triggers on hovering over an item.

If you exand the details of your workaround with what you are trying to accomplish we might be able to come up with some ideas to help you.
lux
Inserter
Inserter
Posts: 25
Joined: Fri Nov 07, 2014 7:40 pm
Contact:

Re: cursor position

Post by lux »

Excellent, thank you aubergine18 and Nexela both, player.selected seems to solve b.
Nexela wrote:If you exand the details of your workaround with what you are trying to accomplish we might be able to come up with some ideas to help you.
I was trying to get the cursor position to teleport a player to that position. Placing an invisible building and getting the position seemed to solve 90% of the cases, but not the case where the player cannot place an entity, such as on top of another entity, on top of water. In those cases I need to find the nearest empty space, which I was going to base of the cursor position, but maybe I can just use the selected entities position instead. I will try that, thanks again for the help!
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: cursor position

Post by Nexela »

lux wrote:I was trying to get the cursor position to teleport a player to that position. Placing an invisible building and getting the position seemed to solve 90% of the cases
This can be done fairly easy

Create an item prototype with no place_result (entity). just the item and recipe.

Code: Select all

script.on_event(defines.events.on_put_item, function(event)
  player=game.players[event.player_index]
  if player.cursor_stack.valid_for_read and player.cursor_stack.name="my-teleport-item" then
    if player.surface.can_place_entity{name="player", position=event.position, force=player.force} then
      player.teleport(event.position)
    end
  end
end)
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: cursor position

Post by aubergine18 »

Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
lux
Inserter
Inserter
Posts: 25
Joined: Fri Nov 07, 2014 7:40 pm
Contact:

Re: cursor position

Post by lux »

Nexela wrote:Create an item prototype with no place_result (entity). just the item and recipe.[/code]
Ah ha, can_place_entity was what I was missing, thanks!
aubergine18 wrote:You can also find non colliding positions within a given area if that's any use?
find_non_colliding_position looks incredibly useful! I will use this as well. The first mod is actually the one I am working on :)

Thanks again for all the help!
Post Reply

Return to “Modding help”