Jump to content

[Question]Notification


Dimos7

Recommended Posts

  • Administrators

This can be done with some basic math.

Let's say we're positioning the notifications from 0,0 on the client's screen (top left)

--Notification values
local notificationBaseX, notificationBaseY = 0, 0
local notificationWidth, notificationHeight = 200, 60
local notificationMargin = 7

local notifications = {} --Our table where the notifications are stored.

function createNotification(text) --Example function to create a notification
  table.insert(notifications, text)
end

function renderNotifications() --Draw the notifications
  for i, notificationText in ipairs(notifications) do
    local notificationX, notificationY = notificationBaseX + notificationMargin, notificationBaseY + (notificationHeight + notificationMargin) * (i - 1) --Add the notification height & margin, then multiply that by the current notification index. We'll start indexing at 0 by removing 1 from the current index.
    dxDrawRectangle(notificationX, notificationY, notificationWidth, notificationHeight, tocolor(255, 0, 0))
    dxDrawText(notificationText, notificationX, notificationY, notificationX + notificationWidth, notificationY + notificationHeight, tocolor(255,255,255), 1, "default-bold", "center", "center")
  end
end
addEventHandler("onClientRender", root, renderNotifications)

--Create the notifications
createNotification("Notification 1")
createNotification("Notification 2")
createNotification("Notification 3")

I hope that's enough to get you started. Any questions just ask :) 

Link to comment
local sw, sh = guiGetScreenSize()
local sx, sy = sw/1920, sh/1080
local notificationBaseX, notificationBaseY = (sw/677)/2, 40
local notificationWidth, notificationHeight = 677, 40
local notificationMargin = 7

function sendClientMessage(message, r, g, b, colorCoded)
    if type(message) == "string" then
	   if not colorCoded then
	       r, g, b = tonumber (r) , tonumber(g) , tonumber(b)
		   colorCoded = false
	   else
	       r, g, b = getColorFromString(message)
		   colorCoded = true
	   end
	   setTimer(function()
	      removeEventHandler("onClientRender", root, renderNotifications)
	   end, 15000, 1)
	end
end
addEvent("sendClientMessage", true)
addEventHandler("sendClientMessage", root, sendClientMessage)



function renderNotifications()
    local notificationX, notificationY = notificationBaseX +notificationMargin ,  notificationBaseY+(notificationHeight +notificationMargin)*1
    dxDrawRectangle(sx*notificationX, sy*notificationY, sx*notificationWidth, sy*notificationHeight, tocolor(0, 0, 0, 255))
	dxDrawText(message, sx*notificationX, sy*notificationY, sx*notificationX+notificationWidth, sy*notificationY+notificationHeight, tocolor(r, g, b, 255), sx*1.2, "default-blod", "left", "top", false, false, false, colorCoded, false)
end
addEventHandler("onClientRender", root, renderNotifications)

i did something like this but i can't take message because i take warning in drawText is nil i want to take the message from that function can you help me?

Link to comment

You need to store 'message' somewhere if you want to use it in renderNotification. In the following code, I've placed it into notification[1] as well as r, g, b and colorCoded in the subsequent indices.

local sw, sh = guiGetScreenSize()
local sx, sy = sw/1920, sh/1080
local notificationBaseX, notificationBaseY = (sw/677)/2, 40
local notificationWidth, notificationHeight = 677, 40
local notificationMargin = 7
local notification -- current notification data storage

function sendClientMessage(message, r, g, b, colorCoded)
  if type(message) == "string" then
    if not colorCoded then
      r, g, b = tonumber (r) , tonumber(g) , tonumber(b)
      colorCoded = false
    else
      r, g, b = getColorFromString(message)
      colorCoded = true
    end
    
    notification = {message, r, g, b, colorCoded} -- set current notification data
    setTimer(function()
        notification = nil -- reset current notification data after 15000ms
        -- but don't remove event handler as that will prevent further notifcations showing up
        -- unless you re-attach the handler every time a new notifcation comes
      end, 15000, 1)
  end
end
addEvent("sendClientMessage", true)
addEventHandler("sendClientMessage", root, sendClientMessage)


function renderNotifications()
  if notification then -- if there is any current notication (data)
    -- collect notification data
    local message = notification[1]
    local r, g, b = notification[2], notification[3], notification[4]
    local colorCoded = notification[5]
    
    local notificationX, notificationY = notificationBaseX +notificationMargin, notificationBaseY+(notificationHeight +notificationMargin)*1
    dxDrawRectangle(sx*notificationX, sy*notificationY, sx*notificationWidth, sy*notificationHeight, tocolor(0, 0, 0, 255))
    dxDrawText(message, sx*notificationX, sy*notificationY, sx*notificationX+notificationWidth, sy*notificationY+notificationHeight, tocolor(r, g, b, 255), sx*1.2, "default-blod", "left", "top", false, false, false, colorCoded, false)
  end
end
addEventHandler("onClientRender", root, renderNotifications)

The way you modified the code allows only for one message to render at any time. If this is not what you intended, you should try to more closely mirror what LopSided gave you.

Edited by MrTasty
Link to comment

i fix it that but not it in middle of screen as i wanted it idk why

local sw, sh = guiGetScreenSize()
local sx, sy = sw/1920, sh/1080


function sendClientMessage(message, r, g, b, colorCoded)
    if type(message) == "string" then
	   if not colorCoded then
	       r, g, b = tonumber (r) , tonumber(g) , tonumber(b)
		   colorCoded = false
	   else
	       r, g, b = getColorFromString(message)
		   colorCoded = true
	   end
	   notification = {message, r, g, b, colorCoded}
	   setTimer(function()
	      notification = nil
	   end, 15000, 1)
	end
end
addEvent("sendClientMessage", true)
addEventHandler("sendClientMessage", root, sendClientMessage)



function renderNotifications()
    if notification then 
	   local notificationText = notification[1]
	   local r, g, b = notification[2], notification[3], notification[4]
       local colorCoded = notification[5]		
       dxDrawRectangle((sw/530)/2, sy*32, sx*530, sy*32, tocolor(0, 0, 0, 255))
 	   dxDrawText(notificationText, (sw/530)/2, sy*44, sx*530, sy*44, tocolor(r, g, b, 255), sx*1.2, "default-blod", "center", "center", false, false, false, colorCoded, false)
	end   
end
addEventHandler("onClientRender", root, renderNotifications)

 

Edited by Dimos7
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...