I can't make my goal at the end of a level change scenes.

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

So what I’m trying to do is have a atom looking object at the end of an level to act as a goal, and when the player touches it, it changes scenes.

The problem is that I can’t seem to get it to work!

Here is the code I used:

Player:

func _on_Goal_body_entered(body):
$GoalTimer.start()
print(“Level Complete”)

func _on_GoalTimer_timeout():
get_tree().change_scene(“res://MainMenuYouWin.gd”)

Goal:

extends Area2D

func _on_Goal_body_entered(body):
get_tree().change_scene(“res://MainMenuYouWin.gd”)

And yes I already made the player be able to collide with any objects in the Goal collision mask

How are your signals set up?

kylemcclinton | 2021-03-15 05:40

You set the player to be able to collide with the goal, but did you do it the other way around? The on_Goal_body_entered(body) function would need the goal to be able to detect the player.

ClumsyDog | 2021-03-15 05:47

Yes, I made the player be able to collide with anything that is in the same layer as the goal (the goal layer)

I anything in the player layer’s collision mask be able to touch the layer the goal is on

mrspaceblock | 2021-03-15 15:21

For the goal object, I made that if you enter the body, it should start the timer, then after one second it should take you to the Victory screen

For the player I made so that if there is an object called Goal in the same scene, and they both collide, to start the timer.

mrspaceblock | 2021-03-15 15:20

:bust_in_silhouette: Reply From: Ngong8

It is not this:

func onGoalTimertimeout():
     gettree().change_scene("res://MainMenuYouWin.gd") # gd is the gdscript, not scene format...

It is like this:

func onGoalTimertimeout():
     get_tree().change_scene("res://MainMenuYouWin.tscn") # tscn is the valid scene format

And you can just let your either player character or goal area to check if your player is inside the goal or not, then change scene.

The thing is, the code I wrote is correctly formated, like get_tree(). is types like this in my code, I don’t know why when I copied the code to put in my question, I don’t know why I can without the “_”. but even with the function typed correctly, it still doesn’t work

I’ve already figured something out though, I used a coin-like object i had in my game, I made a copy of its scene, changed the code so that it sends you to another scene when the animation finishes, and then it worked, but I still have no idea why modifying my coin object worked, but using a body_entered function didn’t

mrspaceblock | 2021-03-16 17:19

Apologize here, I think I have to post my answer clearer, I mean you used “MainMenuYouWin.gd” inside the change_scene() as you posted, you should change this to “MainMenuYouWin.tscn”, because the change_scene() method will only execute the things you refer which are in .tscn format.

Edit: I just noticed using underscore symbol on any word will become italic font instead of just properly showing the symbol if not using code sample(Ctrl+K on any line of code), so that’s why the some words we type will not properly displayed.

Ngong8 | 2021-03-16 17:32

Oh I didn’t notice this, thank you very much!

mrspaceblock | 2021-03-16 19:32