Need help with finding mous_position when get_viewport() is null?

Godot Version

4.2 Stable

Question

I made a 2d-topdown shooter. Everything has been working until I added a main menu. When the main scene plays, var mouse_x = get_global_mouse_position() is supposed to find the mouse position, then the next script is set to determine if the mouse is to the left of the screen, or to the right:

func _process(delta):

	var mouse = get_global_mouse_position()

	if mouse.x > position.x:
		animated_sprite.flip_h = false
		animation_player_direction = 1
	elif mouse.x < position.x:
		animated_sprite.flip_h = true
		animation_player_direction = -1

As I said, it all worked fine until I added a main menu, but now I get these warnings :face_with_raised_eyebrow: :

E 0:00:05:0871 Player.gd:167 @ _process(): Parameter “get_viewport()” is null.
<C++ Source> scene/main/canvas_item.cpp:1047 @ get_global_mouse_position()
Player.gd:167 @ _process()

E 0:00:05:0872 Player.gd:178 @ _process(): Parameter “body->get_space()” is null.
<C++ Source> servers/physics_2d/godot_physics_server_2d.cpp:997 @ body_test_motion()
Player.gd:178 @ _process()

I’m new to godot, and I don’t know what these mean, neither how to fix them. Help would be appreciated.

How are you changing from your main menu to your playing scene?

I use a simple button:

func _on_play_pressed():
	get_tree().change_scene_to_file("res://scenes/game.tscn")

i ran a quick mockup with the code that you gave me and your scene changing logic and i didnt run into any problems. i did notice though, that you have a variable called mouse_x, but your checking mouse in your if statement. could this be a variable that you declared earlier in the script?

So in the error codes your receiving, it the editor is telling you that the problem is in code lines, 167 and 178, id look there for now

Oh, sorry when I pasted the code here that happened. It’s the same variable.

This is line 167:

var mouse = get_global_mouse_position()

I think I get the second error because the variable is not valid. (I’m just thinking here)

Don’t know if this would help, but I made a custom mouse cursor scene the same day with this script:

extends Node2D

func _process(_delta):
	position = get_global_mouse_position()

func _ready():
	DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CONFINED_HIDDEN)

But I don’t think this is where the issue is.

an easier way of changing mouse icon would be through project settings:

after i set the mouse to hidden, i didnt get any errors but the logic for rotating the sprite wouldn’t work, i believe, mainly since when you set the mouse to hidden, it locks mouse position to the center of the screen.

I have done it through the settings, but for some reason the mouse keeps popping back to its original texture, I don’t know if this is for me only (I’m a mac user), but I hid the cursor instead for no further issues.

that’s interesting, im a windows user so unfortunately im not familiar with mac bugs, but, like you said earlier, it doesn’t seem to be the issue but it was worth checking.

i have two ideas that could possibly work,

get_viewport().get_mouse_position()

or place your mouse grabbing and sprite flipping logic in the _input(event) function

func _input(event):
	if event is InputEventMouseMotion:
		var mouse_pos = event.global_position
                #add character flip logic here from your process

i dont think that the first one will work as much, given the error, but id still try it. i just tested the second one but it only works if you click and drag, which dosent make sense.

another thing i would recommend is using the remote-scene during runtime so that you can look at what is being added or removed from the scenetree.

Hey! I think I know whats happening. When I play the game, nothing is wrong until I quit. Therefore, I think the error is caused by closing the game. The second error is also caused by this, move_and_slide(). At this point they are just annoying errors that occur every time I close the game.

Interesting, I’m not sure how I’d go about getting rid of those errors at quit, but I’m glad you figured it out!

Thanks for your time :slightly_smiling_face:

1 Like

Just to let you know, I fixed the issue by putting the script to flip the animation_player2d before the script that quits the game.

	var mouse = get_global_mouse_position()
	
	if mouse.x > position.x:
		animated_sprite.flip_h = false
		animation_player_direction = 1
	elif mouse.x < position.x:
		animated_sprite.flip_h = true
		animation_player_direction = -1

	move_and_slide()

	if Input.is_action_just_pressed("ui_cancel"):
		get_tree().change_scene_to_file("res://scenes/menu.tscn")
1 Like