Finding the mouse position in init?

Is there a way to find the mouse position in init?

I’m hiding/showing/following a certain gui object if the mouse is inside the game window or not and I’d love to set the position of this object even if there is no mouse movement (yet).
Everything works once I start receiving data from on_input.

I haven’t tested this but you should be able to use these defos functions:

defos.get_cursor_pos()
defos.get_cursor_pos_view()
1 Like

I want to find the mouse coordinates during every frame of my game - Questions - Defold game engine forum

This seems to work too, the code in @WhiteBoxDev 's post - this position prints even if the mouse has not moved yet:

function on_input(self, action_id, action)
    if not action_id then  -- action_id == nil, so this is a mouse movement event
        print("Mouse x position = " .. action.x)
        print("Mouse y position = " .. action.y)
    end
end
1 Like

This doesn’t seem to be the case. Launcing from the editor or the exported bundle, on_input doesn’t fire until mouse is moved, which is the expected behaviour.

Also can’t seem to fully grasp the data coming from defos. Which space is that one in?

Interesting - it works for me (on Windows and on Mac) both in the editor and a bundle.

According to the project page:

x, y = defos.get_cursor_pos() -- In screen coordinates
x, y = defos.get_cursor_pos_view() -- In game view coordinates

Yeah I’ve read that but the numbers don’t make much sense. Will need to investigate.

I wonder if the mouse not being updated straight away is a mac only thing? @britzl?

If the mouse has not moved yet it isn’t in the window, even if is sitting on top of the window. So you should treat it as not being in the window until it moves. So the initial position of the mouse is outside the window.

Your application should not be focus stealing from the window manager, it should wait for a signal. Although I like ‘focus strictly under mouse’ it often does not work very well. Responding to mouse click or movement is safest, which is an on_message thing.

Perhaps yes. We’d need to investigate it.