• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

JASS Init Trig

Status
Not open for further replies.
Level 8
Joined
Jul 3, 2011
Messages
251
Hey guys, can someone please explain to me why we put things other than the event conditions and sometimes actions in the init trig? I understand why we do things like
JASS:
local trigger t = CreateTrigger( )
// event/con/action here
set t = null

But why do we sometimes do things like
JASS:
local trigger t = CreateTrigger( )
// event/con/action here
set udg_hash = InitHashtable()
set t = null

I know to use that line, but i dont actually know what it does, nor do i know why it must be in the init trig.

So to anyone who knows JASS, why do we use the init trig, what can we use it for and why do we use this trig for it? Thanks.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
InitTrig functions are created through the GUI of the trigger editor in WE. When the map is loading, the main function is started (which you do not see in normal WE). This one does some stuff and also runs those InitTrig functions on default. Point is, it triggers early, most-likely before any other event. Your blocks of code can make preparations for later use, like registering events, attach code to it or init any other data (though not everything engine-wise already works on MapInit).

It does not necessarily have to be in the InitTrig but imagine you need a hashtable and the code containing it fires at a time when the hashtable does not exist yet. In vJass or outside of standard WE, you can declare your own initializer on MapInit time, so it may be named other than "InitTrig", most only take "Init".
 
Level 8
Joined
Jul 3, 2011
Messages
251
So it is a function that fires at map init? Why cant we just create 1 function with map init event and set a global hashtable there? Why must it be done every time the function takes effect?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Yes, it fires a at map init. You can use a single function but usually the amount of data increases and you do not want to illegibly stuff everything at the same place, only what actually belongs there. This also helps modularity. For example, if you want to deactivate or delete certain parts of code, then you will have a greater independence and know what to do.

And there are some more reasons like the operation limit or code halting because of errors.

Why must it be done every time the function takes effect?

Each InitTrig function is run only once.
 
Level 8
Joined
Jul 3, 2011
Messages
251
So if a hashtable using the same name effects multiple functions, will each function have its own unique hashtable and wont be able to access data from the others? Or is data just stored in that particular function?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
No, you find the same thing under the same name. If you need this hashtable in various functions (various trigger leaves/code blocks), of course you should declare it at some superior place.

block A
block B
block C

Assume these 3 blocks all require the hashtable, then you would not monopolarize in one of these (e.g. in block A) because you might just disable/remove it at a later date and then block B and C do not work any longer.

You could either share the same snippet with all like

JASS:
if (table==null) then
    set table = InitHashtable()
endif

This way, the order and existence of other spots would not matter, the first firing sets the hare running or as a second option, you declare a block D containing the task, being required by the other blocks (runs before the other blocks take need of it). vJass offers structures that create init-queues, so blocks can require each other.

Ps: The variables you declare in GUI accumulate, too, and are only accessible because they get thrown at a point higher than the code calling them. Again, vJass permits more encapsulation and you are able to appoint your variables more tidily.
 
Status
Not open for further replies.
Top