Hello everybody,
I'm posting this question about a problem that I encounter currently in my project.
About the problem itself :
- I have set a "character" as an Area (3D), and that is monitoring.
- I have set some objects as Area (3D) too, that are monitorable.
- I've made a connect("area_enter",self,"someFunc") on the character node.
And, at the first time, it works PERFECTLY FINE ! someFunc() is called everytime when my character enter an Object.
The problem occurs when I leave the scene and load another, and then come back to the scene. After some times, many objects aren't detected anymore. It seems that my "area-enter" signal is not emitted anymore.
So, have you any idea about why it doesn't work ? Why it bugs when i'm leaving the scene and then comes back ?
In order to "show" my problem in details:
This is my script for the character:
extends Area
var SPEED_FORWARD = Global.SPEED
const SPEED_SIDE = 450
const SPRITE_SIZE = 50
const SIZE_MAP = 720
var side
var inputNode
var areaNode
func _ready():
inputNode = get_node("../ControlCharacterUI/CharacterUI")
set_monitorable(true)
set_enable_monitoring(true)
self.connect("area_enter", self, "on_area_enter")
set_fixed_process(true)
func _fixed_process(delta):
#We get the move wanted by the user by calling the appropriate node.
side = inputNode._get_move()
#If the right or left limit of the "moving zone" is reached, we forbid the move.
if(self.get_transform().origin.x - SPRITE_SIZE < -SIZE_MAP/2):
if (side == -1):
side = 0
if(self.get_transform().origin.x + SPRITE_SIZE > SIZE_MAP/2):
if(side == 1):
side = 0
# We move the character forward and left/right according to the side previously get
self.translate(Vector3(side * SPEED_SIDE * delta, 0, -delta * SPEED_FORWARD))
func on_area_enter( area ):
area._action()
And there my script for the objects:
var isAlreadyCollided = false
func _action():
if(!isAlreadyCollided):
isAlreadyCollided = true
-----DOING SOME IRREVELANT HERE-----
self.hide()
func _ready():
set_monitorable(true)
set_enable_monitoring(true)
Here are screens :
http://img15.hostingpics.net/pics/800784SuperSolutecShape.png

http://img15.hostingpics.net/pics/168115SuperSolutecArea.png

(Objects have the same parameters, except for being monitorable instead of monitoring, and the shape, that is a BoxShape instead of a CapsuleShape)
For the context :
The elements/objects are dispatched in a "Level1.tscn":
http://img15.hostingpics.net/pics/164673Level.png

And next, in a main scene, there is our Character, and a script add the "Level" as a child of an "AllElements" node.
http://img15.hostingpics.net/pics/144747Mainarbo.png

And here the portion of script used in AllElements:
var scene = load("res://Levels/Level1.tscn")
var instance = scene.instance()
self.add_child(instance)
Final scene : http://img15.hostingpics.net/pics/348996Final.png

SO, that's all for how it is done.
The problem occurs when we switch between the main scene and another scene, and then come back to the main scene. It's done with :
Global.goto_scene("res://Menus/MainMenu/MainMenu.tscn")
in the "main.tscn" and
Global.goto_scene("res://main.tscn")
in the "MainMenu.tscn".
Where Global.goto_scene(path) is :
var current_scene = null
func goto_scene(path):
call_deferred("_deferred_goto_scene",path)
func _deferred_goto_scene(path):
current_scene.free()
var s = ResourceLoader.load(path)
current_scene = s.instance()
get_tree().get_root().add_child(current_scene)
get_tree().set_current_scene( current_scene )
After changing of scene ( main.tscn -> MainMenu.tscn -> main.tscn ... ) like 2 to 4 times, the area-enter signal doesn't work anymore.
Have you any idea ? Thanks you in advance !
EDIT: The same problem occurs if, for example, I load the Level1.tscn on ready() 's function of AllElements, then instanciate it, then queue_free() it, then re-instanciate it. After a while, some elements aren't detected by area_enter anymore.