Jump to content

Display messages after others


'LinKin

Recommended Posts

How would you solve this situation:

When you kill someone, a message is shown in ur screen "You killed Sam1ler" and it dissapears after 3 seconds, but, for example, you kill someone, and then after 1 second u kill another one, so the both messages would be shown in ur screen, therefore it'll look bad.

How can you do it so that only after the previous message was displayed, the new one comes up?

Link to comment

Are you sure you only want one message to stay on screen and then disappear after 3 seconds? I would suggest you to use a table and add an offset value ( Preferably y ) for the messages along with setTimer to remove the previous message(s) after a certain period of time. This way you will be able to see more than one message at a time. I can paste a sample code and help you in this regard if need be. :)

Link to comment

I once made a script which gets the Name of the Player died and renders it for 5 sec.

server side

  
addEventHandler("onPlayerWasted", getRootElement(),  -- This is an  Event "onPlayerWasted" 
function (  ) -- We don't need to name the function because we're inlcuding it in the event ( brackets " ) ") 
local NubName = getPlayerName (source) -- get the player name who died. 
local Name2 = "Test" 
    triggerClientEvent ( source, "NameOfnabDied", source, NubName, Name2 ) -- we attach it to source ( the player who died and transfer the data now ( player name  ) 
end  
) -- close the bracket here  

In this script text is visible for the Player who died only.

client side

  
HisName = "" -- make a variable and define as empty 
 HisName2 = "" -- another one 
addEvent ("NameOfnabDied", true ) -- add a new event. 
addEventHandler("NameOfnabDied", getRootElement(), -- Add the new event handler 
  
function ( NubName, Name2) -- add the context so you can receive the data. 
  
-- since we want to make it visible on the screen we'll use dxDrawText for this purpose. 
  
 HisName = NubName.." has died." -- We are doing this because we want the Player Name to exist global 
 Name2 = HisName2.." has died." 
    addEventHandler("onClientRender", root, visibletext ) -- make the text visible with this 
    setTimer ( -- We'll use a timer to stop rendering the text 
        function ( ) 
            removeEventHandler("onClientRender", root, visibletext ) -- make the text invisible by removing the event handler 
        end 
        ,5000, 1 -- This  will remove the text after 5 seconds 
    ) -- close the bracket for setTimer 
end  
 ) -- close the bracket for the event handler 
  
 local w, h = guiGetScreenSize ( ) -- get the screen size 
 local dx1 = false 
function visibletext ( )  
   dxDrawText (HisName , w * 0.375, h * 0.25, w * 0, h * 0, tocolor ( 255, 255, 255, 195 ), 1.0, "bankgothic", "left", "top", false, false, false, true ) 
dxDrawText (HisName2 , w * 0.375, h * 0.25, w * 0, h * 0, tocolor ( 255, 255, 255, 195 ), 1.0, "bankgothic", "left", "top", false, false, false, true ) 
-- arrange height and width, right now HisName2 variable Is empty so it will display nothing until You trigger the event then this will render the text. You must arrange dxdraw sizes as many times You want to render the text and add différent évents so you don't define the first variable again and again. 
end 

I'm on from mob so can't help you right now.

P.S if You want the text visible for everyone then replace source with root in triggerClientEvent.

Link to comment

Please, do not use a timer for the second text, that's just a waste of good functions that are made for a reason. Use getTickCount instead, and make a table for your texts.

local final_wait_time = 5000 -- Amount of ticks the last message should be displayed for 
local phrases = { 
    { 0, "The sun is shining right now" }, 
    { 5000, "And so I think it'll be a great day today" } 
} 
  
local visible, tick = false 
local function toggleRendering( ) 
    tick = getTickCount( ) 
    local fn = ( visible and removeEventHandler or addEventHandler ) 
    fn( "onClientRender", root, renderDisplay ) 
    visible = not visible 
end 
  
local screen_width, screen_height = guiGetScreenSize( ) 
function renderDisplay( ) 
    if ( not visible ) or ( not phrases ) or ( #phrases == 0 ) then return end 
    for index,data in ipairs( phrases ) do 
        if ( getTickCount( ) - tick >= data[ 1 ] ) and ( not phrases[ index + 1 ] and ( getTickCount( ) - tick < phrases[ index ][ 1 ] + final_wait_time ) or ( phrases[ index + 1 ] and getTickCount( ) - tick < phrases[ index + 1 ][ 1 ] ) ) then 
            local text_width, text_height = dxGetTextWidth( data[ 2 ] ), dxGetFontHeight( ) 
            dxDrawRectangle( ( screen_width - text_width ) / 2 - 15, ( screen_height - text_height ) / 2 - 15, text_width + 30, text_height + 30, tocolor( 0, 0, 0, 200 ) ) 
            dxDrawText( data[ 2 ], ( screen_width - text_width ) / 2, ( screen_height - text_height ) / 2, ( screen_width - text_width ) / 2 + text_width, ( screen_height - text_height ) / 2 + text_height, tocolor( 255, 255, 255, 255 ) ) 
        end 
    end 
end 
  
addEventHandler( "onClientResourceStart", resourceRoot, 
    function( ) 
        toggleRendering( ) 
    end 
) 

Edited by Guest
Link to comment

Thanks for the help but still...

Pretend you kill a player. So a message is shown in ur screen durring 10 seconds. And then it dissapears. All fine so far.

Now, pretend that you kill a player, then, the message is shown in your screen and the 10 secs countdown to remove it begins, AND then, after 5 seconds you kill another player, but this message will not be shown but untill the last message dissapears, which is within 5 seconds.

Link to comment

I tried to add a 3rd line, and it didn't work properly. I added { 10000, "3rd text" } into the table. But then that text lasted 10 seconds to dissapear... It's supposed it lasts 5 secs no?

Also, I don't understand that code very well

Link to comment
  • Moderators

Server:

--Server 
function onPlayerKilled( killer ) 
    triggerClientEvent("onPlayerKilled", source, killer ) 
end 
addEventHandler("onPlayerWasted", root, onPlayerKilled) 

Client:

-- Client 
local screen_width, screen_height = guiGetScreenSize() 
local killMsgQueue = {} 
local killTimeDisplay = 5000 --5 secs 
  
addEvent("onPlayerKilled", true) 
function addKillEntry( killer ) 
    if not source or not killer or not killMsgQueue then return end 
    local msg = tostring(getPlayerName(source)).." has been killed by "..tostring(getPlayerName(killer)) 
    table.insert(killMsgQueue, msg) 
end 
addEventHandler("onPlayerKilled", root, addKillEntry) 
  
function popFirst() 
    local tmp, killMsgQueue = killMsgQueue, {} 
    for k, i in ipairs(tmp) do 
        if k > 1 then table.insert(killMsgQueue, i) end 
    end 
end 
  
local tick, currentMsgTime = getTickCount(), 0 
function renderDisplay( ) 
    if not killMsgQueue or #killMsgQueue == 0 then return end 
  
    currentMsgTime = currentMsgTime + getTickCount() - tick 
    tick = getTickCount() 
  
    if currentMsgTime <= killTimeDisplay then 
        local msg = killMsgQueue[1] 
        local text_width, text_height = dxGetTextWidth( msg ), dxGetFontHeight() 
        dxDrawRectangle( ( screen_width - text_width ) / 2 - 15, ( screen_height - text_height ) / 2 - 15, text_width + 30, text_height + 30, tocolor( 0, 0, 0, 200 ) ) 
        dxDrawText( msg, ( screen_width - text_width ) / 2, ( screen_height - text_height ) / 2, ( screen_width - text_width ) / 2 + text_width, ( screen_height - text_height ) / 2 + text_height, tocolor( 255, 255, 255, 255 ) ) 
    else 
        popFirst() 
        currentMsgTime = 0 
    end 
end 

Link to comment

I suppose I got out of logic in the morning. At the moment it's waiting double the amount it starts from, which doesn't really work out. Change the * 2 part to + 5000, that should fix the issue.

@Lin: All you have to do is edit the code to reset such stuff. That was just an example script, and I find no reason to use timers in this system.

Link to comment
What is the problem with Timers?

Usually when handling massive amount of data and delaying it, it'd be better to use timers, as timers are on the main application level within the cpp files. Tick count on the other hand goes through the Lua VM, and so that is inefficient for massive data and repeated usage.

However, as we are using small data and not necessarily repeating it endlessly, I'd stick with tick count as it's also easier to modify in a way.

I am not totally "banning" timer usage on this, but I'd highly recommend using ticks, as I find it just way more logical for a small script. That's up to you and your usage though...

Link to comment

So it works or not? :)

And it's fine to use timers, don't get me wrong. It's just about where and how you plan on using them. It's like having two coffee machines, one is very fast, but makes bad coffee, while the other one is slow and makes good coffee; depends on where you plan on using them, so usually at work they might buy a cheap and fast machine to make loads of coffee though bad taste, and at home they might do the opposite. Not the best example really, but you know.

Using getTickCount is quite logical after all. At first back a long time ago I thought it's just not possible to understand, but later found out why: because it's too easy to understand, and ever since I've used often in smaller scripts.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...