Recommended Posts

MTA-Communication-Enchantment

This is an enchantment that allows you to communicate between clientside and serverside a bit easier. If you know how to work with events, then you probably do not need this, but it has some nice features which allows you to sit back and write less code + achieve some nice results.

Note: It is important to keep in mind that this is an enchantment. Which means it is just an layer on top of the basic functionalities of MTA. And most enchantments come with a cost, in this case that is bit of performance.

 

I will keep the information of topic to the minimal, as I have written most of the information already on the repository.

You can find the repository here.

 

What can you do with it?

 


 

Calling from clientside to serverside

 

Client

callServer("hello")

 

Server

function hello ()
	outputChatBox("Hello client!")
end

 


 

Calling from serverside to clientside

 

Server

addCommandHandler("callclient", function (player) 
	
	-- An addCommandHandler is needed, because the client hasn't loaded it's scripts yet.
	callClient(player, "hello")
	
end, false, false)

 

Client

function hello ()
	outputChatBox("Hello server!")
end

 


 

Ok, ok, that was boring. The next one this is a bit nicer!

 

Hello are you there? Just Call-me-back... I miss(ed) you too
Callback

 

Client

callServer(
	"callbackMe", 
	"argument", 
	function (argument) -- < This is the callback function
		outputChatBox(argument) 
	end
)

 

Server

function callbackMe (argument)
	return argument .. " < I looked at it :)"
end

 

 


 

Callback + internal arguments

Sometimes you have arguments that you simply can't send over. > functions
Or arguments that shouldn't be send over. > LARGE quantities of database data

Internal arguments can be used to pass information to a callback without exposing it to the other side(client/server).

 

Client

callServer(
	"callbackMe", 
	
	--------------------------------
	-- arguments that are send over
	
	"argument", 
	
	--
	--------------------------------
	
	function (internalArgument, argument) -- < This is the callback function.
	
		outputChatBox(internalArgument)
		outputChatBox(argument) 
		
	end,
	
	--------------------------------
	-- arguments that are not send over
	
	"internalArgument" -- < internal argument
	
	--
	--------------------------------
)

 

Server

function callbackMe (argument)
	return argument .. " < I looked at it :D"
end

 

 


 

Ha! Serverside what is that? No need for complicated things!

Communicate between clients without writing a single line of serverside. Magic!

Note: There is serverside used behind the scenes, you just don't have to write it.

 

Client

function smile (player)
	
	outputChatBox((isElement(player) and getPlayerName(player) or "[unknown]") .. " has send you a: :)")
	
	local x, y, z = getElementPosition(localPlayer)
	setElementPosition(localPlayer, x, y, z + 100)
	
end
addRemoteClientAccessPoint(smile) -- < This function allows other clients to call this function.


---------------------------------------
--                                   --
function getPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(getElementsByType("player")) do
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if name_:find(name, 1, true) then
                return player
            end
        end
    end
end
-- Author: TAPL
-- https://wiki.multitheftauto.com/wiki/GetPlayerFromPartialName
--                                   --
---------------------------------------


addCommandHandler("smile", 
function (cmd, playerName) 
	local player = getPlayerFromPartialName(playerName)
	if player then
		outputChatBox("Sending smile!")
		callRemoteClient(player, "smile", player)
	else
		outputChatBox("Can't find player!")
	end
end)

 

 


 

Turtle, I will wait for you to catch up. So don't worry, you are still cute.

Await functions

 

When a player has joined the server, he or she doesn't have download + loaded his scripts yet. This means that you can't deliver your love letter yet and all your work will be for nothing. But what if you don't have to worry about that? :propellerhead:You can just wait now!

 

Server

addEventHandler("onPlayerJoin", root, 
function ()
	
	callClientAwait(source, "testCallClientAwait")
	
end)

 

Client

function testCallClientAwait ()
	outputChatBox("Yes this works!")
end

 


 

Security

Worried about security issues?

  • Remote calls for C++/MTA functions have been blocked.
  • There is a whitelist feature included, if enabled your code can only remote-call whitelisted functions. (this is disabled by default)
    Read the docs for that. Here and here

 

 

 

 

 

 

Edited by IIYAMA
  • Like 7

Share this post


Link to post

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.