# Getting accelerometer input (DEF-1396) (SOLVED)

**URL:** <https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406>\
**Category:** Questions\
**Created:** [August 3, 2017, 2:53am UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406 "2017-08-03T02:53:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GFerreira](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gferreira/32/3078_2.png) [@GFerreira](https://forum.defold.com/u/GFerreira)\
**Post date:** [August 3, 2017, 2:53am UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406/1 "2017-08-03T02:53:12Z")

</div>

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](https://github.com/guilhermebferreira/defold_swip_witch/blob/accelerometer/swip_witch/swip_witch/game.script)

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [August 3, 2017, 9:27am UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406/2 "2017-08-03T09:27:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [August 3, 2017, 12:50pm UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406/3 "2017-08-03T12:50:17Z")

</div>

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:

```lua
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

```

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [August 3, 2017, 1:07pm UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406/4 "2017-08-03T13:07:12Z")

</div>

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

```lua
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:

```lua
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.

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [August 3, 2017, 1:17pm UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406/5 "2017-08-03T13:17:04Z")

</div>

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)

```lua
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](https://forum.defold.com/t/opt-out-from-accelerometer-input-def-1396/2515)

---

<div class="post-metadata">

**Author:** ![GFerreira](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gferreira/32/3078_2.png) [@GFerreira](https://forum.defold.com/u/GFerreira)\
**Post date:** [August 3, 2017, 1:55pm UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406/6 "2017-08-03T13:55:17Z")

</div>

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

I fixed my code now

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 31, 2017, 5:09pm UTC](https://forum.defold.com/t/getting-accelerometer-input-def-1396-solved/10406/7 "2017-10-31T17:09:05Z")

</div>

> [@AGulev](#):
>
> And one more strange that developer can’t turn off accelerometer, really old task DEF-1396

Fixed in Defold 1.2.116 (input.use\_accelerometer in game.project)
