Conditions vs. Action-ifs

Matrixboy

New Member
Reaction score
1
I'm sorry, I scoured the forums for the last twenty minutes (searching the word 'condition' gives quite a lot of solutions, believe it or not. =P), and was unable to find the answer to this. I may have overlooked it, or searched incorrectly, but... :/

Well, I've always been curious. I always thought skipping any extra function call would be good, so I would just have actions do the checks, however, doesn't that also make the code run more often, since the events are mostly generic? Would it be better to use conditions so it didn't bother reading the action all the time something happens?

I guess in easier terms, is it better to use Conditions, or skip them and just use an if() in the Actions?

EX:

JASS:
function Trig_Selling_Actions takes nothing returns nothing
    local item checkItem = GetManipulatedItem()
    local integer x = 0
    local unit character = GetTriggerUnit()
    if (GetItemName(checkItem) == "Tournament Registration Forms") then 
        loop
            exitwhen x == 5
            if( GetItemName(UnitItemInSlot(character, x)) == "Tournament Registration Forms") then 
                call UnitDropItemTarget(character, checkItem, gg_unit_n00L_0150)
                endif
            set x = x+1
        endloop
    endif
        
endfunction


Would it be better for me to put that first If as a condition?

Also, I can't exactly find the trigger I was playing with before, but I was-- if I wanted to have two separate conditions to be filled?

JASS:
function cond1 takes nothing returns boolean 
    if a == b then 
        return true
    else
        return false
    endif
endfunction

function cond2 takes nothing returns boolean
    if a != c then
        return true 
    else 
        return false
    endif
endfunction

function condcheck takes nothing returns boolean
    if And(cond1,cond2) == true then
        return true
    else
        return false
    endif
endfunction

function InitTrig takes nothing returns nothing
call  TriggerAddCondition(Trig, condcheck true)
endfunction


Or would it be better to nest the two parts of the condition like so?

JASS:
function cond1 takes nothing returns boolean 
    if a == b then 
        if a != c then
            return true
        endif
    else
        return false
    endif
endfunction


function InitTrig takes nothing returns nothing
call  TriggerAddCondition(Trig, condcheck true)
endfunction



Sorry for the terrible writing of this question, but I'm not very good at explaining things. If anyone off-hand would know the answer to this, would you mind sharing? =)


Thanks in advance!


Travis.
 

Dr.Jack

That's Cap'n to you!
Reaction score
109
Do excuse me if I'm wrong it has been a while since I dealt with Jass or vJass.
Anyway I believe the difference is not much of an importance. The upside with conditions is that if the condition is false it will terminate the action's execution and thus save you a function call. It is also considered more readable. With that If/Then/Else would be faster most of the time I believe (someone please verify).

Anyway it doesn't really matter unless you are executing tons of functions. Just do whatever you feel like.
 

Exide

I am amazingly focused right now!
Reaction score
448
I always inline as much as possible.

Creating extra functions takes up a lot of space and makes it harder to read the code.
I'm not sure if it's more, less or equally effective, but I'm willing to risk it for the read-ability. (It's not like anyone's going to notice, anyway.)

Finally I would like to assume that inlining the conditions (not using a function) is more effective.
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
Conditions are more effective. But keep in mind that you cannot use any kind of wait within a condition-function.
 

Azlier

Old World Ghost
Reaction score
461
This isn't GUI, ya know. You can type any code wherever you want. You can put every action into the conditions to remove one function and gain some speed.
 

jwallstone

New Member
Reaction score
33
Yeah, writing everything together in either the condition or action does save function calls. I wonder if conditions might be slightly more lightweight than actions though, in that less functionality is enabled (TriggerSleepAction doesn't work in a condition, for instance, but you shouldn't really be using that anyway).

I think the only real reason to use both conditions and actions in Jass is for organization. It can help you keep things straight in your head. It also lets you reuse code. Always checking if the unit is hero? then write a condition and you don't worry about writing that into every function.
 

Matrixboy

New Member
Reaction score
1
JASS:
function Cond takes nothing returns boolean
    return a == b and a != c
endfunction


That is all you need.

*smacks forehead* I forgot about doing it that method, just connecting them together.


Exide said:
I always inline as much as possible.

Same, I would always try and subtract the amount of function calls, despite code un-pretiness (why should it be easy to read if it's called code? :p)

T.s.E said:
Conditions are more effective. But keep in mind that you cannot use any kind of wait within a condition-function.

Am aware of not being able to use waits, but wasn't that _everything_ was quicker inside conditions. Thank you! Is there anything else that cannot be used in conditions other than TriggerSleepAction()?

Azlier said:
This isn't GUI, ya know. You can type any code wherever you want. You can put every action into the conditions to remove one function and gain some speed.

Reason I was asking, since I have more control over my scripts, I'd want them to be as effecient as possible. Even if it's a not-noticable differerence, it makes me feel better.

jwallstone said:
Yeah, writing everything together in either the condition or action does save function calls. I wonder if conditions might be slightly more lightweight than actions though, in that less functionality is enabled (TriggerSleepAction doesn't work in a condition, for instance, but you shouldn't really be using that anyway).

That might be the reason! Since it uses less, or as Dr. Jack said
Dr. Jack said:
The upside with conditions is that if the condition is false it will terminate the action's execution and thus save you a function call.

That could be the reason too.

Jesus4Lyfe said:
Put everything in the conditions and make the conditions return false (the last line should always be return false).

This is the most efficient and stable way to do things.

Use timers instead of TriggerSleepAction.

Now, I've seen that somewhere before, I asked for help with a small trigger I was making, and something acting as an action was being put in the condition, and it was giving off a false. Would you be able to explain why you return it as false? I always wondered that.

And thanks for the straight-forward answer :)

Also, I stopped using TriggerSleepAction after I noticed it didn't work very well (I forgot where it didn't work very well, but it really angered me =P), and started using TimerUtils. But, after reading your paper, I JUST MIGHT have to start using Key Timers 1 (or maybe 2, I need to look up the specific pros/cons of each) ;)

Thanks for your help everyone!
 

Jesus4Lyf

Good Idea™
Reaction score
397
>Now, I've seen that somewhere before, I asked for help with a small trigger I was making, and something acting as an action was being put in the condition, and it was giving off a false. Would you be able to explain why you return it as false? I always wondered that.

Trigger:
- Event
- Conditions
- Actions

If we put all the "actions" code into the conditions, and have the condition then return false, the real Actions (which will be blank) will never be run. Efficient. :)

>started using TimerUtils. But, after reading your paper, I JUST MIGHT have to start using Key Timers 1 (or maybe 2, I need to look up the specific pros/cons of each)

Key Timers 1 isn't publicly available as such... I'd just recommend Key Timers 2. Key Timers 2 is the best system without being hard to use for low periods, TimerUtils is better than KT2 for high periods (low and high being, lets say, <0.3 seconds is low, >0.3 seconds is high, so for spells where something is done several times a second you use KT2, but for waits and such TU is more efficient if you like).

Personally I just use KT2. Each to their own, see what you like. Both are valid.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Juts be aware that you can't do all things in a trigger condition like PauseGame.
 

Matrixboy

New Member
Reaction score
1
>Now, I've seen that somewhere before, I asked for help with a small trigger I was making, and something acting as an action was being put in the condition, and it was giving off a false. Would you be able to explain why you return it as false? I always wondered that.

Trigger:
- Event
- Conditions
- Actions

If we put all the "actions" code into the conditions, and have the condition then return false, the real Actions (which will be blank) will never be run. Efficient. :)

>started using TimerUtils. But, after reading your paper, I JUST MIGHT have to start using Key Timers 1 (or maybe 2, I need to look up the specific pros/cons of each)

Key Timers 1 isn't publicly available as such... I'd just recommend Key Timers 2. Key Timers 2 is the best system without being hard to use for low periods, TimerUtils is better than KT2 for high periods (low and high being, lets say, <0.3 seconds is low, >0.3 seconds is high, so for spells where something is done several times a second you use KT2, but for waits and such TU is more efficient if you like).

Personally I just use KT2. Each to their own, see what you like. Both are valid.


Ah, I get it. So it doesn't think of running any actions! Thank you for the explination.

And I'll then take a look at KeyTimers 2. I've been using TimerUtils for a while, but only because it was the first name I heard. I'll see whatever works better for me. Thanks :D
 
General chit-chat
Help Users
  • The Helper The Helper:
    Actually I was just playing with having some kind of mention of the food forum and recipes on the main page to test and see if it would engage some of those people to post something. It is just weird to get so much traffic and no engagement
  • The Helper The Helper:
    So what it really is me trying to implement some kind of better site navigation not change the whole theme of the site
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top