Ifdef equivalent in maxscript


So I have script a which is in file a.ms and scripts b and c, which both use script a. Both scripts b and c start with filein(a.ms), but when using both scripts at the same time it could cause some problems and it looks like the reason is bekause both scripts importing the same script. In C++ there is a solution for such problem called ifdef, is something like this also avaible in maxscript?


you can use the same mechanics as #ifdef, meaning that your filein code has to define a global variable and check its existence to avoid multiple execution.


And how to do that? My maxscript skills are very low.


So is there is no existing solution for my problem? I mean solution by denisT should worck, but I have not enough skils to implement something like that myself.
To be more precise about my problem, when importing a script containing attributes more than 1 time makes the attributes f*cked up.


It seems it’s not a maxscript problem but an imagination problem. DenisT gave you the clue.
In a.ms

Global gtafan_scriptALoaded = true

In both b and c scripts:

if ::gtafan_scriptALoaded == undefined do filein(a.ms)


Like I said it´s more a problem of my limited maxscript skills then imagination, but we don´t need to argue about it. Just 1 question do I really need that “::” in the if statement?
And I am really greatfull for your solution.




Also, I really hope you´ll not take me wrong, but the solution from your post have 1 disadvantage real ifdef doesn´t have:

  1. in every script using script a you need to write the Line:

if ::gtafan_scriptALoaded == undefined do filein(a.ms)

  1. If importing any script using script a first and then script a itself causes still the problem mentioned befor.

A real ifdef equivalent would look more like something like this at the begining of a.ms:

If myVar==undefined then global myVar=OK
else exitLoading()

exitLoading() should just stop loading the scrip.


Come on! You are very close!
Think a little more and you’ll get it.


Posibly I am close, but have no idea how to implement that exitLoading, return, break or even exit would not do, the only way could be throw, but it´s ugly and try catch is necesary and I am not tallking about the case when importing the script direktlly.


Just put your code inside the ‘if’ block and set the global inside.


So if worcks in this case exactly like ifdesf? I was thinking about such solution, but you completlly right, my imagination stoped me from doing that, as in programming languages, I normally using like C++, something like this would never worck. Many thanks.


Nah it wasn´t as easy, I tried firs this:

if myVar==undefined then
 global myVar=OK

MyAttribute = attributes "my attribute"


It didn´t worck, as MyAttribute was seen as a local whatever and not avaible after exiting if statement, at the end have to change my code to:

  if  MyAttribute==undefined then

    global MyAttribute = attributes "my attribute"


Which seems to worck.


It is a great discovery and completely undoes the previous logic. Congratulations!