StateMach1ne

Scope and how to keep track of target_list array

target_list's scope is the entire turrent.gd script/class I assume? Assume that target_list (or any other variable for that matter) is modified by, let's say at least 7 different functions - would it be a feasible approach to pass target_list as value between functions which change it, in order to better keep track?
  • Nathan Lovato replied

    Arrays are always passed by reference so even if you manually passed the target list between functions you would always be referencing the same source array in memory.

    The only way to make pure functions, in this case, would be to copy the contents of the array in every function and return a new array, but that would make the code both more complex and quite a bit costlier performance-wise.

    More importantly, you would still need to store the target list somewhere as the array needs to be referenced or it will get freed automatically.

    Now, if your issue is keeping track of where the target_list is used, the scope of one script is relatively small, especially when you use a game engine that simplifies tons of code for you. If you struggle to keep track of things right now, I would recommend to keep training and see if it doesn't become easier over time. You will need to practice to feel comfortable with more code and wider scopes when working on released games.

    But the good news is with practice you can become comfortable with even huge codebases.

    The main trick I would use in this case is whenever you're unsure where target_list is used, double click the name and press Ctrl+f to do a text search, and cycle through the occurrences.

    If you are already experienced enough in code and have a third-party code editor installed, you can use more advanced search features such as "symbol references". In my code editor, I can use that to see in which places a variable or type used.

    How it works and what features you have exactly depends on the code editor, and it's also beyond the scope of the course as it's more advanced, but I'm sharing it just so you know this exists, in case you don't already.

    1 love
  • StateMach1ne replied

    Thank you very much for the detailed explanation, Nathan!