- Edited
My project its growing at a steady pace and I'm proud to say after 3~4 years programming on and off with GDscript as my very first programming language 90% of the problems that bring me to a halt are no longer related to programming. But that doesn't mean I don't have doubts or that I'm doing things right.
My fundamentals are not terrible but I would consider that I'm still pretty ignorant about godot and programming in general as an example I don’t always know what godot is doing with memory. I've written C++ for a couple of small arduino projects but it's not the same and I was using libraries. Reading other people’s code is still pretty hard for me.
I have some problems where I would like to hear how you would solve them. I wrote down the solutions I know of and my uneducated thoughts about them.
Example 1:
A class that needs a couple of variables from another class.
Constant: This is what I mostly use but sometimes I need to change the values.
Static variables: Seems like the tidiest option if you need to edit the variable and you only need a couple of them but I don't know how they compare to other options in terms of memory/cpu.
Autoload: I know that creating an autoload just for a couple of variables may be a waste but what about adding the variables to an already created autoload?.
Signals: You can use signals but making a lambda or a method just to set and get individual variables doesn’t seem right to me.
Add the instance of the class as a variable in the script that needed them at runtime.
Starting off this was what I mostly did. It was required to populate the script variable with the instance before the script required the variable. When would you consider using this method over any of the other choices in this specific circumstance where you only need a couple of variables?.
Example 2:
A class that needs both variables and methods from another class.
For this case in particular I prefer to store the instance in a variable to use as needed. What’s the difference to calling the method from an Autoload, an instance stored in a variable and a static function?
Before they introduced static variables I thought static functions were only there to prevent the use of public variables which means all variables outside the function since godot doesn’t have private variables.
Example 3:
Storing a functions return value instead of using it directly.
In case I didn’t explain myself clearly:
func foo():
var dict :Dictionary = get_dictionary()
var name:String = dict[0]
var age :int = dict[1]
var height :float = dict[2]
use_something(name)
do_another_thing(age)
print(height)
Would you do something like this? Checking the type and duplicating the data in memory is kind of a waste but how much of a waste is it? I find myself doing this from time to time to infer the type on dictionaries or just for readability.
Example 4:
What kind of array is better for iteration?
If you have a for loop for example and you can choose between a PackedVector2Array and a simple Array which would be better? I know PackedArrays are more memory efficient but does it have a cost to retrieve the data from them?