• 🏆 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] Using uninitialized hashtable entries

Status
Not open for further replies.

Antares

Spell Reviewer
Level 22
Joined
Dec 13, 2009
Messages
528
Hello,

quick question. I want to put a cooldown on damage taken from a source by flipping a boolean in a hashtable at parentKey source and childKey target from false to true to store that the cooldown is active. The code surrounding it would look like this:

JASS:
	function Reset takes nothing returns nothing
		//Do stuff.
		call RemoveSavedBoolean( particleTable , whichParticle , playerNumber )
	endfunction

	function Damage takes nothing returns nothing
		if not LoadBoolean( particleTable , whichParticle , playerNumber ) then
			//Do damage stuff.
			call SaveBoolean( particleTable , whichParticle , playerNumber , true )
		endif
	endfunction

The important point is that I never actually save "false" at position (whichParticle , playerNumber). In good old JASS-fashion, you would expect that the thread crashes on the first line in Damage, but it doesn't and it returns false. This is actually great because it saves me spamming the hashtable with a lot of entries. So, my question is, is there any downside to doing it like this? Can you use hashtable entries in which nothing was written yet as false, 0, and/or null without any problems?
 

Antares

Spell Reviewer
Level 22
Joined
Dec 13, 2009
Messages
528
That's fine as far as I know, Booleans/Integers/Reals/Strings cannot be null and will always have a default starting value of False/0/0.00/"".

I've done this a few times and never noticed any issues so I doubt it's something to worry about.
Thanks, then I will not worry about it! But if I recall correctly, you can even load handles from the hashtable and if there's nothing saved it will return null instead of crash, so I don't think it's because it's one of the types you listed.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Back when hashtables were added you had to check the existence of a mapping. In your case you could use this test to check the value of the boolean since by it not being set it is false and by it existing it is true. Setting it to false would be done by removing the mapping rather than updating it to have a false value.

In the various patches since then I think they added enough sanity/safety checks that reading values from a not initialised mapping does not crash wc3 and instead returns a reasonable default value.
 
Status
Not open for further replies.
Top