AddRemoteEvent: Difference between revisions

From Onset Developer Wiki
No edit summary
No edit summary
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Info|Function|Server & Client|1.0}}
{{Info|Function|Server & Client|1.0}}


{{FuncDescription|The remote event to call from either server to client or vice versa.}}
{{FuncDescription|Register a remote event that can then be called from server to client or vice versa using [[CallRemoteEvent]]}}


{{FuncSyntax|AddRemoteEvent(player, RemoteEventName, LuaFunction)}}
{{FuncSyntax|AddRemoteEvent(RemoteEventName, LuaFunction)}}
{{FuncSyntaxClient|AddRemoteEvent(RemoteEventName, LuaFunction)}}
{{FuncSyntaxClient|AddRemoteEvent(RemoteEventName, LuaFunction)}}


{{FuncParameters}}
{{FuncParameters}}
{{FuncParam|player|The player identifier.}}
{{FuncParam|RemoteEventName|The remote event name.}}
{{FuncParam|RemoteEventName|The remote event name.}}
{{FuncParam|LuaFunction|The lua function.}}
{{FuncParam|LuaFunction|The lua function you want to call. Values you return from this function won't be transmitted to the originating CallRemoteEvent.}}


{{FuncReturnValue|This function has no specific return value.}}
{{FuncReturnValue|This function returns '''true''' on success.}}


== Example ==
== Example ==
<syntaxhighlight lang="Lua">
<syntaxhighlight lang="Lua">
-- This example calls the remote event from the server side to the client side
-- Client side
-- Client side
AddRemoteEvent("SetWeather", function(weatherid)
function ClientSetTime(time)
     SetWeather(weatherid)
SetTime(time)
end
AddRemoteEvent("ClientSetTime", ClientSetTime)
 
-- Server side
function cmd_time(player, player_time)
if (player_time == nil) then
return AddPlayerChat(player, "Usage: /time <time 0.0-24.0>")
end
player_time = tonumber(player_time)
 
if (player_time == nil or player_time < 0 or player_time > 24) then
return AddPlayerChat(player, "Parameter \"time\" 0.0-24.0")
end
 
CallRemoteEvent(player, "ClientSetTime", player_time)
end
AddCommand("time", cmd_time)
</syntaxhighlight>
 
<syntaxhighlight lang="Lua">
-- This example calls the remote event from the client side to the server side
-- Client side
AddEvent("OnKeyPress", function(key)
     if key == 'R' then
        CallRemoteEvent("OnClientRadio")
    end
end)
end)


-- Server side
-- Server side
AddCommand("we", function(playerid, weatherid)
AddRemoteEvent("OnClientRadio", function(playerid)
     CallRemoteEvent(playerid, "SetWeather", weatherid)
     AddPlayerChat(playerid, "Client activated radio")
end)
end)
</syntaxhighlight>
</syntaxhighlight>
Line 29: Line 56:
* [[CallEvent]]
* [[CallEvent]]
* [[CallRemoteEvent]]
* [[CallRemoteEvent]]
* [[BroadcastRemoteEvent]]
* [[AddEvent]]
* [[AddRemoteEvent]]
* [[AddRemoteEvent]]

Latest revision as of 17:07, 30 December 2020

AddRemoteEvent

Type: Function
Context: Server & Client
Introduced: v1.0

Description

Register a remote event that can then be called from server to client or vice versa using CallRemoteEvent

Syntax

AddRemoteEvent(RemoteEventName, LuaFunction)
Client Syntax
AddRemoteEvent(RemoteEventName, LuaFunction)

Parameters

  • RemoteEventName
    The remote event name.
  • LuaFunction
    The lua function you want to call. Values you return from this function won't be transmitted to the originating CallRemoteEvent.

Return Value

  • This function returns true on success.

Example

-- This example calls the remote event from the server side to the client side
-- Client side
function ClientSetTime(time)
	SetTime(time)
end
AddRemoteEvent("ClientSetTime", ClientSetTime)

-- Server side
function cmd_time(player, player_time)
	if (player_time == nil) then
		return AddPlayerChat(player, "Usage: /time <time 0.0-24.0>")
	end
	
	player_time = tonumber(player_time)

	if (player_time == nil or player_time < 0 or player_time > 24) then
		return AddPlayerChat(player, "Parameter \"time\" 0.0-24.0")
	end

	CallRemoteEvent(player, "ClientSetTime", player_time)
end
AddCommand("time", cmd_time)
-- This example calls the remote event from the client side to the server side
-- Client side
AddEvent("OnKeyPress", function(key)
    if key == 'R' then
        CallRemoteEvent("OnClientRadio")
    end
end)

-- Server side
AddRemoteEvent("OnClientRadio", function(playerid)
    AddPlayerChat(playerid, "Client activated radio")
end)

See also