• 🏆 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!
  • ✅ The POLL for Hive's Texturing Contest #33 is OPEN! Vote for the TOP 3 SKINS! 🔗Click here to cast your vote!

[JASS] Basic stuff regarding variables and triggers

Status
Not open for further replies.
Level 17
Joined
Apr 13, 2008
Messages
1,602
So I'm writing a spell:

TLDR

The question is, how do I run a function such as this one:

JASS:
function ItemExpire takes item itvar, real sec returns nothing
    call TriggerSleepAction( sec )
    call SetItemLifeBJ( itvar, 0.00 )
endfunction

inside a trigger, without halting the trigger for (sec) seconds and without global variables.

I solved this particular problem with a global trigger and a global variable, but the spells I'm writing are more complex, and this wouldn't work.

Thanks.
 
Last edited:
Level 24
Joined
Aug 1, 2013
Messages
4,657
If you have TESH (I think... might be JASSHelper too) then you can click on a function name while holding ctrl to open that function in the function list...
Opening natives (pink ones) is not very helpfull but BJs (red ones) is.
When you open SetItemLifeBJ() then you see this:
JASS:
function SetItemLifeBJ takes widget whichWidget, real life returns nothing
    call SetWidgetLife(whichWidget, life)
endfunction
As you can see there is nothing that is done inside that function so you should use SetWidgetLife() instead.

If you encounter stuff like this:
JASS:
function PolarProjectionBJ takes location source, real dist, real angle returns location
    local real x = GetLocationX(source) + dist * Cos(angle * bj_DEGTORAD)
    local real y = GetLocationY(source) + dist * Sin(angle * bj_DEGTORAD)
    return Location(x, y)
endfunction
You can just stick to PolarProjectionBJ() instead of writing that code everytime to make the trigger more readable and easier to modify.

Timers use an extra function.
That function will be called when it expires.
JASS:
function ItemExpire_Callback takes nothing returns nothing
	local timer t = GetExpiredTimer()
	
	call DestroyTimer(t)
	set t = null
endfunction
This is an empty callback for a timer. This just removes the timer that expires.
You want to call this callback so you have to start a timer:
JASS:
function ItemExpire takes item whichItem, real seconds returns nothing
	local timer t = CreateTimer()
	call TimerStart(t, seconds, false, function ItemExpire_Callback)
	
	set t = null
endfunction
As you can see, it is not so hard to start one :D
Now comes the hashtable part.
You want to let the callback (which will destroy the item) to know what item must be destroyed.
So you use call SaveItemHandle(udg_Hashtable, GetHandleId(t), 0, whichItem) somewhere between "local timer t = CreateTimer()" and "set t = null".
This will save the item in the "udg_Hashtable" under the id of the timer. (which is the only reference that you can use)

Now you have to find out what the callback has to destroy.
The item you have is saved in LoadItemHandle(udg_Hashtable, GetHandleId(t), 0) which you can call somewhere between "local timer t = GetExpiredTimer()" and "call DestroyTimer(t)".
If you would use that item multiple times then you should save it in a variable.
But you will only use it once so we can just say call SetWidgetLife(LoadItemHandle(udg_Hashtable, GetHandleId(t), 0), 0.)

JASS:
function ItemExpire_Callback takes nothing returns nothing
	local timer t = GetExpiredTimer()
	
	call SetWidgetLife(LoadItemHandle(udg_Hashtable, GetHandleId(t), 0), 0.)
	
	call DestroyTimer(t)
	set t = null
endfunction

function ItemExpire takes item whichItem, real seconds returns nothing
	local timer t = CreateTimer()
	call TimerStart(t, seconds, false, function ItemExpire_Callback)
	
	call SaveItemHandle(udg_Hashtable, GetHandleId(t), 0, whichItem)
	
	set t = null
endfunction

(btw... "Post Quick Reply"... can someone change that into "Post Long Reply" when you type more than 500 chars? :D)
 
Status
Not open for further replies.
Top