Chat and commands
The code on this page comes from the demo resource and is covered by the server's tests.
Plain chat
Every line a player types that is not a command raises player_chat. Unless a handler returns false, the server broadcasts the line to everyone. The demo limits chat to the sender's virtual world and replies to !ping privately:
-- Plain chat: "!ping" gets a private reply; any other line stays in the sender's
-- virtual world. Both cancel the default broadcast (return false).
dlmp.add_event_handler("player_chat", dlmp.root, function(player, text)
if text == "!ping" then
player:send_message("pong")
return false
end
local world = player:virtual_world()
local recipients = {}
for _, other in ipairs(dlmp.players()) do
if other:virtual_world() == world then
recipients[#recipients + 1] = other
end
end
player:send_chat(recipients, text)
return false
end)
-- A fresh, weaponless player materialized (connect or respawn): arm the starter
-- sidearm + armor. give_weapon lands while the ped is still materializing;
-- add_armor is additive. Weapon skills are gamemode-owned (the server floor is aPlayer:send_messageshows a server message to one player.Player:send_chatsends a chat line in the player's name to the recipients you choose.- Returning
falsecancels the event, which here suppresses the default broadcast. See Events for which events can be canceled and how multiple handlers combine.
Colored messages
A message is either a string or a list of runs, each with its own color:
dlmp.add_event_handler("player_connect", dlmp.root, function(player)
dlmp.show_announcement(player, { title = "DarqlineMP", subtitle = "Freeroam", duration_ms = 4000 })
player:send_message({
{ text = "Welcome to ", color = 0xFFFFFF },
{ text = "DarqlineMP", color = 0x6CB4FF },
{ text = ". Choose a city, or type " },
{ text = "/spawn [skinId] [in]", color = 0xFFC850 },
{ text = " (in = indoors)." },
})
city_menu(player)
end)dlmp.show_announcement shows a title in the center of the screen for a set time.
Commands with arguments
For /city sf, the handler receives the player and "sf". Arguments always arrive as strings: validate them before use, and reply with a usage line when one is invalid:
-- /city [ls|sf|lv] goes to that city, or opens the menu.
dlmp.add_command_handler("city", function(player, id)
local city = city_by_id(id)
if city then
go_to_city(player, city)
elseif id then
player:send_message("usage: /city [ls|sf|lv]")
else
city_menu(player)
end
return true
end)The handler returns true in every branch, so the demo handles /city whatever arguments follow it. Commands covers priorities, case sensitivity and console commands.
