After using the engine for a few months, I’m still not sure what belongs in a process function and what does not. If I have a simple “if” statement based on a boolean variable for example, should this be in process? It seems like a waste to check it every frame, especially when it depends on a very specific event or player choice.
Or maybe the variable should be inside a set function that only executes when the variable changes? If that was the case I would have set functions for almost every variable.
Is it okay to load up _process with many things and not worry about it? What do you guys do?
It depends on whether a check is necessary every frame. If you’re interested in whether the ‘up’ button is being held over a period of 180 frames in total, then _process can be very useful for this.
If you’re just interested in one-off events (e.g. did the player press the ‘start’ button on the screen, has the player entered the loading zone to the next scene, has the foo done the bar, etc), then signals are probably a much better choice. With signals you can execute a specific function when a condition you specify occurs.
The Dodge the Creeps tutorial has an example of how to use signals.
I think you don’t have to worry that a few ifs in the _process function would negatively impact your game’s performance. Of course, it’s not recommended to call computationally intensive functions every frame, but evaluating simple conditions takes just a few milliseconds — you won’t even notice it. In my latest game, I use dozens of ifs every frame and the speed still stays at maximum FPS.
You can continuously monitor performance using Engine.get_frames_per_second().
Refactoring is one of the joys and pains and programming. You cannot anticipate every problem. You can try, but ultimately when writing software, functioning is the most important part. Optimization comes when you see what needs to be optimized. So, if your game is running slow - worry about it. Otherwise, don’t worry about it.
Over time as you learn you will move some of that code out of the _process() and _physics_process() functions as you find other ways to do things. For example, checking input in _input() and _unhandled_input().
What I have done recently is break out every state into its own node. So all my jumping functionality is in a Jump node. If I remove that node from my StateMachine for the character, it stops processing. I can do the same for abilities on enemies and players. If I change the Jump ability to Fly I swap the nodes out. There are no if statements to check which it should use - the code is just gone.
But all this is like iteration 20 of my character controller and is being built based on numerous tutorials, ideas I’ve gotten here, and my own experience in making character controllers over and over and getting tired of programming the same thing over and over.
So I recommend you don’t worry about it too much, and solve the problems you run into as you run into them.