Sometimes annoying WARNINGS.

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

We know all about the warnings .

For example:
The function ‘change scene ()’ returns a value,but this value is never used.

Let’s see now the following part of code:

func _on_Readme_pressed():
	get_tree().change_scene("res://Scenes/Readme.tscn")
	pass 

Ofcourse If i use a variable then the Warning is eliminated:

var k

  func _on_Readme_pressed():
    	k=get_tree().change_scene("res://Scenes/Readme.tscn")
    	pass 

So, what we have to do here if the warnings does not cause to us practical problems?To ignore them , to use a variable avoiding maybe future problems , or turn Off the Enable choice in General->Gdscript->Warning->Enable (ON->OFF)?

Thank you.

:bust_in_silhouette: Reply From: Thomas Karcher

You only know which practical problems might occur after you see them happening. So the safest thing to do would be:

if get_tree().change_scene("res://Scenes/Readme.tscn") != OK:
    print ("An unexpected error occured when trying to switch to the Readme scene")

No more warnings, and a nice error message in case your scene is renamed or deleted. A win-win! :slight_smile:

Hmmm…nice! Thank you!

Nick888 | 2019-07-04 13:55

:bust_in_silhouette: Reply From: RaulSanCtes

I am a novice (very) and I ran into this problem, in version 3.2.1 this solution does not work “cleanly” (in the debugger a yellow warning appears saying that this variable was declared but is not being used), for a detail ( which I don’t understand very well) and to solve it the same debugger informs you that you must place an underscore in front of the variable, so if your variable was suppose playGame now it should be _playGame

func PlayButton():
var _playGame
_playGame = get_tree().change_scene("res://Escenas/Player.tscn")

You’re mixing up to different things here:

The original question is talking about the “Return Value Discarded”-warning, which will occur whenever you’re using a function that returns something without considering what it returns. Because that might become an issue later!

You’re talking about the “Unused Variable”-warning, which will occur whenever you declare a variable in your script but never actually put it to use. Then it’s either dead weight and can be removed or your work isn’t finished yet!

Prefixing a variable with an underscore is GDScript’s way of telling the computer that this is “intended” and comes in very handy if e.g. you’re connecting to a signal, let’s say body_entered in Area2D. This signal comes with an argument: the body which just entered the Area2D. You might care for that information and use it. But if you don’t, you can’t simply get rid of it: any callback you’re connecting to this signal needs to take one argument or it will throw an error! However, if the callback takes an argument and never actually uses it, that will result in an “Unused Variable”-warning. That is the only scenario I can imagine where it’s adequate to prefix with an underscore: when you’re forced to use an API (like Godot’s functions) you cannot simply adapt to your usecase. If you see those warnings in your own functions, you should ask yourself why you’re passing an argument that you have no need for in the first place!

Now why isn’t that a proper fix for the “Return Value Discarded” then too? After all, the warning disappears, right? Right, because you’re not longer ignoring the return value now: you’re saving it to a variable called _playGame. Is the underscore important for that? No! You could also name it playGame or (like in the question) k. Is it a proper way of addressing the warning? Also no! Instead of looking at the return value and what it actually means (in this case it’s an error code) you’re just stashing it away. It’s like cleaning your room by sweeping all dirt everything under the carpet: While it might make your mother/the warning happy, it does not actually clean the room!

The other answer works just fine (in Godot 3.2.1 as well) and is the right way to do it!

njamster | 2020-04-29 00:09

So what would be the solution? I can’t understand it, the code I put from .Example, it’s from tutorials, and it gives me that warning, I can’t understand it.If the variable is declared and then used to call the scene change, I don’t understand it.

RaulSanCtes | 2020-04-29 15:12

The solution for what? Which code from which tutorial gives you which warning?

njamster | 2020-04-29 15:31

Oh, it is an apparently simple tutorial, it has 2 scenes, “MainMenu” and “Player”, in MainMenu it has this code, the scene only has a “PLAY” button that when clicking opens the “Player” scene, another button that prints in console a text and the third button that closes the game.


     extends TextureRect
       
    func PlayButton():
    	var _playGame
    	_playGame = get_tree().change_scene("res://Escenas/Player.tscn")
    
    func CreditsButton():
    	print("Created by")
    
    func QuitButton():
    	get_tree().quit()  
   
Now that I read your answer, I see that the tutorial has a flaw, although everything works, it is a bad practice, right? I would like to do things right from the start, not just "make it work".

I'll find out more about it, I'm not sure I understand your answer, but still thanks for your time.
[wrap=footnote]RaulSanCtes | 2020-04-29 16:46[/wrap]

I have read a bit and I think this may be a solution, maybe not entirely clean, but a little better, right?

extends TextureRect
    
    func PlayButton():
    	var playGame
    	playGame = get_tree().change_scene("res://Escenas/Player.tscn")
    	print(playGame)
    
    func CreditsButton():
    	print("Created by")
    
    func QuitButton():
    	get_tree().quit()
[wrap=footnote]RaulSanCtes | 2020-04-29 17:01[/wrap]

While all solutions you presented will make the warning disappear, only the last one puts the returned value to use. The purpose of the warning is not to force you into creating a variable that you don’t need, it’s to consider what a function is returning and why it is returning something in the first place! In the case of change_scene it returns an Error (you can look that up in the documentation here) . That’s simply a number with a defined meaning: a 0 means no errors occurred, a 6 means your game ran out of memory, a 17 signals missing dependencies, etc. So whenever a function returns an Error, you want to check if it’s 0 (in which case you have nothing to worry about, everything went fine) or not (in which case you might considers stopping your game or at least showing the user that something went wrong):

var error_code = get_tree().change_scene("res://Escenas/Player.tscn")
if error_code != 0:
    print("ERROR: ", error_code)

njamster | 2020-04-29 17:22

Oh! Now I think I understand, in this whole thread I have understood some very clarifying things, paying attention to each warning and acting accordingly, the intended variables and how to actually use the change_scene.

When Godot tells me that a variable was not used, he is really saying that I did nothing with it, it is not enough just to load a value, you have to do something with them! I thought that by loading a value it was already being “used”.

With the example you have given me, I really understand that the most useful way to use the variable is that one that will show me the error if it is not 0! I was using it just to get the warning off me, something totally useless.

I appreciate your patience with me, it was very nice of you.

RaulSanCtes | 2020-04-29 17:54

var n = PackedScene

isaac12345 | 2022-11-12 23:36

:bust_in_silhouette: Reply From: isaac12345

gracias muchísimo a la variable yo
la puse var n = PackedScene si
sirve