Getting accelerometer input (DEF-1396) (SOLVED)

Hi, I’m new here, I’m studying Defold for mobile game development.

I’m trying to use the accelerometer to move the character.

But with the use of action.acc_x / action.acc_y / action.acc_z inside on_input(), I can only get the information when there is also some other action being executed (“click”, “left”, “right”)

I would like to change the script to only move the character with the movement of the device

function on_input(self, action_id, action)

	label.set_text("info#action", "action: ".. action_id)
	label.set_text("info#info", "info: ".. tostring(action.acc_x) ..", ".. tostring(action.acc_y) ..", "..tostring(action.acc_z))
	if action.acc_x > 0 then
		self.player_direction.x = 1
	elseif action.acc_x < 0 then
		self.player_direction.x = -1
	else
		self.player_direction.x = 0
	end
end

github: game.script

1 Like

Hi @GFerreira

Hmm that seems strange, I verified this on an iOS device and get inputs from accelerometer every frame. What does your input bindings look like? What device are you using?

EDIT
I suspect your issue is caused by action_id being nil on the device when the action contains no touch/mouse input, but only accelerometer data in your case. The script will probably print an error and stop execution when you do

label.set_text("info#action", "action: ".. action_id)

when action_id is nil.

1 Like

It is strange that this acc_ parameter has value with other actions.
I think now you can add next checking:

if not action_id then…

Something like:

function on_input(self, action_id, action)
if not action_id then
	label.set_text("info#action", "action: ".. action_id)
	label.set_text("info#info", "info: ".. tostring(action.acc_x) ..", ".. tostring(action.acc_y) ..", "..tostring(action.acc_z))
	if action.acc_x > 0 then
		self.player_direction.x = 1
	elseif action.acc_x < 0 then
		self.player_direction.x = -1
	else
		self.player_direction.x = 0
	end
end
end

This will still fail I think for when action_id is nil on the line

label.set_text("info#action", "action: ".. action_id)

A simple fix, if you want to set text to the labels regardless, would be something like wrapping action_id in tostring when concatenating:

function on_input(self, action_id, action)
	label.set_text("info#action", "action: ".. tostring(action_id))
	label.set_text("info#info", "info: ".. tostring(action.acc_x) ..", ".. tostring(action.acc_y) ..", "..tostring(action.acc_z))
	if action.acc_x > 0 then
		self.player_direction.x = 1
	elseif action.acc_x < 0 then
		self.player_direction.x = -1
	else
		self.player_direction.x = 0
	end
end

This will simply set the string “nil” on the label if action_id is nil, and continue with the script execution.

3 Likes

As I understood problem is about other (“click”, “tap” and so on) actions have the action.acc_x, action.acc_y, action.acc_z parameters and has no action_id.
As I know defold has only one situation when action_id is nil - it is when data received from the accelerometer.

But yes, original code will be fail when some of parameters will be nill.
As option you can replace all concatenate sybbols .. (double dot) wit , (comma) (it works only with print function)

print("info#action", "action: ", action_id)
print("info#info", "info: ", tostring(action.acc_x) , ", ", tostring(action.acc_y) , ", ", tostring(action.acc_z))

UPD:
It is strange that action_id is nil, maybe have sense to make it something like accelerometer
And one more strange that developer can’t turn off accelerometer, really old task DEF-1396

3 Likes

thanks @AGulev @Johan_Beck-Noren, it worked really fine

I fixed my code now

2 Likes

Fixed in Defold 1.2.116 (input.use_accelerometer in game.project)

2 Likes