0 votes

Via inspector I chose my enemy and depending on this choice, it will add the enemy as a child to the scene, this object has a kinematic body with a script attached to it with stats.

The question is, how do change variables in my match? Normally it would be like: $Object.value = 100 or something like that but that doesn't work here, neither does enemy.speed = 100, instead I get an error: invalid set index 'number' (on base: 'KinematicBody2D') with value of type 'int'

instance has a variable called speed, I get this error with every variable.

This is my whole setup:

    extends Node

enum ENEMIES {
    slime,
    goblin,
    mushroom,
    fly,
    worm    
}

export(ENEMIES) var enemies # = ENEMIES.goblin -> set default choice, if not, 
                            #the first enemy in enum gets picked as default

var slime = preload("res://Scenes/Enemies/Slime.tscn")
var goblin = preload("res://Scenes/Enemies/Goblin.tscn")
var bat = preload("res://Scenes/Enemies/Fly.tscn")
var fungi = preload("res://Scenes/Enemies/Fungi.tscn")
var worm = preload("res://Scenes/Enemies/Worm.tscn")

func _ready():

    match enemies:
        ENEMIES.slime:
            var enemy = slime.instance()
            add_child(enemy)

        ENEMIES.goblin:
            var enemy = goblin.instance()
            add_child(enemy)

        ENEMIES.mushroom:
            var enemy = fungi.instance()
            add_child(enemy)

        ENEMIES.fly:
            var enemy = bat.instance()
            add_child(enemy)

        ENEMIES.worm:
            var enemy = worm.instance()
            add_child(enemy)
in Engine by (143 points)
edited by

1 Answer

0 votes

Guess you need enemy outside _ready() too.

Checking with https://docs.godotengine.org/en/stable/classes/class_kinematicbody2d.html

Kinematic bodies are special types of bodies that are meant to be user-controlled.

You can move_and_slide them though.
This movement is done in func _physics_process

...
var enemy :KinematicBody2D
func _ready():
    match enemies:
        ENEMIES.slime:
        enemy = slime.instance()
        add_child(enemy)
...
func _physics_process(delta):
    #enemy.speed = 100
    enemy.move_and_slide(your settings)
by (638 points)
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.