Jump to content

Usage of CallClientFunction


Recommended Posts

  • Moderators
6 hours ago, Heshan_Shalinda_eUnlock said:

I want to know the usage of CallClientFunction to make my further scripts.

 

Step 1

You have to make sure that you have 1 clientside file and 1 serverside file.

The (client/server)side is defined by the meta.xml:

<script src="server.lua" /> <!-- serverside -->

<script src="client.lua" type="client"/> <!-- clientside-->

 

Step 2

Copy the code for the function:

https://wiki.multitheftauto.com/wiki/CallClientFunction

Add to server file.

Spoiler

 

function callClientFunction(client, funcname, ...)
    local arg = { ... }
    if (arg[1]) then
        for key, value in next, arg do
            if (type(value) == "number") then arg[key] = tostring(value) end
        end
    end
    -- If the clientside event handler is not in the same resource, replace 'resourceRoot' with the appropriate element
    triggerClientEvent(client, "onServerCallsClientFunction", resourceRoot, funcname, unpack(arg or {}))
end

 

Add to client file.

Spoiler
function callClientFunction(funcname, ...)
    local arg = { ... }
    if (arg[1]) then
        for key, value in next, arg do arg[key] = tonumber(value) or value end
    end
    loadstring("return "..funcname)()(unpack(arg))
end
addEvent("onServerCallsClientFunction", true)
addEventHandler("onServerCallsClientFunction", resourceRoot, callClientFunction)

 

 

Step 3

The example code:

Add to server file.

addCommandHandler("callFunction", 
function (player, cmd, functionName)
    
	callClientFunction(player, functionName)
    
end, false, false)

 

Add to client file.

function test1 ()
	outputChatBox("You called function test1 ")
end

function test2 ()
	outputChatBox("You called function test2 ")
end

function test3 ()
	outputChatBox("You called function test3 ")
end

 

 

Step 4

Now when use the /callFunction command in combination with a function name of your choice, it will call that client function on YOUR pc.

 

You can use the commands:

/callFunction test1

/callFunction test2

/callFunction test3

 

Or default functions created by MTA. The only problem is that most of those require some arguments. Which are currently not defined and passed to your client functions.

 

 

 

 

 

 

Link to comment
39 minutes ago, Heshan_Shalinda_eUnlock said:

Thank you for your help but I want to know why we use callClientFunction.

And I want to know if I can use it to an only client side function make visible to all the players in the server.

Can I bind this into gui button and make some client side function occur by clicking that gui button??

Thank you for your help

Link to comment
  • Moderators
3 hours ago, Heshan_Shalinda_eUnlock said:

Can I bind this into gui button and make some client side function occur by clicking that gui button??

You mean if you click on the gui button, clientside stuff happens for all other players?

You will have to go through the serverside either way unfortunately. Peer to peer is not allowed.

 

 

The code:

When you write /yo. You will be execute the helloClient function on all loaded players.

 

Client

-- Sending
addCommandHandler("yo", function () 
	
	callServerFunction("helloServer")
	
end, false, false)

--------

-- Receiving

function helloClient (playerName) 
	outputChatBox(playerName .. ": YO!")
end

https://wiki.multitheftauto.com/wiki/CallClientFunction

 

Server

function helloServer ()
  	callClientFunction(root, "helloClient", getPlayerName(client))
end

https://wiki.multitheftauto.com/wiki/CallServerFunction

client = a predefined variable.

All predefined variables that are available in the helloServer function: https://wiki.multitheftauto.com/wiki/AddEventHandler

source, this, sourceResource, sourceResourceRoot, client, eventName

 

 


 

btw. I wrote this library to make this kind of stuff a lot easier. Which does more or less the same, only with a lot more features, like return values to callBack functions and security options. Which I recommend for GUI's, for example you only want to give players access to specific actions within a GUI.

 

 

Syntax:

https://gitlab.com/IIYAMA12/mta-communication-enchantment/-/tree/master/documentation/syntax

 

Client:

-- Sending
addCommandHandler("yo", function () 
	
	callServer("helloServer")
	
end, false, false)

--------

-- Receiving

function helloClient (playerName) 
	outputChatBox(playerName .. ": YO!")
end

 

Server:

function helloServer ()
  	callClient(root, "helloClient", getPlayerName(client))
end

 

 

 

 

 

Link to comment
Quote

You mean if you click on the gui button, clientside stuff happens for all other players?

Exactly not !

I want to make vehicle function which change the vehicleWindowState open/close make visible to other players nearby/ in the server.

Did you get that?

This is my coding...............

-- Client Side --


function callClientFunction(funcname, ...)
    local arg = { ... }
    if (arg[1]) then
        for key, value in next, arg do arg[key] = tonumber(value) or value end
    end
    loadstring("return "..funcname)()(unpack(arg))
end
addEvent("onServerCallsClientFunction", true)
addEventHandler("onServerCallsClientFunction", resourceRoot, callClientFunction)



GUIEditor = {
    button = {},
    window = {}
}
addEventHandler("onClientResourceStart", resourceRoot,
    function()
        GUIEditor.window[3] = guiCreateWindow(129, 311, 105, 121, "Open Shutters", false)
        guiWindowSetSizable(GUIEditor.window[3], false)

        GUIEditor.button[13] = guiCreateButton(9, 27, 41, 39, "Shutter 01", false, GUIEditor.window[3])
        GUIEditor.button[14] = guiCreateButton(54, 71, 41, 39, "Shutter 04", false, GUIEditor.window[3])
        GUIEditor.button[15] = guiCreateButton(54, 27, 41, 39, "Shutter 02", false, GUIEditor.window[3])
        GUIEditor.button[16] = guiCreateButton(10, 71, 40, 39, "Shutter 03", false, GUIEditor.window[3])    


local seatWindows = {
	[0] = 4,
	[1] = 2,
	[2] = 5,
	[3] = 3
}


function open()
	if isPedInVehicle(getLocalPlayer()) then
	playerVehicle = getPedOccupiedVehicle ( getLocalPlayer() )
		if ( playerVehicle ) then
			if seatWindows[0] and setVehicleWindowOpen( playerVehicle, 4, not isVehicleWindowOpen( playerVehicle, 4 ) ) then
				outputChatBox( "Driver Window Switched" )
			else
				outputChatBox( "You don't have window!" )
			end
		end
	end
end
addEventHandler ( "onClientGUIClick", GUIEditor.button[13], open )


function open2()
	if isPedInVehicle(getLocalPlayer()) then
	playerVehicle = getPedOccupiedVehicle ( getLocalPlayer() )
		if ( playerVehicle ) then
			if seatWindows[0] and setVehicleWindowOpen( playerVehicle, 2, not isVehicleWindowOpen( playerVehicle, 2 ) ) then
				outputChatBox( "Front Right Window switched" )
			else
				outputChatBox( "You don't have window!" )
			end
		end
	end
end
addEventHandler ( "onClientGUIClick", GUIEditor.button[15], open2 )


function open3()
	if isPedInVehicle(getLocalPlayer()) then
	playerVehicle = getPedOccupiedVehicle ( getLocalPlayer() )
		if ( playerVehicle ) then
			if seatWindows[0] and setVehicleWindowOpen( playerVehicle, 5, not isVehicleWindowOpen( playerVehicle, 5 ) ) then
				outputChatBox( "Back Left Window switched!" )
			else
				outputChatBox( "You don't have window!" )
			end
		end
	end
end
addEventHandler ( "onClientGUIClick", GUIEditor.button[16], open3 )


function open4()
	if isPedInVehicle(getLocalPlayer()) then
	playerVehicle = getPedOccupiedVehicle ( getLocalPlayer() )
		if ( playerVehicle ) then
			if seatWindows[0] and setVehicleWindowOpen( playerVehicle, 3, not isVehicleWindowOpen( playerVehicle, 3 ) ) then
				outputChatBox( "Back Right Window switched!" )
			else
				outputChatBox( "You don't have window!" )
			end
		end
	end
end
addEventHandler ( "onClientGUIClick", GUIEditor.button[14], open4 )




	function guiackapa ()
		if guiGetVisible ( GUIEditor.window[3] )  then
			guiSetVisible ( GUIEditor.window[3], false )
			showCursor(false)
		else
			guiSetVisible ( GUIEditor.window[3], true)
			showCursor(true)
		end
	end
	
	
	addEvent( "ShowGUI", false )
	addEventHandler ( "ShowGUI", getRootElement(), guiackapa )
    bindKey("F10","down",guiackapa)


    end
)


-- Server Script --

function callClientFunction(client, funcname, ...)
    local arg = { ... }
    if (arg[1]) then
        for key, value in next, arg do
            if (type(value) == "number") then arg[key] = tostring(value) end
        end
    end
    -- If the clientside event handler is not in the same resource, replace 'resourceRoot' with the appropriate element
    triggerClientEvent(client, "onServerCallsClientFunction", resourceRoot, funcname, unpack(arg or {}))
end


addCommandHandler("callFunction", 
function (player, cmd, functionName)
    
	callClientFunction(player, functionName)
    
end, false, false)

I have tried many functions like triggerClientEvent in server side script part but I was not able do make my script successfully.

 

This is my script which I have used triggerClientEvent and TriggerServerEvent, Could you please help me on this.

I want to make windowState visible to other players in the server when I switch window using gui Button..

This part is with triggering I don't understand how to get rid of this mess...

please help me...

-- Client Side --
GUIEditor = {
    button = {},
    window = {}
}
addEventHandler("onClientResourceStart", resourceRoot,
    function()
        GUIEditor.window[3] = guiCreateWindow(129, 311, 105, 121, "Open Shutters", false)
        guiWindowSetSizable(GUIEditor.window[3], false)

        GUIEditor.button[13] = guiCreateButton(9, 27, 41, 39, "Shutter 01", false, GUIEditor.window[3])
        GUIEditor.button[14] = guiCreateButton(54, 71, 41, 39, "Shutter 04", false, GUIEditor.window[3])
        GUIEditor.button[15] = guiCreateButton(54, 27, 41, 39, "Shutter 02", false, GUIEditor.window[3])
        GUIEditor.button[16] = guiCreateButton(10, 71, 40, 39, "Shutter 03", false, GUIEditor.window[3])    


local seatWindows = {
	[0] = 4,
	[1] = 2,
	[2] = 5,
	[3] = 3
}


function open_event(wndid)
    local playerVehicle = getPedOccupiedVehicle ( source )
    if ( playerVehicle ) then
        if setVehicleWindowOpen( playerVehicle, wndid, not isVehicleWindowOpen( playerVehicle, wndid ) ) then
            outputChatBox( "Driver Window Switched" )
        else
            outputChatBox( "You don't have window!" )
        end
    end
end
function open_request()
    triggerServerEvent("onVehicleWindowOpenRequest", root, seatWindows[0]);
end
addCommandHandler("open", open_request)
addEvent("open", true)
addEventHandler( "open", localPlayer, open_event )
addEventHandler ( "onClientGUIClick", GUIEditor.button[13], open_request, false )


function open_event2(wndid)
    local playerVehicle = getPedOccupiedVehicle ( source )
    if ( playerVehicle ) then
        if setVehicleWindowOpen( playerVehicle, wndid, not isVehicleWindowOpen( playerVehicle, wndid ) ) then
            outputChatBox( "Front Right Window Switched" )
        else
            outputChatBox( "You don't have window!" )
        end
    end
end
local function open_request2()
    triggerServerEvent("onVehicleWindowOpenRequest2", root, seatWindows[1]);
end
addCommandHandler("open2", open_request2)
addEvent("open2", true)
addEventHandler( "open2", localPlayer, open_event2 )
addEventHandler ( "onClientGUIClick", GUIEditor.button[15], open_request2, false )


function open_event3(wndid)
    local playerVehicle = getPedOccupiedVehicle ( source )
    if ( playerVehicle ) then
        if setVehicleWindowOpen( playerVehicle, wndid, not isVehicleWindowOpen( playerVehicle, wndid ) ) then
            outputChatBox( "Rear Left Window Switched" )
        else
            outputChatBox( "You don't have window!" )
        end
    end
end
local function open_request3()
    triggerServerEvent("onVehicleWindowOpenRequest3", root, seatWindows[2]);
end
addCommandHandler("open3", open_request3)
addEvent("open3", true)
addEventHandler( "open3", localPlayer, open_event3 )
addEventHandler ( "onClientGUIClick", GUIEditor.button[16], open_request3, false )


function open_event4(wndid)
    local playerVehicle = getPedOccupiedVehicle ( source )
    if ( playerVehicle ) then
        if setVehicleWindowOpen( playerVehicle, wndid, not isVehicleWindowOpen( playerVehicle, wndid ) ) then
            outputChatBox( "Rear Right Window Switched" )
        else
            outputChatBox( "You don't have window!" )
        end
    end
end
local function open_request4()
    triggerServerEvent("onVehicleWindowOpenRequest4", root, seatWindows[3]);
end
addCommandHandler("open4", open_request4)
addEvent("open4", true)
addEventHandler( "open4", localPlayer, open_event4 )
addEventHandler ( "onClientGUIClick", GUIEditor.button[14], open_request4, false )


	function guiackapa ()
		if guiGetVisible ( GUIEditor.window[3] )  then
			guiSetVisible ( GUIEditor.window[3], false )
			showCursor(false)
		else
			guiSetVisible ( GUIEditor.window[3], true)
			showCursor(true)
		end
	end
	
	
	addEvent( "ShowGUI", false )
	addEventHandler ( "ShowGUI", getRootElement(), guiackapa )
    bindKey("F10","down",guiackapa)


    end
)
-- Server Side --

addEvent("onVehicleWindowOpenRequest", true);
addEventHandler("onVehicleWindowOpenRequest", root,
	function(wndid)
		triggerClientEvent("open", client, wndid);
	end
);


addEvent("onVehicleWindowOpenRequest2", true);
addEventHandler("onVehicleWindowOpenRequest2", root,
    function(wndid)
        triggerClientEvent("open2", client, wndid);
    end
);


addEvent("onVehicleWindowOpenRequest3", true);
addEventHandler("onVehicleWindowOpenRequest3", root,
    function(wndid)
        triggerClientEvent("open3", client, wndid);
    end
);


addEvent("onVehicleWindowOpenRequest4", true);
addEventHandler("onVehicleWindowOpenRequest4", root,
    function(wndid)
        triggerClientEvent("open4", client, wndid);
    end
);

Thank you....

Link to comment
  • Moderators
38 minutes ago, Heshan_Shalinda_eUnlock said:

Could you please help me on this.

You will have to add both utility functions: callServerFunction, callClientFunction

 

Syntax:

callServerFunction("openWindowServer", <vehicle>, <window>, <window open status>)

 

Client:

if seatWindows[0] and setVehicleWindowOpen( playerVehicle, 4, not isVehicleWindowOpen( playerVehicle, 4 ) ) then
	callServerFunction("openWindowServer", playerVehicle, 4, isVehicleWindowOpen( playerVehicle, 4 ))
	

 

Server:

function openWindowServer (vehicle, window, openStatus)
	if isElement(vehicle) then
		callClientFunction(root, "openWindowClient", vehicle, window, openStatus)
	end
end

 

Client:

function openWindowClient (vehicle, window, openStatus)
	if isElement(vehicle) then
		setVehicleWindowOpen(vehicle, window, openStatus)
	end
end

 

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...