godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript inconsistent code behavior, giving error "Invalid operands 'Vector2' and 'Nil' in operator '*'"

Open Woolton opened this issue 4 years ago • 3 comments
trafficstars

Godot version

3.3.3 stable

System information

Windows 10, GLES3, Nvidia GTX960m

Issue description

When I created my movement script, it initially worked. After extending a custom class, the below movement script immediately stopped working. Giving the error "Invalid operands 'Vector2' and 'Nil' in operator '*'"

extends Area2D


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var direction = Vector2.ZERO
var bullet = load("res://Bullet.tscn")


var screen_size = Vector2.ZERO
func shoot():
	pass
func bomb():
	pass
func movement(delta):
	#general
	var speed = 150
	if Input.is_action_pressed("right"):
		direction.x += 1
	if Input.is_action_pressed("left"):
		direction.x -= 1
	if Input.is_action_pressed("down"):
		direction.y += 1
	if Input.is_action_pressed("up"):
		direction.y -= 1
	#normalizes diagonals
	if direction.length() > 1:
		direction = direction.normalized()
	#position changes
	position += direction * speed * delta
	position.x = clamp(position.x, 0, screen_size.x)
	position.y = clamp(position.y, 0, screen_size.y)#movement controls
func action():
	if Input.is_action_pressed("shotA"):
		print("shot A")
		shoot()
	if Input.is_action_pressed("shotB"):
		print("shot B")
		shoot()
	if Input.is_action_pressed("bomb"):
		print("bomb")#action controls

# Called when the node enters the scene tree for the first time.
func _ready():
	screen_size = get_viewport_rect().size
	print(screen_size)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	#movement
	movement(delta)
	#action
	action()

On occasion, simply restarting godot fixes this error. On other occasions, adding a print call fixes the error.

Steps to reproduce

  1. Open Godot
  2. run game

Minimal reproduction project

dupe - Copy.zip

Woolton avatar Sep 27 '21 03:09 Woolton

Steps to reproduce

1. Open Godot

2. run game

Minimal reproduction project

dupe - Copy.zip

Can't reproduce, I don't see "Invalid operands 'Vector2' and 'Nil' in operator '*'" error.

Besides that please provide the whole error log, not just the error message. That's what I've got after running your project:

SCRIPT ERROR: GDScript::reload: Parse Error: The identifier "origin" isn't declared in the current scope.
          At: res://Bullet.gd:12
ERROR: reload: Method failed. Returning: ERR_PARSE_ERROR
   At: modules/gdscript/gdscript.cpp:585
SCRIPT ERROR: GDScript::reload: Parse Error: Unexpected token: Identifier:speed
          At: res://prefabs/Enemy.gd:5
ERROR: reload: Method failed. Returning: ERR_PARSE_ERROR
   At: modules/gdscript/gdscript.cpp:585

There's nothing wrong with these errors themselves, you'd need to fix your scripts to get rid of them.

kleonc avatar Sep 27 '21 10:09 kleonc

I also can't reproduce the bug with the provided project. I don't even get any error.

KoBeWi avatar Sep 27 '21 23:09 KoBeWi

Still happens in Godot 4.2, I don't really know what triggers this, sorry.

MiTsSsS avatar May 13 '24 21:05 MiTsSsS