HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Crazy idea

08-24-2009, 10:51 AM#1
Maxus
I have a large trigger like this:
Collapse JASS:
globals
    boolean bool = false
endglobals

function Trig_trig_Actions takes nothing returns nothing
    // bla bla bla
    call TriggerSleepAction( 5 )
    if ( bool == true ) then
        return
    endif
    // bla bla bla
    call TriggerSleepAction( 5 )
    if ( bool == true ) then
        return
    endif
    // bla bla bla
endfunction

//===========================================================================
function InitTrig_trig takes nothing returns nothing
    set gg_trg_trig = CreateTrigger(  )
    call TriggerAddAction( gg_trg_trig, function Trig_trig_Actions )
endfunction

And there's many more of those ifs, I want to be able to set the global bool to true from outside which would interrupt the trigger (like skipping cinematics).

I'm just tired of writing all those ifs, so I can do:
Collapse JASS:
globals
    boolean bool = false
endglobals

function Trig_trig_Actions takes nothing returns nothing
    loop
        // bla bla bla
        call TriggerSleepAction( 5 )
        exitwhen ( bool == true )
        // bla bla bla
        call TriggerSleepAction( 5 )
        exitwhen ( bool == true )
        // bla bla bla
        exitwhen ( true )
    endloop
endfunction

//===========================================================================
function InitTrig_trig takes nothing returns nothing
    set gg_trg_trig = CreateTrigger(  )
    call TriggerAddAction( gg_trg_trig, function Trig_trig_Actions )
endfunction

Where instead of 3 lines of if there is one line of exitwhen which has the same effect, the problem is that is has to be in a loop - which is actually no loop because it always exits before repeating, and also exits when bool is set to true.

It's just the fact that using too many ifs makes the code too messy, this seems to be nicer.

I know it may be totally crazy, but please tell me what you think about it. I know I can use //! textmacro for that, but I just want to know if this is also possible or is it buggier, slower or actually mad??
08-24-2009, 11:05 AM#2
uberfoop
To avoid the loop for cleaner code, another way to do it in some cases would be to call a function that just kills the thread. Something more or less along the lines of:
Collapse JASS:
function BoolCheck takes nothing returns nothing
  if bool == true then
    call R2S(1/0)
  endif
endfunction


Although, exitwhen probably takes a little less processing power, esp for large blocks.
This stuff takes so little comp power anyway though that it probably won't matter that much performance-wise what you do. Go with what suites your purposes the best.
08-24-2009, 11:10 AM#3
Anitarf
The easiest way would be to use some sort of a system for scripting actions, especially if what you're doing are cinematics.
08-24-2009, 12:23 PM#4
Themerion
Something like this?

(Uses a timer instead of waits, in case the map is multi-player. Waits have complications with the Waiting-For-Players dialog).

Collapse JASS:
library YourNameHere initializer Init

// -----------------------------------------------------------
// Use the function Add to set up the cinematic:
//
// call YourNameHere_Add( Afunction, 5 )
// call YourNameHere_Add( AnotherFunction, 3 )
//
// Will call Afunction after 5 seconds, and AnotherFunction after 3 more.

// -----------------------------------------------------------
// Use the variable wantEnd to terminate the cinematic:
//
// set YourNameHere_wantEnd = true
// -----------------------------------------------------------

globals
    public boolean wantEnd=false

    private timer t
    private integer N=0
    private integer pos=0
    private real array wait
    private CinFunc array func
    private boolean running=false
endglobals

function interface CinFunc takes nothing returns nothing

private function Loop takes nothing returns nothing
    if wantEnd==false then
        call func[pos].execute()
        set pos=pos+1
    endif

    if pos>=N or wantEnd==true then
        set wantEnd=false
        set pos=0
        set N=0
        set running=false
        call PauseTimer(t)
    else
        call TimerStart(t,wait[pos],false,function Loop)
    endif
endfunction

public function Add takes CinFunc f, real waitTime returns nothing
    set wait[N]=waitTime
    set func[N]=f
    set N=N+1

    if running==false
        set running=true
        call TimerStart(t,waitTime,false,function Loop)
    endif
endfunction

private function Init takes nothing returns nothing
    set t=CreateTimer()
endfunction

endlibrary
08-24-2009, 02:25 PM#5
Maxus
It's not a multiplayer, it's singleplayer and I would use it in cinematics mainly.
I just wanted to make the most efficient way of terminating the trigger like return, not using ifs (it seems to be messy if used so many times in cinematics, the code is overloaded with ifs and it distracts me from real code - that's the only reason otherwise it's is fine) and the loop idea seemed to solve that.
I also like uberfoop's idea call R2S(1/0), but I don't know whether it's some kind of a bug or it's usual way of killing threads ;-)
08-24-2009, 03:57 PM#6
Troll-Brain
Collapse JASS:
//! textmacro Skip takes BOOLEAN
if $BOOLEAN$ then
     return
endif
//! endtextmacro
08-24-2009, 04:11 PM#7
Themerion
If it's not multi-player, the most simple solution you can use is the textmacros in combination with Waits.

Efficiency doesn't matter. We're talking about reading a boolean value (once, in between waits :P).

Collapse JASS:
//! textmacro Wait takes TIME
call TriggerSleepAction($TIME$)
if bool==true then
    return
endif
//! endtextmacro

It will reduce your 2-line loop-solution ( Sleep + exitwhen ) into 1 line.