• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] trigger issue

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,219
I had an idea of a 'new' way to create MUI stuff. I had some issues and messaged Edo about it. After a few fixes my WIP system seemed to work fine but Edo was claiming more than once that it shouldn't work at all due the GetTriggeringTrigger.

JASS:
globals
	hashtable hash = InitHashtable()
endglobals

function someFunc takes nothing returns nothing
	local trigger t = GetTriggeringTrigger()
	local integer h = GetHandleId(t)
	local integer counter = LoadInteger(hash, h, 3)
	local integer goal =  LoadInteger(hash, h, 2)
	if counter < goal then
		call TriggerExecute(LoadTriggerHandle(hash, h, 1))
		set counter = counter + 1
		call SaveInteger(hash, h, 3, counter)
	else
		//will this actually destroy the trigger?
		call BJDebugMsg("asd")
		call DestroyTrigger(LoadTriggerHandle(hash, h, 4))
		call FlushChildHashtable(hash, h)
	endif
endfunction

function applyTimer takes trigger t, integer interval, integer times returns nothing
	local trigger newTrigger = CreateTrigger()
	local timer tempTimer = CreateTimer()
	call SaveTriggerHandle(hash, GetHandleId(newTrigger), 1, t)
	call SaveInteger(hash, GetHandleId(newTrigger), 2, times)
	call SaveInteger(hash, GetHandleId(newTrigger), 3, 0)
	call SaveTriggerHandle(hash, GetHandleId(newTrigger), 4, newTrigger)
	call TriggerRegisterTimerExpireEvent(newTrigger, tempTimer )
	call TriggerAddAction(newTrigger, function someFunc )
	call TimerStart(tempTimer, interval, true, null)
endfunction
  • call
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set t = periodic <gen>
      • Custom script: call applyTimer(udg_t, 2, 3)
  • periodic
    • Events
    • Conditions
    • Actions
      • Game - Display to (All players) the text: derp
However, "derp" is printed 3 times and "asd" is printed once so it seems to work correctly at least.
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
This will work as you want, but this is a semi-horrible way of doing this.
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
I almost forgot, don't use triggeraction for dynamic triggers, it leaks when the trigger is destroyed, use triggercondition instead.
 

Deleted member 219079

D

Deleted member 219079

I dunno, but you could reduce the amount of native calls you make by using array index for each instance. It's one of the tips in my jass tutorial.

Well first of all, you never destroy the timer. Have you tested destroying the triggar via the local pointer?

edit: just as a cheerup, keep messing with jass, who says we've already invented everything we can make in jass? ;P
 

Deleted member 219079

D

Deleted member 219079

JASS:
    // globals
globals
    integer nextIndex=0
    integer maxIndex=0
    integer array indexArray
endglobals
    // get index
function getIndex takes nothing returns integer
 local integer this=nextIndex
    if (this!=0) then
        set nextIndex=indexArray[this]
    else
        set maxIndex=maxIndex+1
        set this=maxIndex
    endif
    set indexArray[this]=-1
    return this
endfunction
    // return index
function killIndex takes integer this returns nothing
    if this==null or indexArray[this]!=-1 then
        return
    endif
    set indexArray[this]=nextIndex
    set nextIndex=this
endfunction
^Here's how to make an object

JASS:
    // your own stuff
globals
    trigger array trig
    timer array tim
    hashtable hash = InitHashtable()
endglobals

    // example
function genObject takes trigger t, real interval, integer count returns nothing
    local integer object = getIndex()
    set trig[object] = t
    set tim[object] = CreateTimer()
        ...
endfunction
How to use the objects^

e: this is too taken from my jass tutorial, i've put my knowledge there..
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
JASS:
scope s initializer i
    
    private function w takes nothing returns nothing
        call BJDebugMsg(I2S(GetHandleId(GetTriggeringTrigger())))
    endfunction
    
    private function q takes nothing returns nothing
        local timer t = CreateTimer()
        call BJDebugMsg(I2S(GetHandleId(GetTriggeringTrigger())))
        call TimerStart(t, 0., false, function w)
    endfunction
    
    private function i takes nothing returns nothing
        local trigger t = CreateTrigger()
        
        call TriggerAddAction(t, function q)
        call TriggerExecute(t)
    endfunction
    
endscope

disprooves the fact that triggering trigger is set.

HOWEVER, Ive missed the fact that there is the TriggerRegisterTimerExpireEvent, which will fire the conditions and actions of trigger when the timer expires, which you start afterwards. This is equal to this code:

JASS:
scope s initializer i
    
    private function w takes nothing returns nothing
        call BJDebugMsg(I2S(GetHandleId(GetTriggeringTrigger())))
    endfunction
    
    private function i takes nothing returns nothing
        local trigger t = CreateTrigger()
        local timer tm = CreateTimer()
        
        call TriggerRegisterTimerExpireEvent(t, tm)
        call TriggerAddAction(t, function w)
        call TimerStart(tm, 0., false, null)
        
        call BJDebugMsg(I2S(GetHandleId(t)))
    endfunction
    
endscope

which will correctly print the same integer twice
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,219
Okay, I created a few MUI spells with close to no effort. The triggers are super short.

Something tells me it isn't good looking enough to make up for the spell making system x) Also ignore the sarcastic title :D

edit: I fixed the bug at the end, no need to comment on that!

edit2: Either way, you can consider this to be solved (mod can close or mark it as solved, W/E). If you want to talk about the system just send a VM or something.
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
chaosy said:
I never used conditions in jass. Basically just the same but it has to return a bool?

chobibo said:
I almost forgot, don't use triggeraction for dynamic triggers, it leaks when the trigger is destroyed, use trigger condition instead

It the accepted vJASS style to not use trigger actions but trigger conditions instead. Why?

The biggest reason is that each trigger action creates a new thread which runs the trigger. But, because WC3 is actually single-threaded, this doesn't improve performance or speed at all. In fact it just adds overhead and probably consumes more memory / CPU resources.

Just do all your computation in the condition--the same functions will work, and make sure it fits this template:

JASS:
function main takes nothing returns boolean
  //all your trigger code here
  …
  return false
endfunction
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
it doesnt create physical threads, it only requires a new execution from the virtual mashine, which is a lot quicker then spawning a real thread. Afaik, conditions spawn 1 "thread" per conditionfunc too, otherwise you would potentionally hit op limit. We should change the name to something less generic
 
Status
Not open for further replies.
Top