+1 vote

When should I create my own classes? For example, I want to create a fireball spell, I create a scene and in the scene script I create an instance of that spell class to access the methods. Am I doing it right? What would be the right thing to do in my case?

Godot version 3.3.4.stable.mono.official
in Engine by (154 points)

1 Answer

+1 vote
Best answer

In object oriented programming, you would at least create a class for everything that you want to appear in your program. The same goes for Godot. In Godot this is more appropiately created through the scene.
In the case of your firespell, you should create a scene based on a fitting node, maybe an Area(2D), Mesh, ... and attach all specific parameters that it needs as sub nodes. If you find that you implement something you would like to reuse for let's say an ice spell, then you should try to create a spell scene where you can put all the stuff a spell should have in your game, like attributes, maybe some general functions like cast(), hit() and so on that will be implemented in your specific fire/ice/whatever spells.
I would suggest you start with a specific fire spell scene and work your way up through refactoring. See what you copy from the fire spell scene to the next spells scene, that's what you should implement/add in the more general spell scene.
If you already have a coding background, I suggest you look at this page https://www.geeksforgeeks.org/introduction-of-object-oriented-programming/
It gives a good introduction into the basics of object oriented programming. Just think of objects that they mention as the node instances you want to spawn of your spell and classes are the scenes in Godot.
Disclaimer: Of course you can also create classes in Godot, but I don't think thats what you are looking for with a component you want to visualize in the game.

by (102 points)
selected by
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 webmaster@godotengine.org with your username.