• 🏆 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!

Basics - Hashtable - 2

Status
Not open for further replies.
Level 2
Joined
Jul 12, 2018
Messages
17

Tasks

Code

Changes



Create a (JASS) trigger that runs (only) when Hero makes a point order.
Create a (JASS) trigger that runs (only) when Hero makes a target order.
Create a (JASS) trigger that runs (only) when Hero makes an order without target.

Create a (JASS) trigger that runs when you select the Hero.
The trigger's goal is to print following values on screen:
  1. PointOrders: X
  2. TargetOrders: Y
  3. NontargetOrders: Z
... where, X, Y, Z are the correct values respectivly how much orders the Hero did (like counters).

Tip: You must use the first three triggers to count the different orders, and use hashtable to bind the values to the unit.


Create a (JASS) trigger that runs when a player presses the 'Esc' button.
The trigger's goal is to clean the hashtable of all it's entries.



JASS:
scope Part1n2 initializer init
    globals
        private key point
        private key target
        private key nontarget
   
        private unit Hero
        private hashtable Hash = InitHashtable()
    endglobals
    private function updateRec takes nothing returns nothing
        call ClearTextMessages()
        call BJDebugMsg("PointOrders: " + I2S(LoadInteger(Hash, point, 0)) + "\nTargetOrders: " + I2S(LoadInteger(Hash, target, 0)) + "\nNonTargetOrders: " + I2S(LoadInteger(Hash, nontarget, 0)))
    endfunction
   
    private function issuePoint takes nothing returns nothing
        call SaveInteger(Hash, point, 0, LoadInteger(Hash, point, 0) + 1)
    endfunction
   
    private function issueTarget takes nothing returns nothing
        call SaveInteger(Hash, target, 0, LoadInteger(Hash, target, 0) + 1)
    endfunction
   
    private function issueNonTarget takes nothing returns nothing
        call SaveInteger(Hash, nontarget, 0, LoadInteger(Hash, nontarget, 0) + 1)
    endfunction
   
    private function select takes nothing returns nothing
        call updateRec()
    endfunction
   
    private function esc takes nothing returns nothing
        call FlushChildHashtable(Hash, point)
        call FlushChildHashtable(Hash, target)
        call FlushChildHashtable(Hash, nontarget)
    endfunction
   
    private function isHero takes nothing returns boolean
        return GetFilterUnit() == Hero
    endfunction
   
    private function init takes nothing returns nothing
        local trigger pointTrigger = CreateTrigger()
        local trigger targetTrigger = CreateTrigger()
        local trigger instantTrigger = CreateTrigger()
        local trigger selectTrigger = CreateTrigger()
        local trigger escTrigger = CreateTrigger()
        local integer i = 0
       
        set Hero = CreateUnit(Player(0), 'Hpal', .0, .0, .0)
       
        loop
            exitwhen i >= bj_MAX_PLAYERS
            call TriggerRegisterPlayerUnitEvent(pointTrigger, Player(i), EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER, Condition(function isHero))
            call TriggerRegisterPlayerUnitEvent(targetTrigger, Player(i), EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, Condition(function isHero))
            call TriggerRegisterPlayerUnitEvent(instantTrigger, Player(i), EVENT_PLAYER_UNIT_ISSUED_ORDER, Condition(function isHero))
            call TriggerRegisterPlayerUnitEvent(selectTrigger, Player(i), EVENT_PLAYER_UNIT_SELECTED, Condition(function isHero))
            call TriggerRegisterPlayerEvent(escTrigger, Player(i), EVENT_PLAYER_END_CINEMATIC)
            set i = i + 1
        endloop
       
        call TriggerAddAction(pointTrigger, function issuePoint)
        call TriggerAddAction(targetTrigger, function issueTarget)
        call TriggerAddAction(instantTrigger, function issueNonTarget)
        call TriggerAddAction(selectTrigger, function select)
        call TriggerAddAction(escTrigger, function esc)
       
        call updateRec()
    endfunction
endscope



-submission


-instead integer, now uses key
-replaced FlushParentHashtable with FlushChildHashtable

 

Attachments

  • [Crash Course] Basics - Hashtable - 2.w3m
    24.6 KB · Views: 55
Last edited:

Jampion

Code Reviewer
Level 15
Joined
Mar 25, 2016
Messages
1,327
The important points regarding the tasks are all good, so I can only give some hints to optimize the code.

JASS:
    //point - p = 1
    //target - p = 2
    //nonTarget - p = 3
Keys can be useful here. A key is an integer variable that gets a unique integer assigned:
JASS:
globals
    private key point
    private key target
    private key nonTarget
endglobals
Then you can use the keys instead of numbers. Especially in bigger scripts this can reduce the amount of errors you make.
 
Level 2
Joined
Jul 12, 2018
Messages
17
That's new thing for me, thanks
So, here's the update

v1.1

-instead integer, now uses key
-replaced FlushParentHashtable with FlushChildHashtable
 
Status
Not open for further replies.
Top