The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

Hi everyone!
I have around 30 kinds of enemy's and each with approx. 10 levels. And each of the level has some variables like:

hp
hp_max
damage = [1,2,3,4]
attack_names = ["atk1', "atk2", "atk3'. "atk4"]
#etc

earlier I had decided to make a dictionary for each level's variables.
And now I think making a class for enemy and an object for each level would be better. But I don't know how to create a class how to make an object out of the class. Please tell me about it

Godot version v3.2.3-stable_win64
in Engine by (942 points)

2 Answers

+1 vote
Best answer

The benefit of classes are inheritance, function calls, etc. And you may not know this but you're already familiar with classes if you used GDscript with nodes as they function more or less the same way

Try to avoid using classes if your intention is to store data as even though the difference is micro classes take longer to construct.

To create a class

class EnemyClass:
    enum ATTACKS {atk1, atk2, atk3, atk4}
    var hp = {"max":10, "amt":10}
    var damage = [1,2,3,4]

    # what todo on enemy created
    func _init():
        pass

    func heal(amt):
        hp.amt += amt
        if hp.amt >= hp.max:
            how.amt = hp.max

    func receive_damage(dmg):
        hp.amt -= dmg
        if hp.amt <= 0:
            queue_free()

To use a class by creating an object

func ready():
    var enemy1 = EnemyClass.new()

    #Class aren't much different from Dictionaries when manipulating values.
    print(enemy1.hp.max)
    enemy1.receive_damage(3)
    print(enemy1.hp.amt)

Further information

by (6,942 points)
selected by

Means having 300 dictionaries is better than having one enemy class and having 300 enemy objects out of it. I am not interested to save enemy's stats.

I don't believe that class can be accessed without loading the class file with load(), or adding a class_name definition at the top of the class file. class is used for nesting one class inside another. Load the class file using onready var class_file=load("res://gdrespath") and then use class_file.EnemyClass.new() to instantiate.

Or fix the class file as below to access the class globally :

E.g.

class_name EnemyClass

enum ATTACKS {atk1, atk2, atk3, atk4}
var hp = {"max":10, "amt":10}
var damage = [1,2,3,4]

# what todo on enemy created
func _init():
    pass
+2 votes

Hello. You can find it here

All GDScript files are classes. You can create "enemy.gd" file and here use

Construcor:

func _init():
   pass

Variables and Methods

var health = 100

func attack(foo):
    pass

And create object using this

const Enemy = preload("enemy.gd")

func _ready():
    var enemy = Enemy.new()
    enemy.attack(...)

More info about classes (and inner classes) you can read in documentation (link above)
Hope it helps

by (102 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 Frequently asked questions and 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 [email protected] with your username.