–1 vote

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 onGoalbodyentered(body):
$GoalTimer.start()
print("Level Complete")

func onGoalTimertimeout():
get
tree().change_scene("res://MainMenuYouWin.gd")

Goal:

extends Area2D

func onGoalbodyentered(body):
gettree().changescene("res://MainMenuYouWin.gd")

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

Godot version v3.2.3.stable.official
in Engine by (20 points)
edited by

How are your signals set up?

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.

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.

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

1 Answer

0 votes
Best answer

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.

by (117 points)
selected by

The thing is, the code I wrote is correctly formated, like gettree(). 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

Apologize here, I think I have to post my answer clearer, I mean you used "MainMenuYouWin.gd" inside the changescene() as you posted, you should change this to "MainMenuYouWin.tscn", because the changescene() 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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.