0 votes

I tried to make my first game in godot. I followed the Godot Documentary for the 'first Game' documentary. I then got this error:

" Invalid type in built-in function 'rand_range'. Cannot convert argument 1 from Nil to float."

The error is in the last line of code of the main file. I hope someone can help! :)
Have a nice day! :)

Here the main file:

    extends Node

export (PackedScene) var Mob
var score

func _ready():

func new_game():
    score = 0
    $HUD.show_message("Get Ready")

func game_over():

func _on_StartTimer_timeout():

func _on_ScoreTimer_timeout():
    score += 1

func _on_MobTimer_timeout():
    var mob = Mob.instance()
    var direction = $MobPath/MobSpawnLocation.rotation
    mob.position = $MobPath/MobSpawnLocation.position
    direction += rand_range(-PI/4, PI/4)
    mob.rotation = direction
    mob.set_linear_velocity(Vector2(rand_range(mob.MIN_SPEED, mob.MAX_SPEED), 0).rotated(direction))

Here the Code for the Mobs:

extends RigidBody2D

export (int) var MIN_SPEED
export (int) var MAX_SPEED
var mob_types = ["fly", "swim", "walk"]

func _ready():
    $AnimatedSprite.animation = mob_types[randi() % mob_types.size()]

func _on_VisibilityNotifier2D_screen_exited():

Here the code for the player:

    extends Area2D

signal hit

export (int) var SPEED
var velocity = Vector2()
var screensize

func _ready():
    screensize = get_viewport_rect().size

func _process(delta):
    velocity = Vector2()
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    if Input.is_action_pressed("ui_down"):
        velocity.y += 1
    if Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    if velocity.length() > 0:
        velocity = velocity.normalized() * SPEED

    position += velocity * delta
    position.x = clamp(position.x, 0, screensize.x)
    position.y = clamp(position.y, 0, screensize.y)

    if velocity.x != 0:
        $AnimatedSprite.animation = "right"
        $AnimatedSprite.flip_v = false
        $AnimatedSprite.flip_h = velocity.x < 0

    if velocity.y != 0:
        $AnimatedSprite.animation = "up"
        $AnimatedSprite.flip_v = velocity.y > 0

    func _on_Player_body_entered(body):
        call_deferred("set_monitoring", false)

    func start(pos):
        position = pos
        monitoring = true
in Engine by (15 points)

1 Answer

+2 votes
Best answer

If you look closely at the error message, the issue is with this: rand_range(mob.MIN_SPEED, mob.MAX_SPEED)

Specifically, "argument 1" is "Nil". Argument 1 is mob.MIN_SPEED.

From this, we can deduce that you haven't assigned a value to the mob's MIN_SPEED property (I'm guessing not MAX_SPEED either). In your mob scene, click on the root node (the RigidBody2D) and set these values in the Inspector.

by (20,475 points)
selected by

Thank you so much! But what does "Nil" mean?

"Nil" is another word for "nothing" or "null value".

thanks! I really like your videos, they help me a lot. Thank you!! :)

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 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 webmaster@godotengine.org with your username.