Invalid set index 'screensize' (on base 'Area2D (') with value type of 'Vector2'

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

I’m following the guide in the Godot Engine Game Development Projects book that I ordered, and to be honest. I’ve hit a snag already.

I get the above error when trying to run the game. I’m using Godot 3.1.0 Alpha 32 Bit. (This is where the screensize variable SHOULD be set)

extends Area2D

signal pickup
signal hurt

export (int) var speed
var velocity = Vector2()
var screensize = Vector2(480, 720)

func get_input():
	velocity = Vector2()
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1
	if velocity.length() > 0:
		velocity = velocity.normalized() * speed

func _process(delta):
	position += velocity * delta
	position.x = clamp(position.x, 0, screensize.x)
	position.y = clamp(position.y, 0, screensize.y)
	if velocity.length() > 0:
		$AnimatedSprite.animation = "run"
		$AnimatedSprite.flip_h = velocity.x < 0
		$AnimatedSprite.animation = "idle"

func start(pos):
	position = pos
	$AnimatedSprite.animation = "idle"
func die():
	$AnimatedSprite.animation = "hurt"
func _on_Player_area_entered(area):
	if area.is_in_group("coins"):
	if area.is_in_group("obstacles"):
		die() (This is the script giving me the error. Line 33. I’ve added arrows to make it easier to pinpoint)

extends Node

export (PackedScene) var Coin
export (int) var playtime

var level
var score
var time_left
var screensize
var playing = false

func _ready():
	screensize = get_viewport().get_visible_rect().size
	$Player.screensize = screensize

func new_game():
	playing = true
	level = 1
	score = 0
	time_left = playtime

func spawn_coins():
	for i in range(4 + level):
		var c = Coin.instance()
		c.screensize = screensize <<<<<<<<<< ERROR
		c.position = Vector2(rand_range(0, screensize.x), rand_range(0, screensize.y))

func _process(delta):
	if playing and $CoinContainer.get_child_count() == 0:
		level += 1
		time_left += 5

Did I miss something? If so. Please let me know. Thanks!

:bust_in_silhouette: Reply From: kidscancode


You’ve fallen victim to one of the book’s errors, which are compiled here:

The problem is that the following line is missing from

var screensize = Vector2()

The coin needs to have its screensize variable set as well so that it can choose a random position within the bounds of the screen.

Thank you. This fixed the issue.

9BitStrider | 2018-10-07 12:15