Overkillz

Run functions with the same name

Recommended Posts

Hello dear community. Today I wanted to create a multi room script. However, I have realized that might could be an easy way to make something similar like copy and paste to use almost the whole code as template but on different files. (ON THE SAME RESOURCE BUT DIFFERENT FILES)

Here goes my question. Could I run the function from one file just changing a VARIABLE VALUE?.

I would like that you explain me whats the best way to reach and optimize result without using different names for functions, events ..etc The code is going to be so long, thats why I wont to start yet.

NOTE: The script looks the same 2 times due to I just change the variable 'currentArena'. Read it carefully please.

--### guiResourceName/
--CIENT
function joinArena(ID)
	if tonumber(ID) == 1 then
		triggerServerEvent ( "onPlayerJoinServerRoom", localPlayer, 1 )
	elseif tonumber(ID) == 2 then
		triggerServerEvent ( "onPlayerLeaveServerRoom", localPlayer, 1 )
	elseif tonumber(ID) == 3 then
		triggerServerEvent ( "onPlayerJoinServerRoom", localPlayer, 2 )
	elseif tonumber(ID) == 4 then
		triggerServerEvent ( "onPlayerLeaveServerRoom", localPlayer, 2 )
	end
end
bindKey("1","down",joinArena)
bindKey("2","down",joinArena)
bindKey("3","down",joinArena)
bindKey("4","down",joinArena)

-- ### roomResource/room1/ -> CLIENT & SERVER
--CLIENT
local currentArena = 1
local arenaList = {}

function loadEvents()
	addEvent( "onEventTest", true )
end
loadEvents()

function removeHandlers()
	removeEventHandler( "onEventTest", getRootElement(), listEventTest)
end

addEvent("onPlayerJoinRoom", true)
addEventHandler("onPlayerJoinRoom", root, function(ArenaNo)
	if ArenaNo == currentArena then
		outputChatBox("Succesfully joined at arena: "..currentArena)
		loadHandlers()
	end
end)

addEvent("onPlayerLeaveRoom", true)
addEventHandler("onPlayerLeaveRoom", root, function(ArenaNo)
	if ArenaNo == ArenaNo then
		outputChatBox("Succesfully left arena: "..currentArena)
		removeHandlers()
	end
end)

function listEventTest(tableListTest)
	if tableListTest and type(tableListTest) == "table" then
		arenaList = tableListTest
	end
end

--SERVER
local currentArena = 1
local srvArenaTable = {}
local roomRunning = false

function updateRoomResources()
	srvArenaTable = {}
	local resourceTable = getResources()
    for resourceKey, resourceValue in ipairs(resourceTable) do
        local name = getResourceName(resourceValue)
		table.insert(srvArenaTable,{resourceName = name})
    end
end

function updateRoomOnStart()
	updateRoomResources()
	outputChatBox("Updating Resource list for arena: "..currentArena)
end
addEventHandler("onResourceStart",resourceRoot,updateArenaonStart)

function joinServerRoom(ArenaNo)
	if ArenaNo == currentArena then
		triggerClientEvent(source, "onPlayerJoinRoom", source, currentArena)
		loadArena()
	end
end
addEvent("onPlayerJoinServerRoom",true)
addEventHandler("onPlayerJoinServerRoom",getRootElement(),joinServerRoom)

function leaveServerRoom(ArenaNo)
	if ArenaNo == currentArena then
		triggerClientEvent(source,"onPlayerLeaveRoom",source, currentArena)
	end
end
addEvent("onPlayerLeaveServerRoom",true)
addEventHandler("onPlayerLeaveServerRoom",getRootElement(),leaveServerRoom)

function loadArena()
	if not roomRunning then
		startRoomApps()
	end
end

function startRoomApps()
	outputChatBox("Apps should be running for room: "currentArena)
end

-- ### roomResource/room2/ -> CLIENT & SERVER
--CLIENT
local currentArena = 2 -- <---- THIS CHANGES
local arenaList = {}

function loadEvents()
	addEvent( "onEventTest", true )
end
loadEvents()

function removeHandlers()
	removeEventHandler( "onEventTest", getRootElement(), listEventTest)
end

addEvent("onPlayerJoinRoom", true)
addEventHandler("onPlayerJoinRoom", root, function(ArenaNo)
	if ArenaNo == currentArena then
		outputChatBox("Succesfully joined at arena: "..currentArena)
		loadHandlers()
	end
end)

addEvent("onPlayerLeaveRoom", true)
addEventHandler("onPlayerLeaveRoom", root, function(ArenaNo)
	if ArenaNo == ArenaNo then
		outputChatBox("Succesfully left arena: "..currentArena)
		removeHandlers()
	end
end)

function listEventTest(tableListTest)
	if tableListTest and type(tableListTest) == "table" then
		arenaList = tableListTest
	end
end

--SERVER
local currentArena = 2 -- <---- THIS CHANGES
local srvArenaTable = {}
local roomRunning = false

function updateRoomResources()
	srvArenaTable = {}
	local resourceTable = getResources()
    for resourceKey, resourceValue in ipairs(resourceTable) do
        local name = getResourceName(resourceValue)
		table.insert(srvArenaTable,{resourceName = name})
    end
end

function updateRoomOnStart()
	updateRoomResources()
	outputChatBox("Updating Resource list for arena: "..currentArena)
end
addEventHandler("onResourceStart",resourceRoot,updateArenaonStart)

function joinServerRoom(ArenaNo)
	if ArenaNo == currentArena then
		triggerClientEvent(source, "onPlayerJoinRoom", source, currentArena)
		loadArena()
	end
end
addEvent("onPlayerJoinServerRoom",true)
addEventHandler("onPlayerJoinServerRoom",getRootElement(),joinServerRoom)

function leaveServerRoom(ArenaNo)
	if ArenaNo == currentArena then
		triggerClientEvent(source,"onPlayerLeaveRoom",source, currentArena)
	end
end
addEvent("onPlayerLeaveServerRoom",true)
addEventHandler("onPlayerLeaveServerRoom",getRootElement(),leaveServerRoom)

function loadArena()
	if not roomRunning then
		startRoomApps()
	end
end

function startRoomApps()
	outputChatBox("Apps should be running for room: "currentArena)
end

 

Edited by Overkillz

Share this post


Link to post

Sorry, but I didn't got your question !

7 hours ago, Overkillz said:

Could I run the function from one file just changing a VARIABLE VALUE?

For a example ?

Share this post


Link to post

 

You can do that with local functions. Those functions can only be used within the file

local name = function () end
local function name ()
  
  -- this one is able call itself,in compare to the previous one: name()
end  

 

local name -- variable scope starts here

function name ()
  
end

 

Another solution would a table with methods.

 

 

  • Like 1

Share this post


Link to post
1 hour ago, IIYAMA said:

Another solution would a table with methods

What do you mean with table methods ? Something like this ?

Room = {}

function Room.Join()
	--Something Here
end

function Room.Leave()
	--Something Here
end

function Room.Start()
	--Something Here
end

--.....

 

Share this post


Link to post
15 minutes ago, Overkillz said:

What do you mean with table methods ? Something like this ?


Room = {}

function Room.Join()
	--Something Here
end

function Room.Leave()
	--Something Here
end

function Room.Start()
	--Something Here
end

--.....

 

Yes. To be honest, I still do not understand your question completely, so I am trying to gamble a bit.

 

 

 

  • Like 1

Share this post


Link to post

I'd recommend using table based functions for organization.

functions = {}
functions['RoomA'] = {}
functions['RoomB'] = {}

functions['RoomA'].test = function ()
  
end


functions['RoomB'].test = function ()
  
end

functions['RoomA'].test()
functions['RoomB'].test()

 

  • Like 1

Share this post


Link to post
34 minutes ago, CodyJ(L) said:

I'd recommend using table based functions for organization.


functions = {}
functions['RoomA'] = {}
functions['RoomB'] = {}

functions['RoomA'].test = function ()
  
end


functions['RoomB'].test = function ()
  
end

functions['RoomA'].test()
functions['RoomB'].test()

 

Well, but following this method I must to have all the rooms on the same file. I want to have them on different files but in the same resource.

And I have another question. How can I call some of these functions from another resource without adding a custom event, exports and without using callClientFunction/callServerFunction ?

Thanks for your help. I know I can sound so annoying but I would like to learn more about this.

Regards.

Share this post


Link to post
17 minutes ago, Overkillz said:

 

And I have another question. How can I call some of these functions from another resource without adding a custom event, exports and without using callClientFunction/callServerFunction ? 

That is not possible.

 

 

 

 

Edited by IIYAMA
  • Like 1

Share this post


Link to post
40 minutes ago, IIYAMA said:

That is not possible.

Well, according to your previous answer. Is there someway to execute the function for ROOM 1 (And not the ROOM 2) from an external resource without using a variable ?

--## resourceName/room1/client.lua
local function testFunction ()
	--CODE
end
addEvent("onTestThisFunction",true)
addEventHandler("onTestThisFunction",root,testFunction)

--## resourceName/room2/client.lua -- <-- Now the folderRoom is the 2nd one (SAME Resource)

local function testFunction ()
	--CODE
end
addEvent("onTestThisFunction",true)
addEventHandler("onTestThisFunction",root,testFunction)

--## anotherResource/ -- 

triggerServerEvent ( "onTestThisFunction", localPlayer )

 

Share this post


Link to post

You don't have to have them in the same file. You define the global table in one file then every file can use that table.

  • Like 1

Share this post


Link to post
On 12/01/2019 at 18:48, CodyJ(L) said:

You don't have to have them in the same file. You define the global table in one file then every file can use that table.

Well, but could I run just one of the functions by a trigger using the same name for the event ? Something like that. 

-- FOR ROOM 1
addEvent("onTestThisFunction",true)
addEventHandler("onTestThisFunction",root,whateverFunctionName)

-- Same event name FOR ROOM 2
addEvent("onTestThisFunction",true)
addEventHandler("onTestThisFunction",root,whateverFunctionName)

In case of no. which one would be the optimal way for it ?

Thanks for ur help, regards.

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.