Touch screen press not persisting between scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By abasslinelow

I am messing around with making a mobile game, which means touch controls. I have stages set up as individual scenes, and each scene has a Player node with touch controls attached. My problem is that, when I transition scenes to change levels, the game stops registering the screen touch event. Here is the relevant code:

if Input.is_action_pressed("ui_left") || $"TControls/Left".is_pressed():
	velocity.x = -SPEED
elif Input.is_action_pressed("ui_right") || $"TControls/Right".is_pressed():
	velocity.x = SPEED
velocity.y += GRAVITY
velocity = move_and_slide(velocity, FLOOR)

That’s in the Player object. I’ve obviously removed some unrelated code, but this is essentially how I’m dealing with the touch controls. What I think is happening is that, when I load a new scene, it creates a new Player object, which in turn creates new touch controls which don’t get registered as pressed until the player re-touches the button.

It’s not a major issue, but, it would help the flow of the gameplay a lot if the movement persisted. Does anyone have any ideas on how to get around this issue? Thanks a bunch in advance.

:bust_in_silhouette: Reply From: wombatstampede

This is probably not a very convient answer but I’d use a single parent scene which instances the level scenes as child nodes. The input handling should be done by the parent scene and handed over the currently active level. Naturally you can do also other tasks in the parent scene (score counting etc.)

Amusingly enough, I came to this conclusion as well, but my attempts to do so had me pulling my hair out. Given how this project is designed and implemented, it might be easier to start from scratch. I have a plethora of questions concerning the best way to organize this task - too many to feel comfortable asking, in all honesty. I’ll continue to mess around and see what I can do on my own. Thanks for your answer and your time!

abasslinelow | 2019-04-08 22:11

Update: I got it! I was massively over-complicating things. The level handling I had set up beforehand was perfectly acceptable. The problem was with the touch controls being attached to the player - all I had to do was create a scene with the touch controls in it, make it a singleton, and set the on_pressed and on_released signals to use action_press and action_release to simulate the proper inputs on my input map:

func _on_Left_pressed():
    Input.action_press("ui_left")
func _on_Left_released():
    Input.action_release("ui_left")

etc etc. It really was that simple. In fact, it’s more simple than my original setup! I’ve been banging my head against the wall for 3 days with this. Thank you so much for your help.

abasslinelow | 2019-04-08 23:50