"attempt to concatenate global 'mouse_x' (a nil value)"?

Hi,

Trying to print mouse screen coordinates now, but can’t seem to get it working:

label.set_text("#MouseX", "X:" .. mouse_x) -- attempt to concatenate global 'mouse_x' (a nil value)

Would like to have one line that looks like this: “X:180/Y:320”

Any help would be appreciated, thanks!

J.

It says that your variable mouse_x is not yet set, or set to nil. Make sure you set your mouse_x to action.x or what you want it as.

After that it should be what you wrote, just concatenate the value for Y too.
label.set_text("#MouseX", "X:" .. action.x .. "/Y:" .. action.y).

If for some reason your mouse_x is sometimes nil and you want to know that you can cast it to a string with tostring(mouse_x).

typo, should be this:
label.set_text("#MouseX", “X:” … screen_x) – still does not work

Hi,

Ok, almost there…
Still have nil for both mouse x and mouse y?

function on_input(self, action_id, action)
	-- Add input-handling code here
	-- Remove this function if not needed

	if action_id == hash("GotoNextScreen") then
		if CurrentScreen == 1 then Screen1FadeStatus = 1
		end
	end

	label.set_text( "#MouseXY", "X:" .. tostring(screen_x) .. "/Y:" .. tostring(screen_y) ) -- Always nil for both values?
end

What is wrong?

J.

The problem is that you are referencing a variable called “screen_x” (and “screen_y”) that you never define. Undefined variable are nil in Lua.

Hi,

I got “screen_x” & “screen_y” from below Defold documentation:

Little lost here…

J.

If you write the following:

function on_input(self, action_id, action)
   -- 
end

That means that you define a function called on_input() that takes three arguments: self, action_id and action. There are no other magic global variables that are set.

The engine calls your function and provides values for the arguments. The page you linked says this:

Mouse and touch inputs set fields in the action table…

This means that a table is passed to the function. The table contains the values you need. Try this:

function on_input(self, action_id, action)
   pprint(action)  -- pretty print table "action".
end

If you wonder what a table is, we have a brief Lua manual (Lua programming in Defold), but you should probably get hold of a copy of “Programming in Lua” and work with that.

3 Likes

Thanks, we got it working:

label.set_text( "#MouseXY", "X:" .. tostring(action.screen_x) .. "/Y:" .. tostring(action.screen_y) )

J.

Defold does not seem to scale mouse coordinates?

This code does it:
(original screen size is 360x640)

	local mX = action.screen_x
	local mY = action.screen_y
	mX = mX * ( (360/WindowsWidthTrue) )
	mY = mY * ( (640/WindowsHeightTrue) )
	label.set_text( "#MouseXY", "X:" .. tostring(mX) .. "/Y:" .. tostring(mY) )

Moving forward…

J.

Here you go: API reference (go)

Or, from the page you linked above:

Mouse and touch inputs set fields in the action table for location (x and y) as well as movement (dx and dy).

1 Like