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

[Snippet] MessageQueue

This will 'merge' DisplayTimedTextToPlayer calls in the same trigger-function-call-stack.
DisplayTimedTextToPlayer is, after all, a heavy function call.

JASS:
/**********************************
*
*   MessageQueue
*   v1.0.0.0
*   By Magtheridon96
*
*   - Enqueues messages to display with
*     one call rather than having multiple
*     calls.
*
*   - All messages will be displayed based 
*     on the longest duration given for any
*     message in one trigger function call
*     stack.
*
*       call MessageQueue.push("Hello", 2)
*       call MessageQueue.push("Okay", 4)
*
*   - Both of the above messages will be 
*     displayed for 4 seconds.
*
*   API:
*   ----
*
*       struct MessageQueue extends array
*
*           static method enqueue takes string message, real time returns nothing
*           static method insert takes string message, real time returns nothing
*           static method push takes string message, real time returns nothing
*           static method add takes string message, real time returns nothing
*               - Enqueues a message to be displayed.
*
*           static method clear takes nothing returns nothing
*               - Clears the message queue.
*
*           static method enable takes nothing returns nothing
*           static method disable takes nothing returns nothing
*               - When the system is disabled, incoming messages will be ignored.
*
**********************************/
library MessageQueue

    globals
        private constant real DEFAULT_X = 0
        private constant real DEFAULT_Y = 0
    endglobals
    
    struct MessageQueue extends array
        private static player localPlayer
        private static timer t = CreateTimer()
        private static boolean enabled = true
        private static integer count = 0
        private static real duration = 0
        private static string text = ""
        
        private static method display takes nothing returns nothing
            call DisplayTimedTextToPlayer(localPlayer, DEFAULT_X, DEFAULT_Y, duration, text)
        endmethod
        
        static method enable takes nothing returns nothing
            set enabled = true
        endmethod
        static method disable takes nothing returns nothing
            set enabled = false
        endmethod
        
        static method enqueue takes string message, real time returns nothing
            if enabled then
                if count == 0 then
                    /*
                    *   We have to start the timer since this is the first message
                    *   to be enqueued in the queue.
                    */
                    call TimerStart(t, 0, false, function thistype.display)
                    
                    /*
                    *   We can safely just set the values
                    */
                    set duration = time
                    set text = message
                    set count = 1
                else
                    /*
                    *   If the current message has been given a greater duration
                    *   than the one currently in use, then we set the current 
                    *   duration to this new one.
                    */
                    if time > duration then
                        set duration = time
                    endif
                    
                    /*
                    *   We add the text to the queue with a line-break
                    *   to simulate multiple calls of DisplayTimedTextToPlayer.
                    */
                    set text = text + "\n" + message
                    set count = count + 1
                endif
            endif
        endmethod
        
        static method push takes string message, real time returns nothing
            call enqueue(message, time)
        endmethod
        
        static method add takes string message, real time returns nothing
            call enqueue(message, time)
        endmethod
        
        static method insert takes string message, real time returns nothing
            call enqueue(message, time)
        endmethod
        
        static method clear takes nothing returns nothing
            /*
            *   This will reset the queue.
            */
            set text = ""
            set duration = 0
            set count = 0
            call PauseTimer(t)
        endmethod
        
        private static method onInit takes nothing returns nothing
            /*
            *   GetLocalPlayer() calls may cause crashes in global blocks.
            */
            set localPlayer = GetLocalPlayer()
        endmethod
    endstruct
    
endlibrary

Feel free to comment.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
So, even with all this overhead, one DisplayText with some big string is still more efficient than simply X DisplayText calls, and it is noticeable ?

I'm quite sceptic, since you're not supposed to spam text every 0.01 s or so.
Also i would assume that what is "costly" in this function, is not the calling function by itself, but to display the text.
So with one big string or several ones, that would be about the same (but my guess could be wrong)

Btw you could warn about the string size limitation, since you will reach it sooner with your stuff.
 
Level 10
Joined
May 27, 2009
Messages
494
let's say hero descriptions as an example :O
like those in hero selection systems o.o

or even total pwnage with some kill systems that will display lots and lots of "Noob pwned another noob because that noob is just a noob for xxx gold/lumber whatsoever"

but the chance is... 40-60

so this can be useful too lol
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
[blizzard troll]
JASS:
            /*
            *   GetLocalPlayer() calls may cause crashes in global blocks.
            */

->
JASS:
            /*
            *   GetLocalPlayer() actually cause crashes, and eventually will create other bugs if by some miracle a fix attempt is tried
            */
[/blizzard troll]

Fixed.

I still doubt about any script efficiency won with this, comparing to just use several DisplayText.
So in other words if it's really worth it (quite like my library LocalHelper in some way).
 
It's not all about efficiency, you can clear enqueued messages too :p
You can even disable all messages if you would use this instead of DTTTP.
And then you could enable them at your own will

This could be useful for users that want to easily modify libraries like Advanced Streak System.
If they want custom text in certain circumstances, they would register an Event, call clear(), display their text, then disable :D
 
Top