generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: SelectionChanged event?

Previous Thread :: Next Thread 
MrgamesNwatch is not online. MrgamesNwatch
Joined: 02 Feb 2009
Total Posts: 7729
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
kirkyturky12 is not online. kirkyturky12
Joined: 30 Apr 2010
Total Posts: 1915
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
kirkyturky12 is not online. kirkyturky12
Joined: 30 Apr 2010
Total Posts: 1915
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
kirkyturky12 is not online. kirkyturky12
Joined: 30 Apr 2010
Total Posts: 1915
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
MrgamesNwatch is not online. MrgamesNwatch
Joined: 02 Feb 2009
Total Posts: 7729
20 Jun 2013 09:56 AM
oooooh, that's mighty helpful, thanks. C:
Report Abuse
kirkyturky12 is not online. kirkyturky12
Joined: 30 Apr 2010
Total Posts: 1915
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image