How to properly instance?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sprowk
:warning: Old Version Published before Godot 3 was released.

I created scene with complex button. When you touch it. Joystick appears and button disapears. Then you can swipe to any direction. Everything in 1 motion and last position saves in Global variable. joysticks hides and button appears again.

extends Control

var pressed = false

func analog_force_change(inForce, inAnalog):
    	if inForce.x == 0 and inForce.y == 0:
		   pressed = false

func _on_Button_mouse_enter():
   if pressed == true:

func _on_Button_focus_enter():
	pressed = true

Everything works as I typed above. The problem is when I try to instance these nodes to another scene…Its far away from working. No value in Global and joystick stays visible. Even it feels very different and ALL I did was instanced fully working button scene…

Are the corresponding signals connected? Unlike functions like “_ready()” the callback functions for button clicks need to be connected.

By the way I recommend that you don’t use globals. Godot makes it so easy to use object orientation that there’s no need to go out of your way to break encapsulation. Why not have your game read the input values from the joystick object rather than from a global variable? That way you will run into less trouble in the long run e.g. when you want to add another player or have different joysticks affect different elements in the game.

Warlaan | 2016-12-31 09:45

1, I will try to not use globals.
2, The button scene is working correctly even when I exported it to android but when I put it to other scene then It stops working.

found the problem
When instanced I had to change listener node from root/action to root/ui/action

Sprowk | 2016-12-31 09:56