• 🏆 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] Local Handle Variables vs Global Variables

Status
Not open for further replies.
Level 3
Joined
Dec 22, 2007
Messages
35
Hello, so I just recently learned how to use game caches to pass variables between triggers (NOT just functions within triggers, but seperate triggers - big difference) and I was wondering, are there any advantages besides multi instancability (I realize that is huge, but still) that game caching has over global variables? The reason I am asking is because it seems like game caching, from an optimization point of view, would take more processor time than just one simple global variable. I'm just wondering if I should ALWAYS go for game caching over global variables, or only in situations where I need mutli instancability.

Thanks!!
 
Level 11
Joined
Feb 18, 2004
Messages
394
H2I() returns an integer above the null handle index, which is something like 1 million. Arrays have a range of 0-8191. Its possible to store data at arrayVar[H2I(Whatever)], but then you have to keep the handle count under 8191. Array writes and reads are much faster than the game cache.

The best solution i've found is using vJASS structs alongside this system:
http://wc3campaigns.net/pastebint.php?t=93235&code=29e26baa170b05af2d3ffd04408ae7e1

There is another reason to use a solution such as this over the game cache, and that is avoiding I2H() casting. (Integer to Handle) Casting from integer to handle can cause some rather annoying bugs.
 
Level 5
Joined
Oct 27, 2007
Messages
158
Hello, so I just recently learned how to use game caches to pass variables between triggers (NOT just functions within triggers, but seperate triggers - big difference) and I was wondering, are there any advantages besides multi instancability (I realize that is huge, but still) that game caching has over global variables? The reason I am asking is because it seems like game caching, from an optimization point of view, would take more processor time than just one simple global variable. I'm just wondering if I should ALWAYS go for game caching over global variables, or only in situations where I need mutli instancability.

Thanks!!

If you use globals you need (could be done without but it wouldn't be 100% failsafe) to have a mutex (mutual exclusion) to deal with two trigger instances trying to alter the same global. Using an attachment system solves this problem for you. Every instance of a local is unique to that instance and can be accessed by any other trigger, if you attach it to something that is certain to be passed to that other function. You just have to be carefull where you attach data to. Units for example can be tricky because a unit can be removed, leaving you with a non valid data reference. If an index is reused you'd end up doing stuff to a different unit which is undesirable.
 
Status
Not open for further replies.
Top