So I have an idea, but upon initial thought it seems like a bad one. I'd like to store UserData for the life of the script in an external table (called by require).
I'm looking for ways to implement this, but I'm thinking that storing UserData in a global environment could be troublesome. Could someone point out any pitfalls from this method? Or provide a better suggestion?
External (centralized) function - loaded using 'require' during script execution:
--Chat.lua
--Location: Scripts/Globals/Custom/Functions/Chat
Convo = 0xD
Special = 0x1F
TempUserData = {}
TempTargetData = {}
function Show (Message, Display, Color, VAR1, VAR2, VAR3, VAR4, VAR5)
local User = TempUserData[1]
local Name = Display and TempTargetData[1]..": " or " "
Color = Color or Convo
VAR1 = VAR1 or nil
VAR2 = VAR2 or nil
VAR3 = VAR3 or nil
VAR4 = VAR4 or nil
VAR5 = VAR6 or nil
if (VAR1) then
User:PrintToUser(string.format(Name..Message, VAR1, VAR2, VAR3, VAR4, VAR5), Color)
else
User:PrintToUser(Name..Message, Color)
end
end
Main script:
---------------------------------------------------------------
package.loaded["Scripts/Globals/Custom/Functions/Chat"] = nil
require("Scripts/Globals/Custom/Functions/Chat")
---------------------------------------------------------------
local function foo (VAR1)
Show("Message")
Show("Message",true,Special,VAR1)
--Stuff
end
local function bar (VAR2)
Show("Message")
Show("Message",true,Special,VAR2)
--Stuff
end
function onTrigger (User, VAR1, VAR2, VAR3) --Start of script
table.insert(TempUserData, User)
table.insert(TempTargetData, "System")
--Stuff
end
The reason I'd like to do this is because:
User:PrintToUser is not my function. It could change at some point or be renamed and since I have so many files using it, changing it in one location and having the effect cascade to my own custom scripts is appealing to me.
Some messages can be lengthy and having User and Target in-line with the function call is just more space added over and over.
The end result is shorter and cleaner, thus making it more readable in a large script. Also, make debugging easier for me (how many times I've misspelled PrintToUser as printToUser...)
Instead of:
User:PrintToUser(string.format("Message %s, %s", VAR1, VAR2), 0xD)
It could be cleaner:
Show("Message %s, %s", true, Convo, VAR1, VAR2) -- With variables
or
Show("Message",true) -- Display "System" with default color
Show("Message") -- Don't display "System", still with default color
I have two questions regarding this:
Based on the code above, would I run into problems if several people are using the same script at once? (i.e. User Data somehow being mixed up, or global variables in use).
Would there be any performance degradation from redefining functions in this way? Or from storing, retrieving, deleting a table of User Data each time? (200+ people using this)