0 votes

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):
        pass

    #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 :-)

in Engine by (837 points)

1 Answer

0 votes
Best answer

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 = MoveState.new(self) 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.

by (837 points)
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 Frequently asked questions and 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 [email protected] with your username.