The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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

in Engine by (23 points)

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

1 Answer

+1 vote
Best answer

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()
by (22,674 points)
selected by

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!

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.