Invalid get index and weird graphical errors

Godot Version

v4.2.2.stable.official [15073afe3]

Question

Hello! I’m a few days into Godot and also programming in general. I had this weird game idea of hitchhiking. I made a mechanic to raise your thumb, environment, models etc. everything worked fine, until i started programming the core mechanic. I wanted the game to detect if the thumb is up or not, and according to that a car would start moving. But when i tried referencing the variable for the thumbs up mechanic, the game sometimes would give a weird black and white window or would just not open. I’ll give any photos related with this issue down below.


The car movement script (it’s not finished, I’m just trying to figure this problem out.)


Thumbs up script (works fine)

image
image
The error and its source


Issue 1 (when i detach the script completely, the game works fine.)


I added @onready to the “var waitingforcar = handobject.waitingforcar” line in the first picture. The game now started, but the mesh instance didn’t move.

I also found this weird graphical error while trying to find and answer on reddit.
I don’t remember what caused it, but here is it anyway.
image
Here is that black and white game window i was talking about.

(To clarify, my game doesn’t consist of glowing incomprehensible shapes on a full black sky.
the map isn’t even shaped like this image.)

If you could help, i would very, VERY much appreciate it. I think it’s a small issue but the solutions that worked for other people that faced this issue didn’t work for me

Even if you don’t help (which wouldn’t be a problem at all) I’m sure I’ll figure something out.

Thanks.

Please paste scripts between three ticks like so

```
type or paste code here
```


You are trying to use a variable before it is ready

@onready var handobject = $"etc" # onready
var waitingforcar = handobject.waitingforcar # before ready

It’s generally a bad idea to @onready a parent path. Nodes are only ready when all of their children are ready, so if you are looking for a parent path it will not be ready during the @onready in this script.


This line, if it worked, would copy the boolean value, and remain false forever. You cannot create references to boolean values, so you will have to use handobject.waitingforcar instead.

var waitingforcar = handobject.waitingforcar # copy of bool

if handobject.waitingforcar: # using handobject reference

also @onready var car = $"." is equivalent to self, which is implicit. These two lines are the same and you should prefer the latter.

car.position = Vector3(-0.4, 1.0, -320)
position = Vector3(-0.4, 1.0, -320)

Thank you! This fixed it for me. Hopefully I’ll remember these things. Also, thanks for the tip on three ticks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.