Godot Version
4.2
Question
I am implementing a PID loop that will be used by my character controller and items in the level such as non-player vehicles and characters. Therefore I’d like it to be its own class that can be instantiated when needed. With so many potential calls to this class, I want to make sure that it is as performant as I can make it (within reason). So what is the best approach (using GDScript)?
He’s referring to PID controllers (see this). It’s a common way to control machinery and other hardware-related systems.
2 Likes
I’m not quite sure what you’re looking for as a response. There’s only a couple of places to store a class instance, right?:
- The node tree (if it’s a node)
- In a variable
- In a resource (.tres)
All of these may be valid depending on your workflow and what you want to achieve. Personally, I would make a custom resource (here’s how) and then create a folder of them which all displayed unique behaviour or parameter combinations. The resources could then easily be used and hot-swapped via the Resource Loader or an exported variable to alter the behaviour of one of your objects (character controller, vehicles etc.).
Does this answer your question?
Given that you already know of PID controllers, I’m going to go ahead and assume you know how to implement one.
What I’m really wondering is whether it would be more efficient, with dozens or hundreds of calls, to implement it as a node or resource. I know how to call it and use it, I just don’t want it kill my framerate.
I don’t see how calling a function from a node is any different than calling a function from a resource.
Why do you think this would kill your framerate?
EDIT:
Don’t be afraid of just giving it a go. Whatever approach you use, you will learn what is best. If it ends up not being performant, that is when you should fix it – not before it’s a problem.