Title.
For context: I'm looking to copy a function from a weapon base and sticking it in another.
My hangup is the proper syntax while doing this.
Here's an example of code:
function wepbase1.WasBought = wepbase2.WasBought
In this scenario, the function doesn't exist in wepbase1, and does in wepbase2
However, I may also be overwriting pre-existing functions as well (in which case I'm not sure I should be writing "function" at the start of the line).
I'm not looking to call the function here, this code is in the weapon base, only create it, which is why I left out the colon and parentheses.
Any help appreciated.
For starters, this sounds like a problem that could be solved more elegantly (namely through inheritance), but without having more context I can't assume anything.
You don't need to use the keyword 'function' in front of "wepbase1.WasBought" for starters. wepbase1 is a table, and at the entry 'WasBought' you are wanting to copy another function. Putting function in front of this is not valid code and likely doesn't run.
Here's a contrived example that might help clarify things... might make them more confusing I don't know:
--Declare a test function
function test()
print "test"
end
--Copy that function to a new function
local differentFunc = test
--Call both, see that they do the same thing
test() --should print "test"
differentFunc() --should print "test"
--Overwrite differentFunc, just to show that test is unnaffected
function differentFunc()
print "different"
end
test() --should still print "test"
differentFunc() --should now print "different"
--copying func to table entry
local table = { }
table.func = test
table.func() --should print "test"
--Output of program:
--test (from test())
--test (from differentFunc())
--test (from test())
--different (from differentFunc())
--test (from table.func())
They're two distinct weapon bases, regular inheritance just wouldn't work.
It's a compatibility addon, I'm making a weapon base work correctly within TTT, and that requires the weapon base have functions from the TTT weapon base.
Normally, I can just copy+paste the required functions into the new weapon base, but this is something I'm uploading to the workshop, and the weapon base that may be updated in the future.
But yes, your reply is perfect. Out of all the things, it's proper function syntax that escapes me.
Thanks for the reply.
Sorry, you need to Log In to post a reply to this thread.