When wanting to visualize a project I get an error Invalid operands 'Vector2' and 'Nil' in operator

recosarecosa Posts: 1Member
edited October 2018 in 2D

I was doing a project, tenrmine with the movements and save it before I saved the probe and it worked fine. I opened it in the afternoon and it gave me error when visualizing the project. Specifically this Invalid operands 'Vector2' and 'Nil' in operator
the code

extends Area2D

export (int) var velocidad
var movimiento = Vector2(32,16)
var limite
signal golpe
func _ready():
    hide()
    limite = get_viewport_rect().size


func _process(delta):
    movimiento  = Vector2(32,16)
    if Input.is_action_pressed("ui_right"):
        movimiento.x += 1
    if Input.is_action_pressed("ui_left"):
        movimiento.x -= 1
    if Input.is_action_pressed("ui_down"):
        movimiento.y += 1
    if Input.is_action_pressed("ui_up"):
        movimiento.y -= 1
    if movimiento.length() > 0:
        movimiento = movimiento.normalized() * velocidad

    position += movimiento * delta
    position.x = clamp(position.x, 0, limite.x)
    position.y = clamp(position.y, 0, limite.y)

func _on_Player_body_entered(body):
    hide()
    emit_signal("golpe")
    CollisionShape2D.disabled = true
func inicio(pos):
    position = pos
    show()
    CollisionShape2D.disabled = false

Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 1,000Admin

    Which line does the debugger say is causing the issue?

    What the error is saying is that some variable is null, and it is trying to be combined/operated with another variable that is a Vector2. This is probably due to a variable not being initialized correctly, or at least that is generally what causes the problem for me.

    I think the problem is that velocidad is null when you are multiplying it in _process. I would either try adding the following to _ready:

    if (velocidad == null):
        velocidad = 1
    

    And/Or double checking that velocidad is set in the editor. Then that may fix it the problem if the problem was that velocidad is null. Without knowing which line is causing the problem, that is what I would try to do :smile:


    Side note: If you indent all the code with a single tab or surround the code with ` (on my keyboard it’s on the Tilda key), then the code will be formatted to look like code in your post :smile:

  • MegalomaniakMegalomaniak Posts: 1,059Admin
    edited October 2018

    @TwistedTwigleg said:
    Side note: If you indent all the code with a single tab or surround the code with ` (on my keyboard it’s on the Tilda key), then the code will be formatted to look like code in your post :smile:

    The bold part in particular is meant for single line/in-line code. In the meanwhile I fixed it for the OP by quickly editing the opening post.

Sign In or Register to comment.