So, here is a dilemma...
Let's just say, that we have 2 "TextBox" under "ScreenGui" in StarterGui (name them Frame1 and Frame2), and we want to change the colors of the object that we select. We want the first box to turn (234,2,183), and other one to turn (0,0,0)
so here, we are just setting up events and functions:
following code is from a localscript under ScreenGui
frame1 = script.Parent.Frame1 frame2 = script.Parent.Frame2 print("wtf") function onClickColorChange1(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then print("pressed!") frame1.BackgroundColor3 = Color3.fromRGB(234,2,183) end end function onClickColorChange2(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then print("Pressed!") frame2.BackgroundColor3 = Color3.new(0,0,0) end end frame1.InputBegan:connect(onClickColorChange1) frame2.InputBegan:connect(onClickColorChange2)
Of course the code works - that's not the problem. The problem is, I think it is VERY inefficient. I am thinking about putting an if statement, saying "If frame1 is pressed, then change colors for frame1" and so on. But how do you construct this if statement? How do you call the object that event acted upon?
Thank you so much. You guys always help me a lot. I appreciate it.
I assume, by "inefficient", you mean that it is inefficient for you to have to type all that for every frame you want; otherwise, what you have is actually as efficient as you can get in terms of processing time. However, it's far more important to make it easy to modify, especially when the processing efficiency difference is small (it's unlikely to be a concern unless you're running a large loop every frame or something -- and you should probably test it out to make sure it's slow before worrying about performance).
Anyway:
When you see very similar code (as in onClickColorChange1 and 2), you should always strive to simplify it; this usually involves functions and tables. Replace the similar code blocks with a single function that has a parameter for every difference between the originals. In your case, with the onClickColorChange functions, the differences are which frame to act on and which color to change it to. The resulting function could be this:
function onClickColorChange(frame, color, input) if input.UserInputType == Enum.UserInputType.MouseButton1 then print("pressed for", frame, "; changing to color", color) frame.BackgroundColor3 = color end end
To connect the events to this function, you could use an anonymous function, like this:
frame1.InputBegan:Connect(function(input) onClickColorChange(frame1, Color3.new(234, 2, 183), input) end) frame2.InputBegan:Connect(function(input) onClickColorChange(frame2, Color3.new(0, 0, 0), input) end)
(Note that connect
is deprecated, use Connect
instead)
However, those two lines are also duplicates. If you want the code to be of really good quality, you get rid of that duplication as well by putting the varying data in a table and iterating over it:
local function Frame(frame, color) -- Helps create the data structure we want return {Frame = frame, Color = color} end local frameData = { Frame(frame1, Color3.new(234, 2, 183)), Frame(frame2, Color3.new(0, 0, 0)), } function onClickColorChange(frame, color, input) if input.UserInputType == Enum.UserInputType.MouseButton1 then print("pressed for", frame, "; changing to color", color) frame.BackgroundColor3 = color end end for i = 1, #frameData do frameData[i].Frame.InputBegan:Connect(function(input) onClickColorChange(frameData[i].Frame, frameData[i].Color, input) end) end
Now, if you aren't going to use onClickColorChange
anywhere else, you could even just move the contents of the function into the for loop (so that the anonymous function doesn't have to call onClickColorChange
), but it's that's a choice of organization.
Either way, you can now easily add more frames/colors by extending the frameData list. If you regularly needed other data with frames, you might also adjust the Frame
function I put in there. If you like the idea of that Frame
function, you might also be interested in classes (look up classes or Object Oriented Programming in lua).