#About correctly written scripts

1 messages · Page 1 of 1 (latest)

timid cairn
#

Hello! I learn engine Godot and I have a question. I have character, who can run, jump and fight. I think it's not good when you write all scripts in one file. How do write scripts correctly? May be every action must be written in different files? Or I must create one main script file and connect to him other script file?

How to write correctly scripts in terms of optimisation?

It's my first game engine. 🙂

spice adder
#

In Godot, each script is its own class. That means each script represents its own 'thing' and its behavior.

To answer your question, I don't understand what you mean by 'all scripts in one file'. Every script file is only ever one script, one class. It can have multiple methods (functions), but those do not by themselves make a script.

Also, in Godot, each object can only have one script attached....

timid cairn
#

animation scripts should be in the animation NODE, movement scripts should be in the hero NODE, and battle scripts should be in the weapon. Something like that?

spice adder
#

I mean I think you should just learn the Godot basics at this point.

timid cairn
#

Ok, I will read 🙂

fervent quarry
#

Separating behaviors can be useful at preventing messiness, but it's more advanced. So if you're just starting I would say - keep it in mind, because it's a good idea to learn that separation where it makes sense to use it, but it's not useful to make things too complex for a starter project.

If you do want to get to it later, the pattern of "composition" might be what you have in mind, there are some videos on it on YT. Like in my game, I have a "Targeting" node that handles anything related to finding a new target. It only does that, so if I slap it on as a child of a Enemy, or a Tree, it doesn't matter, both of them can acquire targets in the world.

nimble abyss
#

Please don't think that because everything is a single gdscript file it is a wrong way of coding. An example of separated files to control a character is the Finite State Machine example: https://www.gdquest.com/tutorial/godot/design-patterns/finite-state-machine/ Bear in mind that if you make a game like Sonic the Hedgehog, where you can run left, right and jump, it is perfectly fine to have the script in a single file and probably doesn't require an FSM. Why over complicate it?