Advertisement
WackoMcGoose

LibCommon Module for Roblox

May 22nd, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.29 KB | None | 0 0
  1. --[[ LibCommon Module, by WackoMcGoose
  2.  
  3. So yeah, figured I'd share this neat little pile of functions with you guys. I created these for use in Roblox Kart: Courses Unlimited, but they're generic enough and useful enough to use in any game. In particular, makeDataValue and makeInstance(WithProperties) ought to save you quite a bit of typing.
  4.  
  5. Releasing this as open source (because again, they're just simple little utility methods I think any Roblox dev could find useful, and I doubt I'm the first to come up with them - in particular getRecursiveChildren is a no-brainer that should be stock code), credit appreciated but not required.
  6.  
  7. Usage: Just paste wholecloth into a ModuleScript, require() that thing, then requiredModule.someName("Some", "list", of, { params }).
  8.  
  9. ]]
  10.  
  11. local module = {}
  12.  
  13. --Create a %SOMETHING%Value object, optionally setting its value
  14. --Params: String class, String name, Instance parent, [Data type corresponding to object] value (optional)
  15. --Returns: Instance %SOMETHING%Value, or nil
  16. module.makeDataValue = function(class, name, parent, value)
  17.     local outputdis = nil
  18.     local diditwork = pcall(function()
  19.         outputdis = Instance.new(class)
  20.         outputdis.Name = name
  21.         outputdis.Parent = parent
  22.         if value ~= nil then outputdis.Value = value end
  23.     end)
  24.     if diditwork then return outputdis else return nil end
  25. end
  26.  
  27. --Create... well, an Instance of something. Dead-simple, just makes a default of an object.
  28. --Params: String class, String name, Instance parent
  29. --Returns: Instance (ClassName = class), or nil
  30. module.makeInstance = function(class, name, parent)
  31.     local outputdis = nil
  32.     local diditwork = pcall(function()
  33.         outputdis = Instance.new(class)
  34.         outputdis.Name = name
  35.         outputdis.Parent = parent
  36.     end)
  37.     if diditwork then return outputdis else return nil end
  38. end
  39.  
  40. --Create an Instance of something, and set properties in it.
  41. --Params: String class, String name, Instance parent, Table properties (format: {PropertyName = PropertyValue, ...}, invalid things will be ignored)
  42. --Returns: Instance (ClassName = class), or nil
  43. module.makeInstanceWithProperties = function(class, name, parent, properties)
  44.     local outputdis = module.makeInstance(class, name, parent)
  45.     if outputdis == nil then return nil end
  46.     for property, value in pairs(properties) do pcall(function() outputdis[property] = value end) end
  47.     return outputdis
  48. end
  49.  
  50. --Set multiple properties of an existing Instance
  51. --Params: Instance object, Table properties (format: {PropertyName = PropertyValue, ...}, invalid things will be ignored)
  52. --Returns: Instance (same as passed in), or nil
  53. module.setPropertiesInInstance = function(object, properties)
  54.     if object == nil or properties == nil then return end
  55.     if not pcall(function() object:IsA("Instance") end) then return end --If pcall fails (IsA not valid member), then it's not an Instance!
  56.     for property, value in pairs(properties) do pcall(function() object[property] = value end) end
  57.     return object --Eh, for convenience
  58. end
  59.  
  60. --Clone specified properties from one Instance to another, using the given table to specify which properties to copy
  61. --Params: Instance source (copy things from), Instance target (copy things to), Table propsCloneList (format: {"PropertyName", ...}, invalid things will be ignored)
  62. --Returns: Instance (target that params were cloned to), or nil
  63. module.clonePropertiesToInstance = function(source, target, propsCloneList)
  64.     if source == nil or target == nil or propsCloneList == nil then return end
  65.     if not pcall(function() source:IsA("Instance") target:IsA("Instance") end) then return end --See above reasoning
  66.     --Uncomment next line if you want it to be strict (must be same ClassName to be valid)
  67.     --  if source.ClassName ~= target.ClassName then return end
  68.     --Uncomment next line if you want it to be /semi/-strict (doesn't have to be same ClassName, but one must be inheriting class of another)
  69.     --  if not source:IsA(target.ClassName) and not target:IsA(source.ClassName) then return end
  70.    
  71.     --Now clone values of source's properties, for named props in propsCloneList, into target (will only do it if both have a prop of that name)
  72.     --Note: Unlike the other methods, propsCloneList goes with /values/ for which prop, not keys
  73.     for _, property in pairs(propsCloneList) do pcall(function() target[property] = source[property] end) end
  74.     return target --Eh, why not.
  75. end
  76.  
  77. --Essentially the same as makeInstanceWithProperties, but with an additional shortcut for setting Surfaces (defaults to Smooth)
  78. --Params: String class, String name, Instance parent, Table properties (format: {PropertyName = PropertyValue, ...}) (optional), Table surfaces (format: {*Surface = Enum.SurfaceType,...}) (optional)
  79. --Returns: Instance (BasePart of type class), or nil if class was not a BasePart ClassName
  80. module.makePart = function(class, name, parent, properties, surfaces)
  81.     local outputdis = module.makeInstanceWithProperties(class, name, parent, properties)
  82.     if outputdis == nil then return nil end
  83.     if not outputdis:IsA("BasePart") then return nil end
  84.     if surfaces == nil then surfaces = {} end
  85.     outputdis.TopSurface = surfaces.TopSurface or "Smooth"
  86.     outputdis.BottomSurface = surfaces.BottomSurface or "Smooth"
  87.     outputdis.LeftSurface = surfaces.LeftSurface or "Smooth"
  88.     outputdis.RightSurface = surfaces.RightSurface or "Smooth"
  89.     outputdis.FrontSurface = surfaces.FrontSurface or "Smooth"
  90.     outputdis.BackSurface = surfaces.BackSurface or "Smooth"
  91.     if outputdis:IsA("FormFactorPart") then outputdis.FormFactor = "Custom" end
  92.     return outputdis
  93. end
  94.  
  95. --Make a Weld object, with the cframes set to the current cframes of the specified parts
  96. --Assumes that both params are BaseParts, will silently fail otherwise
  97. --Params: Instance(BasePart) part0, Instance(BasePart) part1
  98. --Returns: Instance (Weld that was created), or nil
  99. module.makeWeld = function(part0, part1)
  100.     local weld = nil
  101.     local diditwork = pcall(function()
  102.         weld = Instance.new("Weld")
  103.         weld.Part0 = part0
  104.         weld.C0 = part0.CFrame - part0.Position
  105.         weld.Part1 = part1
  106.         weld.C1 = (part1.CFrame - part0.Position):inverse()
  107.         weld.Parent = part0
  108.     end)
  109.     if diditwork then return weld else return nil end
  110. end
  111.  
  112. --Makes a copy of a table, with the objects scrambled, either randomly or random-by-seed
  113. --Params: Table scrambleDis (format: Array, arbitrary values), Integer seed (optional)
  114. --Returns: Table (scrambled copy of passed-in table), or nil
  115. module.scrambleTable = function(scrambleDis, seed)
  116.     local success, scrambled = pcall(function()
  117.         local result = {}
  118.         local toAdd = scrambleDis
  119.         if seed ~= nil and pcall(function() return seed + 1 end) then math.randomseed(seed) end --Set random seed if seed is provided (and is a number)
  120.         for i = 1, #scrambleDis do
  121.             local index = math.random(1, #toAdd)
  122.             result[i] = toAdd[index]
  123.             table.remove(toAdd, index)
  124.         end
  125.         return result
  126.     end)
  127.     if success then return scrambled else return nil end
  128. end
  129.  
  130. --Recursive version of Instance:GetChildren(). That's all there is to say on the matter.
  131. --Params: Instance obj
  132. --Returns: Table (all objects that are a descendant of obj, in the same format as Instance:GetChildren()), or nil
  133. module.getRecursiveChildren = function(obj)
  134.     if not pcall(function() return obj:IsA("Instance") end) then return nil end --Sanity check, that object better be an Instance
  135.     local childrens = {}
  136.     for _,v in pairs(obj:GetChildren()) do
  137.         table.insert(childrens, v)
  138.         if #v:GetChildren() > 0 then
  139.             local subChildrens = module.getRecursiveChildren(v)
  140.             for _2,v2 in pairs(subChildrens) do
  141.                 table.insert(childrens, v2)
  142.             end
  143.         end
  144.     end
  145.     return childrens
  146. end
  147.  
  148. --Weighted random: Pass in a table of key-value pairs, where the keys can be either ints or strings, and values are ints >= 1.
  149. --Will return one of the keys randomly (based on weights), or nil if the table be invalid.
  150. --Params: Table (format: {Integer/String key = Integer value, ...})
  151. --Returns: Integer/String key, or nil if you did tables wrong
  152. module.weightedRandom = function(values)
  153.     result = nil
  154.     pcall(function()
  155.         local index = 1
  156.         local weighttable = {}
  157.         --Set up table, eith 'value' copies of 'key'
  158.         for key, value in pairs(values) do
  159.             if value < 1 then value = 1 end
  160.             for i = 1, value do
  161.                 weighttable[index] = key
  162.                 index = index + 1
  163.             end
  164.         end
  165.         result = weighttable[math.random(1, #weighttable)]
  166.     end)
  167.     return result
  168. end
  169.  
  170. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement