• 🏆 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!

[JASS] This thing about ifs and booleans

Status
Not open for further replies.
Level 10
Joined
Sep 3, 2009
Messages
458
So I have a question. But first let me show you two different codes that should do the same thing.


JASS:
        private static method Loop takes nothing returns nothing
            local integer i = 1

            loop
                exitwhen i > TOTAL
                set data = TRANS_ARRAY[i]
                if data.revert == false then
                  if data.ctime > 1.0 then
                    set data.ctime = data.ctime - TICK
                  else
                  if data.finish == false then
                    call DestroyEffect(AddSpecialEffect("", GetUnitX(data.master), GetUnitY(data.master)))
                    call BJDebugMsg("FINISHED!")
                 
                    call RemoveItem(UnitRemoveItemFromSlot( data.master, 0 ))
                    call UnitAddItemToSlotById( data.master, TRANS_ITEM[GetUnitPointValue(data.servant)], 0 )
                    call ShowUnit( data.servant, false)
                    
                    set data.finish = true
                  endif
                    
                  endif
                else
                
                  call RemoveItem(UnitRemoveItemFromSlot( data.master, 0 ))
                  call UnitAddItemToSlotById( data.master, TRANS_WEAPON, 0 )
                  call ShowUnit( data.servant, true)
                  
                  call SetUnitX( data.servant, GetUnitX(data.master))
                  call SetUnitY( data.servant, GetUnitY(data.master))
                  
                  set TRANS_ARRAY[i] = TRANS_ARRAY[TOTAL]
                  set TOTAL = TOTAL - 1
                  set i = i - 1
                  call data.destroy()
                
                endif
                            
                if TOTAL == 0 then
                   call PauseTimer(TRANS_TIMER)
                endif
              
                set i = i + 1
            endloop        
        endmethod



JASS:
        private static method Loop takes nothing returns nothing
            local integer i = 1

            loop
                exitwhen i > TOTAL
                set data = TRANS_ARRAY[i]
                if data.revert == false then
                  if data.ctime > 1.0 and data.finish == false then
                    set data.ctime = data.ctime - TICK
                  else

                    call DestroyEffect(AddSpecialEffect("", GetUnitX(data.master), GetUnitY(data.master)))
                    call BJDebugMsg("FINISHED!")
                 
                    call RemoveItem(UnitRemoveItemFromSlot( data.master, 0 ))
                    call UnitAddItemToSlotById( data.master, TRANS_ITEM[GetUnitPointValue(data.servant)], 0 )
                    call ShowUnit( data.servant, false)
                    
                    set data.finish = true

                    
                  endif
                else
                
                  call RemoveItem(UnitRemoveItemFromSlot( data.master, 0 ))
                  call UnitAddItemToSlotById( data.master, TRANS_WEAPON, 0 )
                  call ShowUnit( data.servant, true)
                  
                  call SetUnitX( data.servant, GetUnitX(data.master))
                  call SetUnitY( data.servant, GetUnitY(data.master))
                  
                  set TRANS_ARRAY[i] = TRANS_ARRAY[TOTAL]
                  set TOTAL = TOTAL - 1
                  set i = i - 1
                  call data.destroy()
                
                endif
                            
                if TOTAL == 0 then
                   call PauseTimer(TRANS_TIMER)
                endif
              
                set i = i + 1
            endloop        
        endmethod


Now in the first code I use

JASS:
                 if data.ctime > 1.0 then
                    set data.ctime = data.ctime - TICK
                  else
                  if data.finish == false then
                    call DestroyEffect(AddSpecialEffect("", GetUnitX(data.master), GetUnitY(data.master)))
                    call BJDebugMsg("FINISHED!")
                 
                    call RemoveItem(UnitRemoveItemFromSlot( data.master, 0 ))
                    call UnitAddItemToSlotById( data.master, TRANS_ITEM[GetUnitPointValue(data.servant)], 0 )
                    call ShowUnit( data.servant, false)
                    
                    set data.finish = true
                  endif
                    
                  endif

Basically I check if data.ctime > 1.0 and I check again if finish is == false.

What this does is that if data.ctime becomes less than 1.0 it runs the code where it checks if finish == false. If false it will run the code then set data.finish == true so that it will not run another time.

So this Code works but lest got to the second code.

In the second code

JASS:
                  if data.ctime > 1.0 and data.finish == false then
                    set data.ctime = data.ctime - TICK
                  else

                    call DestroyEffect(AddSpecialEffect("", GetUnitX(data.master), GetUnitY(data.master)))
                    call BJDebugMsg("FINISHED!")
                 
                    call RemoveItem(UnitRemoveItemFromSlot( data.master, 0 ))
                    call UnitAddItemToSlotById( data.master, TRANS_ITEM[GetUnitPointValue(data.servant)], 0 )
                    call ShowUnit( data.servant, false)
                    
                    set data.finish = true

                    
                  endif

Instead of making an additional if I just do if data.ctime > 1.0 and data.finish == false

What I don't understand is when I set data.finish == true, it still runs the code. In theory it should be working like the first code but it doesn't

Please enlighten me I'm still new with vJass/Jass in general. Please and thank you
 
Level 10
Joined
Sep 3, 2009
Messages
458
This part.
JASS:
call DestroyEffect(AddSpecialEffect("", GetUnitX(data.master), GetUnitY(data.master)))
  call BJDebugMsg("FINISHED!")
                 
  call RemoveItem(UnitRemoveItemFromSlot( data.master, 0 ))
  call UnitAddItemToSlotById( data.master, TRANS_ITEM[GetUnitPointValue(data.servant)], 0 )
  call ShowUnit( data.servant, false)
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
First code
JASS:
if data.ctime > 1.0 then
   set data.ctime = data.ctime - TICK
 else
 if data.finish == false then

First data.ctime becomes less than or equal to 1.0. Then data.finish becomes true. During the next loop the code checks:

data.ctime > 1.0 -> false, go to else
data.finish == false -> false, end of thread.

Second code
JASS:
if data.ctime > 1.0 and data.finish == false then
  set data.ctime = data.ctime - TICK
else

First the data.ctim becomes less than or equal to 1.0. Then data.finish becomes true. During the next loop the code checks

data.ctime > 1.0 -> false
data.finish == false -> false

AND condition checks whether all conditions are true or not, therefore -> false -> go to else.

In the first code you give three "paths" for the code to execute,

subtract from data.ctime
set data.finish = true
do nothing

In the second code, you give only two "paths"
subtract from data.ctime
set data.finish = true
 
Status
Not open for further replies.
Top