[Closed] Proper(?) way of using own libraries
- So… let’s say I have a few .ms files for own library.
If the .ms file only has functions and variables, I just use “include”.
If the .ms file has struct. what would be the best way to use?
Declare global variable of struct name at the end of file and use “Filein” like this? Or… just use “include”?
myLib.ms
struct dothis ()
global dothis
myscript.ms
filein “myLib.ms”
- Should I reuse a struct over and over again if it is possible or I can make instances as many as I want? It there any problem if I keep making instances?
I think the answer depends on the ‘scope’ you need for your functions
If the library function struct is ONLY needed by the script your about to run then don’t use global variables and simply use filein.
Most of my scripts us this simple technique.
However. If multiple scripts need to share data from the same instance then the coding is a little more involved.
your mylib.ms function library should look like this:-
global myGlobalSingelton
global myLibraryStruct
Struct myLibraryStruct
(
.......
)
::myGlobalSingelton = myLibraryStruct()
note the :: is important for defining global scope
when you filein the library you need to check if the definition exists first. So any script that uses the library should do something like this:-
if myGlobalSingelton == undefined then
(
fileIn "... myLib.ms"
)
lastly to call functions from the library you’d use the ::: again like this
::myGlobalSingelton.somefunction()
For examples you can check out my scripts (for free).
use “3rd Party Plug-ins” paths
all scripts from these paths automatically load in the system.
the file with your libraries might be organized with structs and look like:
global GeneralMathOperations
(
struct GMathOperationsStruct
(
/** Two times the constant Pi defined as a float. */
TWOPI = 6.283185307,
/** Half of the constant Pi defined as a float. */
HALFPI = 1.5707963268,
/** The coefficient to convert the value of an angle in degrees into radians. */
DEG_TO_RAD = (PI/180.0),
/** The coefficient to convert the value of an angle in radians into degrees. */
RAD_TO_DEG = 180.0/PI,
/** A function macro to convert degrees to radians with float precision. */
fn _deg_to_rad deg = (deg * DEG_TO_RAD),
/** A function macro to convert radians to degrees with float precision. */
fn _rad_to_deg rad = (rad * RAD_TO_DEG),
on create do
(
)
)
global _math = GeneralMathOperations = GMathOperationsStruct()
ok
)
so you will get only one Global as instance of a struct
every tool (script) that uses this struct has to call methods as:
::GeneralMathOperations.halfpi
but if you use these methods many times in the code it might be clear and safer to use simple and locally unique name across whole code (for example):
rollout MyRollout "My Roillout"
(
local _math = if globalvars.isglobal #GeneralMathOperations do globalvars.get #GeneralMathOperations
/*
after that the rollout's body
where you can call :
_math.halfpi
*/
)
why do i do it like above?
the name _math is too common ant shouldn’t be used as a global because it can be easily redefined by other tools. the GeneralMathOperations is safe enough instead.
but _math is easy to use for debug purpose
in mylib.ms function library it doesn’t make sense. because in the code above you already defined the variable as global.
:: means to take (or put) the value directly from (to) globals HashTable instead of from global scope
Each filein also has scope. I was having issues when defining
global myGlobalSingelton
myGlobalSingelton = myLibraryStruct()
myGlobalSingelton wasn’t seen by my macro scripts. Adding the :: seemed to fix it.
OHHHHH… never thought about doing this.
What is globals HashTable?
And… should I use one struct over ans over again?
I know I;m lazy. But, sometimes I create an instance of struct in a loop like
for o in selection do (
local onparser = objnameParser()
onparser .parse o,name
)
I guess this is a bad habit.