Probably scope issue. Calling get_viewport() in a class function (method?)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I’m still learning and figuring out classes and scope etc.

I have the following:

class MoveState:
	var bat

	#init gets called when an object of this class gets created
	func _init(bat):
		self.bat = bat

	#frame by frame update
	func update(delta):

	#it's good to be able to handle input from within the state
	func input(event):
		#if event.is_action_pressed("ui_up"):
		if event.type == InputEvent.KEY:
			var mousePosition = get_viewport().get_mouse_pos()

On that last line I’m simply trying to find the mouse position. I get the following error though:
Invalid call. Nonexistent function 'get_viewport' in base 'Reference ()'.

I’m SURE it’s to do with scope. How could I get the position from within this function then? I’m really struggling and have spent hours on this. Any help would be greatly appreciated. Brain is shutting down :slight_smile:

:bust_in_silhouette: Reply From: Robster

MARGH! I found it… finally.

var mousePosition = bat.get_viewport().get_mouse_pos()

Note the bat.get_viewport

I create this by first calling onready var state = and it passes ‘self’ (which is the bat) to the class. OK, I can put mind to ease now. Leaving this for anyone in the future.