|
| 20 Jun 2013 12:39 AM |
mkay, so i'm modifying cmdutl to function in-game, and i've got just about everything done I think, I'm in the process of recreating the Selection service.
Problem: I came across a section of the code that depends on the SelectionChanged event, which seems to be undocumented in the wiki, I was wondering if anyone knows what it is? I assume it fires when the Selection changes. Also If anyone is familiar with cmdutl's code and wouldn't mine telling me what this part of code is doing exactly, to many functions that are all interconnecty. I also have a feeling I may need to use metatables to remake SelectionChanged, however I'm not to experienced with them....
Code in Question: local Selection = Selection() Selection.SelectionChanged:connect(function() if SelectedTool then local callback = SelectionChangedCallback[SelectedTool] if callback then callback() end local func = OnSelectionChanged[SelectedTool] if func then func(SelectedTool,Variables[SelectedTool]) end end end)
My Selection service: function Selection() local Items = {} return{ Get = function() return Items end, Set = function(item) --table.insert(Items, item) Items = {item} end } end |
|
|
| Report Abuse |
|
|
|
| 20 Jun 2013 09:16 AM |
SelectionService = {["Items"] = {}, ["Connections"] = {}, ["Get"] = function() return {unpack(SelectionService.Items)} end, ["Set"] = function(x) SelectionService.Items = {unpack(x)} end, ["SelectionChanged"] = function(func) table.insert(SelectionService.Connections,func) end,}
setmetatable(SelectionService.Items,{ __index = SelectionService.Items, __newindex = function() -- Tell when it is edited. for i,v in pairs(SelectionService.Connections) do v(SelectionService.Get()) -- Fire all of the connections. end end,})
Btw, that's untested, so yeah.
Modified code:
local Selection = Selection() Selection.SelectionChanged(function() -- Notice I took out the :connect part? <----------- if SelectedTool then local callback = SelectionChangedCallback[SelectedTool] -- What is SelectionChangedCallback? <---------------- if callback then callback() end local func = OnSelectionChanged[SelectedTool] -- What is OnSelectionChanged? <------------------- if func then func(SelectedTool,Variables[SelectedTool]) end end end)
And this is all untested, so yeah. |
|
|
| Report Abuse |
|
|
|
| 20 Jun 2013 09:18 AM |
Oops, there was a problem, sorry.
Replace this part:
setmetatable(SelectionService,{ __index = SelectionService, __newindex = function(Self,Ind,Value) -- Tell when it is edited. if Ind == "Items" then for i,v in pairs(SelectionService.Connections) do v(SelectionService.Get()) -- Fire all of the connections. end end end,}) |
|
|
| Report Abuse |
|
|
|
| 20 Jun 2013 09:37 AM |
Acutally metatables probably isn't the best solution. Here's a simplified version that works just fine:
SelectionService = {["Items"] = {}, ["Connections"] = {}, ["Get"] = function() return {unpack(SelectionService.Items)} end, ["Set"] = function(x) SelectionService.Items = {unpack(x)} for i,v in pairs(SelectionService.Connections) do v(SelectionService.Get()) end end, ["SelectionChanged"] = function(func) table.insert(SelectionService.Connections,func) end,}
And then you would do:
SelectionService.SelectionChanged(function(Selection) table.foreach(Selection,print) -- Print each item in the selection. end) |
|
|
| Report Abuse |
|
|
|
| 20 Jun 2013 09:56 AM |
| oooooh, that's mighty helpful, thanks. C: |
|
|
| Report Abuse |
|
|
|
| 20 Jun 2013 11:07 AM |
SelectionService = {["Items"] = {}, ["Connections"] = {}, ["Get"] = function(Self) return {unpack(SelectionService.Items)} end, ["Set"] = function(Self,x) SelectionService.Items = {unpack(x)} for i,v in pairs(SelectionService.Connections) do v(SelectionService.Get()) end end, ["SelectionChanged"] = function(func) table.insert(SelectionService.Connections,func) end,}
SelectionService:Set({})
|
|
|
| Report Abuse |
|
|