• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

static if.....

Status
Not open for further replies.
Level 3
Joined
Nov 3, 2017
Messages
34
At me such specific question - tell about the conditional operator "statics if" how and when is used? I just met a lot where I could not understand what is different from the usual "if". Thanks
 
Static ifs are used such that at the flag's notice, it can change the behavior of your code.

For example, take Debug mode. You have functions that tell you what is happening as of the execution of certain parts of your code. When you disable debug mode, those messages do not appear. When enabled, those messages appear.
 
Level 12
Joined
Mar 24, 2011
Messages
1,082
Static ifs are used such that at the flag's notice, it can change the behavior of your code.

For example, take Debug mode. You have functions that tell you what is happening as of the execution of certain parts of your code. When you disable debug mode, those messages do not appear. When enabled, those messages appear.

This confused me a bit... are you saying they are executed during compilation time?
 
Level 14
Joined
Mar 11, 2017
Messages
587
What are the differential characters of compile-time ifs (static ifs if you prefer) on one hand, and runtime ifs on the other?
(while I think I have a general idea of the meaning of the compile time vs runtime difference when it comes to errors, I'm not able to figure out precisely what the implications for if statements might be)

In other words, could you please explain in simple terms when is using static ifs advantageous over the usual ifs?
And conversely, could you name an example situation when you do NOT want to use static ifs?

Would this example I just thought at be a case where using static if would be appropriate?
static if: - a constant that I suspect is already defined at compile time exists -
then: - use the predefined constant -
else: - explicitly define a guess-value for the nonexisting constant, and use the guessed value for the rest of the code
 
Last edited:
Level 12
Joined
Mar 24, 2011
Messages
1,082
What are the differential characters of compile-time ifs (static ifs if you prefer) on one hand, and runtime ifs on the other?
(while I think I have a general idea of the meaning of the compile time vs runtime difference when it comes to errors, I'm not able to figure out precisely what the implications for if statements might be)

In other words, could you please explain in simple terms when is using static ifs advantageous over the usual ifs?
And conversely, could you name an example situation when you do NOT want to use static ifs?

Would this example I just thought at be a case where using static if would be appropriate?
static if: - a constant that I suspect is already defined a compile time exists -
then: - use the predefined constant -
else: - explicitly define a guess-value for the nonexisting constant, and use the guessed value for the rest of the code


Am, it is not exactly "static if" vs "regular if" as they are used for completely different things.
There is not really a comparison or advantage as they have very different purposes.
"static if" would be used to compile the code and you have
Code:
static if(conditions)
    compile code
and "if" would be to run the code and you have:
Code:
if (condition)
    logic & actions

Imagine the following for static if:
JASS:
//This is a bizarre mixture between C & JASS syntax and some random custom simplified library, look the other way.

function ButtonDown takes nothing returns boolean
    static if (OS == Windows) //Static if for compilation
        if (Windows.mouse.position == positionOfButton) and (Windows.mouse.button1.buttonDown == true) //normal if for logic
            return true
        end if
    else if (OS == Android) // part of static if for compilations
        if (Android.screenTap.position == positionOfButton) //regular if for logic
            return true
        end if
    end if
    return false
end function

If we are using a Windows the code that would run would be:
JASS:
function ButtonDown takes nothing returns boolean
    if (Windows.mouse.position == positionOfButton) and (Windows.mouse.button1.buttonDown == true) //Logical if
        return true
    end if
    return false
end function
The rest of the code (the android part) would not exist in what is ran.

You could by all means do the following, without static if, just normal if:
JASS:
function ButtonDown takes nothing returns boolean
    if (OS == Windows)
        if (Windows.mouse.position == positionOfButton) and (Windows.mouse.button1.buttonDown == true)
            return true
        end if
    else if (OS == Android)
        if (Android.screenTap.position = positionOfButton)
            return true
        end if
    end if
    return false
end function
But now you have all this code that is compiled and ran. Each character takes space in your program and the OS check is also ran every single time the function is called, do you need this extra size and computations? No.


Hope this helps!
-Ned

Edit//I've noticed that I've done if(X = Y) instead of if(X == Y) :p
 
Last edited:
You could make something like:
JASS:
static if LIBRARY_UnitIndexer then
    do_some_indexer_things()
else
    do_some_not_indexer_things()
endif
.. to check if a certain library, like here library UnitIndexer exists in code, or not (or checking other constant booleans), and to arrange code as needed.
People for example use it to check if Table instead of hashtable could be used, EventRegistration things, etc.
 
Only one code block exists in the end, other one won't be compiled and be just thrown away. Example:
JASS:
                static if not LIBRARY_TimerUtils then
                    set tmr = CreateTimer()
                    set table[GetHandleId(tmr)] = this
                else
                    set tmr = NewTimerEx(this)
                endif
The goal is to get a new timer. If TimerUtils exists, he gets a recylced timer from library TimerUtils ( using NewTimerEx())), and if ithe library doesn't exist the code line creates a new one.

You can do similar things for events you maybe want when a unit enters the world. If a UnitIndexer exists you can straight use the IndexEvent, but if not you have to create a new trigger.

It's used to have any lines of code, like maybe whole function blocks optionally compiled in code, as they are only needed if some conditions met. Otherwise I or remove them completly, or need to achieve the same goal with other code.
 
Status
Not open for further replies.
Top