Page 1 of 1

cursor position

Posted: Thu Sep 08, 2016 4:10 am
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

Re: cursor position

Posted: Thu Sep 08, 2016 5:42 am
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

Re: cursor position

Posted: Thu Sep 08, 2016 8:12 am
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.

Re: cursor position

Posted: Fri Sep 09, 2016 4:29 am
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!

Re: cursor position

Posted: Fri Sep 09, 2016 7:18 am
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)

Re: cursor position

Posted: Fri Sep 09, 2016 11:18 am
by aubergine18

Re: cursor position

Posted: Sat Sep 10, 2016 5:29 am
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!