Either is fine. If you’re going to be reusing the function across other scripts, it’d make sense to put it in a library, but if you’re only going to be using it within a single script, then keep it in that one.
The advantage of a script library is that you’ve only got a single instance of the function. Like this RandomFloat function here; I’m mostly using that for delays and ranges across spell scripts. Now, if I wanted to affect all the random floats in all my spell scripts at once, I could alter the function in the library, which all of those scripts are using, whereas, if each spell script had their own instance of the function, I’d have to open each spell script and put the edits to the function in one at a time.
If you plan ahead for that, you can save yourself a whole lot of refactoring (rewriting/restructuring/simplifying code without changing it’s external/visible capabilities) later on.
But to continue with the spell scripts example; while setting up new spell templates, I’m starting to repeatedly script similar functions for different spells - which I then later combine into merged, reusable functions that get added to the library. Without adding the individual spells and seeing what they need to work, I wouldn’t have the “individual instances” of code that I could later merge into functions. So I think there isn’t really one ultimate catch-all “Do it this way! This way is The Correct Way!”-method there, and if you want nice, clean, easy-to-use code, you’ll need to refactor it later one way or another.
For data and variable stuff like this, a library would make a lot of sense, though, since this is stuff that’s very likely to be reused in other scripts in the future.
This is a float type function, which means that it returns a float, and can be used anywhere a float is called for as a parameter, such as for DelayCommand()'s first parameter (a float).
DelayCommand(CBS_RandomFloat(300.0, 60.0), InsertDelayedFunctionCallHere);
For understanding functions, acquire a rubber duck, and then read the code to it one line at a time while it serenely smiles at you like the little duckie angel it is:
If max value is less than or equal to min value (user input error), always return min value.
Assuming a max value of 10.5, min value of 0.5.
10.5 - 0.5 = 10.0
Multiply remainder by ten:
Turn 100.0 into an integer; 100.
Get a random number from 0 to (99+1). (Random(100) is 0-99 because the zero counts as the first number… Random(2) returns either 0 or 1)
Let’s assume we got a 100 (max result).
Turn random number into float again and divide by ten:
100.0 / 10.0 = 10.0
Add the previously removed min value back in:
::headscratch:: Don’t look at me, though. ::cough:: I don’t know what I’m doing either; I kinda just looked up other random float generating functions online and got annoyed that most of the ones I found never returned either the exact minimum or exact maximum value, so I tried to write one that does. This one seems to work as intended. Fingers crossed; it hasn’t made anything explode for me just yet. … except for all the actual explosions it’s being used for.
God I hope I’m not publicly talking mathematical/logical nonsense. >_<
Edited because the glorious power of Socratic Teaching has revealed an unnecessary step. Hooray! One less line of code.