Warcraft III: MapsModelsSkinsIconsSpellsToolsTutorials
WC3 JASS: Code SnippetsJASS and vJASS Spells and SystemsJass Tutorials
Chat @Discord

Author Topic: [Snippet] MessageQueue  (Read 5376 times)

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *
  • Posts: 83
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 516
  • vJass Incarnate
    • View Profile
[Snippet] MessageQueue
« on: July 09, 2012, 01:31:44 PM »
This will 'merge' DisplayTimedTextToPlayer calls in the same trigger-function-call-stack.

Code: jass  [Select]
  1. /**********************************
  2. *
  3. *   MessageQueue
  4. *   v1.0.0.0
  5. *   By Magtheridon96
  6. *
  7. *   - Enqueues messages to display with
  8. *     one call rather than having multiple
  9. *     calls.
  10. *
  11. *   - All messages will be displayed based
  12. *     on the longest duration given for any
  13. *     message in one trigger function call
  14. *     stack.
  15. *
  16. *       call MessageQueue.push("Hello", 2)
  17. *       call MessageQueue.push("Okay", 4)
  18. *
  19. *   - Both of the above messages will be
  20. *     displayed for 4 seconds.
  21. *
  22. *   API:
  23. *   ----
  24. *
  25. *       struct MessageQueue extends array
  26. *
  27. *           static method enqueue takes string message, real time returns nothing
  28. *           static method insert takes string message, real time returns nothing
  29. *           static method push takes string message, real time returns nothing
  30. *           static method add takes string message, real time returns nothing
  31. *               - Enqueues a message to be displayed.
  32. *
  33. *           static method clear takes nothing returns nothing
  34. *               - Clears the message queue.
  35. *
  36. *           static method enable takes nothing returns nothing
  37. *           static method disable takes nothing returns nothing
  38. *               - When the system is disabled, incoming messages will be ignored.
  39. *
  40. **********************************/
  41. library MessageQueue
  42.  
  43.     globals
  44.         private constant real DEFAULT_X = 0
  45.         private constant real DEFAULT_Y = 0
  46.     endglobals
  47.    
  48.     struct MessageQueue extends array
  49.         private static player localPlayer
  50.         private static timer t = CreateTimer()
  51.         private static boolean enabled = true
  52.         private static integer count = 0
  53.         private static real duration = 0
  54.         private static string text = ""
  55.        
  56.         private static method display takes nothing returns nothing
  57.             call DisplayTimedTextToPlayer(localPlayer, DEFAULT_X, DEFAULT_Y, duration, text)
  58.         endmethod
  59.        
  60.         static method enable takes nothing returns nothing
  61.             set enabled = true
  62.         endmethod
  63.         static method disable takes nothing returns nothing
  64.             set enabled = false
  65.         endmethod
  66.        
  67.         static method enqueue takes string message, real time returns nothing
  68.             if enabled then
  69.                 if count == 0 then
  70.                     /*
  71.                     *   We have to start the timer since this is the first message
  72.                     *   to be enqueued in the queue.
  73.                     */
  74.                     call TimerStart(t, 0, false, function thistype.display)
  75.                    
  76.                     /*
  77.                     *   We can safely just set the values
  78.                     */
  79.                     set duration = time
  80.                     set text = message
  81.                     set count = 1
  82.                 else
  83.                     /*
  84.                     *   If the current message has been given a greater duration
  85.                     *   than the one currently in use, then we set the current
  86.                     *   duration to this new one.
  87.                     */
  88.                     if time > duration then
  89.                         set duration = time
  90.                     endif
  91.                    
  92.                     /*
  93.                     *   We add the text to the queue with a line-break
  94.                     *   to simulate multiple calls of DisplayTimedTextToPlayer.
  95.                     */
  96.                     set text = text + "\n" + message
  97.                     set count = count + 1
  98.                 endif
  99.             endif
  100.         endmethod
  101.        
  102.         static method push takes string message, real time returns nothing
  103.             call enqueue(message, time)
  104.         endmethod
  105.        
  106.         static method add takes string message, real time returns nothing
  107.             call enqueue(message, time)
  108.         endmethod
  109.        
  110.         static method insert takes string message, real time returns nothing
  111.             call enqueue(message, time)
  112.         endmethod
  113.        
  114.         static method clear takes nothing returns nothing
  115.             /*
  116.             *   This will reset the queue.
  117.             */
  118.             set text = ""
  119.             set duration = 0
  120.             set count = 0
  121.             call PauseTimer(t)
  122.         endmethod
  123.        
  124.         private static method onInit takes nothing returns nothing
  125.             /*
  126.             *   GetLocalPlayer() calls may cause crashes in global blocks.
  127.             */
  128.             set localPlayer = GetLocalPlayer()
  129.         endmethod
  130.     endstruct
  131.    
  132. endlibrary

Feel free to comment.

 

[Snippet] Group Units in line, with distance and optional spread-value

Started by rvonsonsnadtzBoard Codes & Snippets

Replies: 5
Views: 11935
Last post March 13, 2013, 06:52:16 PM
by rvonsonsnadtz
[Snippet] Single Player Detector

Started by moyackBoard Codes & Snippets

Replies: 12
Views: 21563
Last post March 13, 2013, 12:48:49 PM
by Magtheridon96
[Snippet] Simple Unit Indexer

Started by moyackBoard Codes & Snippets

Replies: 17
Views: 27029
Last post September 30, 2013, 08:07:19 AM
by moyack
[Snippet] Disable Transmission Skip

Started by PurgeandfireBoard Codes & Snippets

Replies: 2
Views: 9005
Last post July 02, 2013, 01:34:55 PM
by Purgeandfire
[Snippet] Timed Effects

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 7770
Last post January 09, 2012, 07:03:32 PM
by moyack
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!