Where to get mouse co-ordinates

Is there a way to get the X and Y of the mouse all the time?

This works

function on_input(self, action_id, action)
	if action_id == hash("click") and action.pressed then
	p=(vmath.vector3(action.x, action.y, 0))
	factory.create("#clickerfactory", p)
	end
end

But this doesn’t

function update(self, dt)
go.set_position(vmath.vector3(action.x, action.y, 0))
end

(basically i need a tiny little collision object constantly following the mouse around in order to do some mouse over stuff for the menus and stuff)

Maybe try something like this. Remember that when you create a variable, you usually want to use local. For example local p = etc. When you just do p=, you could have problems in the global namespace. In this example below, there’s no local variable needed, because I’m storing mpos_vec as part of self instead.


function init(self)
  msg.post(".", "acquire_input_focus")
  self.mpos_vec = vmath.vector3(0, 0, 0)
end

function on_input(self, action_id, action)
  self.mpos_vec.x = action.x
  self.mpos_vec.y = action.y
  
  if action_id == hash("click") and action.pressed then
    factory.create("#clickerfactory", self.mpos_vec)
  end
end

function update(self, dt)
  go.set_position(self.mpos_vec)
end

Alternatively, you could do go.set_position(vmath.vector3(action.x, action.y, 0)) within the on_input function, and not have an update function at all. Then the script would only set the position when the mouse moves, rather than needlessly setting it every update.

I think you didn’t understand my question: I WANT to set the posición every update in order to have a little collision object that collides with stuff.

In any case, this part still returns an error (same error as before)

ERROR:SCRIPT: main/clickerstuff/clicker.script:10: attempt to index global ‘action’ (a nil value)

My best guess would be it’s trying to parse action during a null input event, but according to the docs mouse movement uses action_id nil, so maybe you can wrap the accessing of action.x and .y within this:

function on_input(self, action_id, action)
  if action_id == hash("nil") then
    self.mpos_vec.x = action.x
    self.mpos_vec.y = action.y
  elseif action_id == hash("click") and action.pressed then
    factory.create("#clickerfactory", self.mpos_vec)
  end
end

Also make sure you have

function init(self)
  msg.post(".", "acquire_input_focus")
end

at the top of your script, or it won’t receive action data.

That would be if action_id == nil or even better if not action_id then

Like this (line #24): https://github.com/britzl/publicexamples/blob/examples/click_and_drag/example/cursor.script#L24

That script will move a game object every frame following the mouse/finger.

Wouldn’t the mouse have to be down for that to work? As it is inside the on input función?

No, on_input() will give you any kind of input you’ve bound in your input bindings, not only button press, game pads etc but also mouse/touch movement (and it doesn’t require that a button is pressed).

Create a key binding for MOUSE_BUTTON_1 to activate mouse and touch input, then acquire_input_focus and finally do a pprint(action) in your on_input function and see for yourself!