14 years
edited 8 years
This will 'merge' DisplayTimedTextToPlayer calls in the same trigger-function-call-stack.
Feel free to comment.
1 /**********************************2 *3 * MessageQueue4 * v1.0.0.05 * By Magtheridon966 *7 * - Enqueues messages to display with8 * one call rather than having multiple9 * calls.10 *11 * - All messages will be displayed based 12 * on the longest duration given for any13 * message in one trigger function call14 * 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 array26 *27 * static method enqueue takes string message, real time returns nothing28 * static method insert takes string message, real time returns nothing29 * static method push takes string message, real time returns nothing30 * static method add takes string message, real time returns nothing31 * - Enqueues a message to be displayed.32 *33 * static method clear takes nothing returns nothing34 * - Clears the message queue.35 *36 * static method enable takes nothing returns nothing37 * static method disable takes nothing returns nothing38 * - When the system is disabled, incoming messages will be ignored.39 *40 **********************************/41 library MessageQueue42 43 globals44 private constant real DEFAULT_X = 0
45 private constant real DEFAULT_Y = 0
46 endglobals47
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 endmethod59
60 static method enable takes nothing returns nothing
61 set enabled = true
62 endmethod63 static method disable takes nothing returns nothing
64 set enabled = false
65 endmethod66
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 message72 * 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 values78 */79 set duration = time
80 set text = message
81 set count = 1
82 else83 /*84 * If the current message has been given a greater duration85 * 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 endif91
92 /*93 * We add the text to the queue with a line-break94 * to simulate multiple calls of DisplayTimedTextToPlayer.95 */96 set text = text + "\n" + message
97 set count = count + 1
98 endif99 endif100 endmethod101
102 static method push takes string message, real time returns nothing
103 call enqueue(message, time)
104 endmethod105
106 static method add takes string message, real time returns nothing
107 call enqueue(message, time)
108 endmethod109
110 static method insert takes string message, real time returns nothing
111 call enqueue(message, time)
112 endmethod113
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 endmethod123
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 endmethod130 endstruct131
132 endlibrary
Feel free to comment.