Invalid set Index error

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

I’m currently following along with the “Coin Dash” project as provided in the “Godot Engine Game Development Projects: Build five cross-platform 2D and 3D games with Godot 3.0” book. and I keep running into an Invalid set Index error. The code in question looks as follows:

    extends Node2D

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
		c.position = Vector2(rand_range(0, screensize.x),rand_range(0, screensize.y))

The error occurs at line 32(c.screensize = screensize), with the following error message: "Invalid set index ‘screensize’ (on base: ‘Area2D (’) with value of type ‘Vector2’. I’m still rather new at this but I’m unsure what exactly is wrong, is the fact that screensize is a vector causing the issue? Any help would be greatly appreciated.

Did you define var screensize in the coin’s script?

kidscancode | 2020-03-01 09:04

:bust_in_silhouette: Reply From: jgodfrey

I’m not familiar with the tutorial, but…

I assume your coin scene has a script on it. It’s probably missing a Vector2 definition for screensize. So, something like this:

var screensize = Vector2()

That did fix the issue, but removing the previously problematic line of code also seemed to achieve the same desired effect (the effect being that it randomly places the coins on screen within the allowed region) so i’m not entirely sure what they were trying to do with it. I’ll have to read through this section of the tutorial again to see if this is just an error on their part or if I misread something. Thank you for your help!

Andromachus | 2020-03-02 03:41