Jump to content

Call another function from different files into the same resource


Overkillz

Recommended Posts

Hello dear community, 

First of all, I know that i can solve my problem with a simple trigger, but Im a bit curious and I would like to know why this is not working or which one is the best way to call it.

Well, I have done a template to use it multiple times in the same resource but just changing a variable, so, some of them uses the same fucntion name but i just used local (This should prevent interaction issues) 

So I realized the is only executed the last one added in meta.xml

I know that Im explaining very very bad, but I have done an example that it understable (I think so)

--## META.XML
<script src="file1.lua" type="client" cache="false" />
<script src="file2.lua" type="client" cache="false" />
<script src="file3.lua" type="client" cache="false" />  --THE LAST ONE IS ONLY EXECUTED

--## MAIN FILE
showContent(1) --THIS WONT WORK
showContent(2) --THIS WONT WORK
showContent(3) --WORKING DUE TO IS THE LAST ONE IN META.XML

--## 1ST FILE
local fileID = 1
local function sayHI()
	outputChatBox("HI WORLD FROM FILE 1")
end

function showContent(id)
	if fileID == id then
		sayHI()
	end
end

--## 2ND FILE
local fileID = 2
local function sayHI()
	outputChatBox("HI WORLD FROM FILE 2")
end

function showContent(id)
	if fileID == id then
		sayHI()
	end
end

--## 3RD FILE
local fileID = 3
local function sayHI()
	outputChatBox("HI WORLD FROM FILE 3")
end

function showContent(id)
	if fileID == id then
		sayHI()
	end
end

I hope you can bring me a hand with this simple doubt.

Thanks for reading, best regards.

Link to comment
  • Moderators
9 minutes ago, Overkillz said:

but Im a bit curious and I would like to know why this is not working or which one is the best way to call it.

A function name is just a variable.

When creating a function with the same name, you will overwrite that variable/function.

 


 

You can solve your problem with a table.

Main file:

showContent = {}


setTimer(function () -- add a little delay so that the function can be added before calling it.
	showContent[1]()
	showContent[2]()
	showContent[3]()
end, 100, 1)

For each other file:

local fileID = --[[...]]
showContent[fileID] = function ()
	sayHI()
end

 

 

 

 

 

  • Like 1
Link to comment
  • Moderators
6 hours ago, Overkillz said:

Is there another efficent way? (Not adding all the content in the same file)

If you want to create environments, OOP would be another way and maybe also use a meta table to inherit properties

 

But I can understand that it might be a little bit complex, especially when you are doing it like this:

showContentConstructor = {
	new = function (self)
		local newShowContent = {}

		setmetatable(newShowContent, self.metaTable)

		return newShowContent
	end,
	prototype = { -- initial properties
		text = "<please set text>",
		sayHI = function (self)
			return outputChatBox(self.text)
		end
	}
}

-- Set the meta table as lookup table when the information is not found in the instance table
showContentConstructor.metaTable = {__index = showContentConstructor.prototype}

-- create new instances
showContent1 = showContentConstructor:new()
showContent2 = showContentConstructor:new()

showContent1:sayHI()
showContent1.text = "test1"
showContent1:sayHI()

showContent2:sayHI()
showContent2.text = "test2"
showContent2:sayHI()

 

Unfortunately there are no options without tables afaik, so you really need to be able to understand the basics of tables, if you want to build this.

So I recommend doing it the way as in my previous reply, which should be easy to do.

Edited by IIYAMA
  • Like 1
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...