I'm trying to get the viewport size using the standard boilerplate code:

var screen size
...
screen_size = get_viewport_rect().size

but am getting the error 'Parser Error: Function "get_viewport_rect()" not found in base self.'

I have tried it in multiple scripts and get the same error. The frustrating thing is I already have the exact same code in my camera script and it works fine. But now when I try it elsewhere, I get this message. I tried googling and haven't found anyone complaining about this exact error.

  • xyz replied to this.

    Discant get_viewport_rect() is a method of CanvasItem class. You can only call it on an object of that class or a class that inherits it. To what type of node is this script attached?

      xyz It was attached to a basic Node type node (my root node). What's a better node type for that?

      • xyz replied to this.

        Discant As I said, this function will only be available in a script that is attached to a node that inherits CanvasItem. If this is a 2d project you can try with Node2D instead of Node.

          xyz Thanks for answering. So I tried changing it to Node2D and still got the same error.

          • xyz replied to this.

            Discant you also need to change the extends statement at the start of your script to match the node type.