Should I make a more efficient way of organizing the functions for items?

Hello!

I am in the process of developing a first-person game involving items with varying functions. But recently, I’ve been having second thoughts about how I organize all of these functions.

Currently I have a large module script consisting of all an array of functions which a main controller script uses to execute said functions. I’m worried that this is an inefficient way of storing functions and could end up confusing by the time I have a plethora of different items in the game.

Are there any other methods I could perhaps use to sort these functions? If so, please let me know! Thanks! :slight_smile:

1 Like

Depends on what kinds of functions are in the array. If they’re pure functions, that’s fine. If you’re editing tables and stuff from within scripts, it’s probably best to put those functions where they’re most appropriate (where they’re used most)

Can you show some examples? chars chars

I clearly understand the problem that you are facing. Basically, everyone has their own best way to handle where they should put functions for specific things. Easiest way to think about this is when you write a new function, think first if you will use that function again in a different places. For example, if you have giveMoney() function, there will be so many actions that you need to give money to players, so in order to prevent you from writing that code again, putting it in the ModuleScript is a good solution. However, if you have like shootGun() function, you know that letting one Script handles it should be enough, then putting the code inside Script. If you change your mind later, you can just copy, paste into a ModuleScript. Also, multiple ModuleScript can also be used, it is a technique called, “Separation of concerns” where you try to put code based on its functionality. If functions have quite similar functionalities, then put it together so you don’t have to scroll through thousand lines of code looking for one specific function that you may try to fix. Last, it is all about the simplicity. Make it easy for you.