Invalid set index 'screensize' (on base 'Area2D (Coin.gd)') 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.

Player.gd (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):
	get_input()
	
	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
	else:
		$AnimatedSprite.animation = "idle"

func start(pos):
	set_process(true)
	position = pos
	$AnimatedSprite.animation = "idle"
	
func die():
	$AnimatedSprite.animation = "hurt"
	set_process(false)
	
func _on_Player_area_entered(area):
	if area.is_in_group("coins"):
		area.pickup()
		emit_signal("pickup")
	if area.is_in_group("obstacles"):
		emit_signal("hurt")
		die()

Main.gd (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():
	randomize()
	screensize = get_viewport().get_visible_rect().size
	$Player.screensize = screensize
	$Player.hide()
	new_game()

func new_game():
	playing = true
	level = 1
	score = 0
	time_left = playtime
	$Player.start($PlayerStart.position)
	$Player.show()
	$GameTimer.start()
	spawn_coins()

func spawn_coins():
	for i in range(4 + level):
		var c = Coin.instance()
		$CoinContainer.add_child(c)
		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
		spawn_coins()

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

:bust_in_silhouette: Reply From: kidscancode

Hi!

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 Coin.gd:

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