User:ArceusInator/Sandbox: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>ArceusInator working |
>ArceusInator ... |
||
Line 71: | Line 71: | ||
function math.curt(v) | function math.curt(v) | ||
if | if r(v) ~= "number" then | ||
error("Number expected",1) | error("Number expected",1) | ||
end | |||
return v^(1/3) | |||
end | |||
function t(...) -- for me | |||
return shared:ToupleToTable(...) | |||
end | |||
--- math.add(...) | |||
function math.add(...) | |||
local fin = 0 | |||
for i,v in pairs(t(...)) do | |||
if r(v) ~= "number" then | |||
error("Number expected",1) | |||
end | |||
fin = fin + v | |||
end | |||
return fin | |||
end | |||
--- math.sub(...) | |||
function math.sub(...) | |||
local fin = 0 | |||
for i,v in pairs(t(...)) do | |||
if r(v) ~= "number" then | |||
error("Number expected",1) | |||
end | |||
fin = fin - v | |||
end | |||
return fin | |||
end | |||
--- math.factorial(v) | |||
function math.factorial(v) | |||
local fin = v | |||
for i=1, v do | |||
if r(v) ~= "number" then | |||
error("Number expected",1) | |||
end | |||
if i >= v or v - i <= 0 then | |||
break | |||
end | |||
fin = fin * (v - i) | |||
end | |||
return fin | |||
end | |||
--- math.mul(...) | |||
function math.mul(...) | |||
local fin = 0 | |||
for i,v in pairs(t(...)) do | |||
if r(v) ~= "number" then | |||
error("Number expected",1) | |||
end | |||
fin = fin * v | |||
end | |||
return fin | |||
end | |||
--- math.div(...) | |||
function math.div(...) | |||
local fin = 0 | |||
for i,v in pairs(t(...)) do | |||
if r(v) ~= "number" then | |||
error("Number expected",1) | |||
end | |||
fin = fin / v | |||
end | |||
return fin | |||
end | |||
--- string.concat(...) | |||
function string.concat(...) | |||
local fin = "" | |||
for i,v in pairs(t(...)) do | |||
if r(v) ~= "string" then | |||
error("String expected",1) | |||
end | |||
fin = fin .. tostring(v) | |||
end | |||
return fin | |||
end | |||
--- math.concat(...) | |||
function math.concat(...) | |||
local fin = "" | |||
for i,v in pairs(t(...)) do | |||
if r(v) ~= "number" and r(v) ~= "string" then | |||
error("String or Number expected",1) | |||
end | |||
fin = fin .. tostring(v) | |||
end | |||
return fin | |||
end | |||
</pre> | |||
==CustomEvent library== | |||
This library _should_ allow you to create, fire, connect, etc. custom events. | |||
<pre> | |||
local g = {} | |||
local events = { | |||
-- eventName = { firstConnections, connections, lastConnections } | |||
} | |||
function g:CreateEvent(eventName) | |||
if not eventName then | |||
error("String expected for eventName when calling CreateEvent",1) | |||
end | |||
if events[tostring(eventName)] ~= nil then | |||
error("There is already a customevent with name " .. '"' .. tostring(eventName) .. '".',1) | |||
end | |||
events[tostring(eventName)] = {{},{},{}} | |||
function events[tostring(eventName)]:connect(func) | |||
local index = #events[2]+1 | |||
local r = {} | |||
function r:disconnect() | |||
events[2][index] = nil | |||
end | |||
r.connected = false | |||
events[2][#events[2]+1] = {func,r} | |||
return r | |||
end | |||
function events[tostring(eventName)]:connectFirst(func) | |||
local index = #events[1]+1 | |||
local r = {} | |||
function r:disconnect() | |||
events[1][index] = nil | |||
end | |||
r.connected = false | |||
events[1][#events[1]+1] = {func,r} | |||
return r | |||
end | |||
function events[tostring(eventName)]:connectLast(func) | |||
local index = #events[3]+1 | |||
local r = {} | |||
function r:disconnect() | |||
events[3][index] = nil | |||
end | |||
r.connected = false | |||
events[3][#events[3]+1] = {func,r} | |||
return r | |||
end | |||
function events[tostring(eventName)]:fire() | |||
for i,v in pairs(events[1]) do | |||
v[2].connected=true | |||
v[1]() | |||
v[2].connected=false | |||
end | |||
for i,v in pairs(events[2]) do | |||
v[2].connected=true | |||
v[1]() | |||
v[2].connected=false | |||
end | |||
for i,v in pairs(events[3]) do | |||
v[2].connected=true | |||
v[1]() | |||
v[2].connected=false | |||
end | |||
end | |||
end | |||
function g:GetEvent(eventName) | |||
return events[tostring(eventName)] or nil | |||
end | |||
return g | |||
-- This may not work yet... | |||
</pre> |
Revision as of 20:56, 30 March 2011
Some useful functions...
--- shared:RealType( v ) function shared:RealType(v) local libraries = {Axes, BrickColor, CFrame, Color3, Faces, Ray, Region3, UDim, UDim2, Vector2, Vector3, Instance, math, table, string, coroutine} if type(v) == "userdata" then for _,i in pairs(libraries) do if v == i then return string.lower(tostring(i)) end end if pcall(function() i = v:IsA("Instance") end) then return "object" elseif pcall(function() x = v.X end) then if pcall(function() z = v.Z end) then if Axes.new(v.X, v.Y, v.Z) == v then return "axes" elseif pcall(function() comp = v:components() end) then return "coordinateframe" else return "vector3" end elseif Vector2.new(v.X, v.Y) == v then return "vector2" elseif pcall(function() xo = v.X.Offset end) then return "universaldimension2" end elseif pcall(function() r = v.r end) then if BrickColor.new(v.r, v.g, v.b) == v then return "brickcolor" elseif Color3.new(v.r, v.g, v.b) == v then return "color3" end elseif pcall(function() top = v.Top) then if Faces.new(v.Back, v.Bottom, v.Front, v.Left, v.Right, v.Top) == v then return "faces" end elseif pcall(function() connect = v.connected end) then return "rbxscriptconnection" elseif pcall(function() conn = v:connect(function()end)end) then return "rbxscriptsignal" elseif pcall(function() origin = v.Origin end) then return "ray" elseif pcall(function() cf = v.CFrame end) then return "region3" elseif pcall(function() o = v.Offset end) then return "universaldimension" end end return type(v) end --- shared:ToupleToTable( ... ) function shared:ToupleToTable(...) assert(loadstring("return {" .. ... .. "}"))() end function r(v) -- for me return shared:RealType(v) end --- math.curt(v) function math.curt(v) if r(v) ~= "number" then error("Number expected",1) end return v^(1/3) end function t(...) -- for me return shared:ToupleToTable(...) end --- math.add(...) function math.add(...) local fin = 0 for i,v in pairs(t(...)) do if r(v) ~= "number" then error("Number expected",1) end fin = fin + v end return fin end --- math.sub(...) function math.sub(...) local fin = 0 for i,v in pairs(t(...)) do if r(v) ~= "number" then error("Number expected",1) end fin = fin - v end return fin end --- math.factorial(v) function math.factorial(v) local fin = v for i=1, v do if r(v) ~= "number" then error("Number expected",1) end if i >= v or v - i <= 0 then break end fin = fin * (v - i) end return fin end --- math.mul(...) function math.mul(...) local fin = 0 for i,v in pairs(t(...)) do if r(v) ~= "number" then error("Number expected",1) end fin = fin * v end return fin end --- math.div(...) function math.div(...) local fin = 0 for i,v in pairs(t(...)) do if r(v) ~= "number" then error("Number expected",1) end fin = fin / v end return fin end --- string.concat(...) function string.concat(...) local fin = "" for i,v in pairs(t(...)) do if r(v) ~= "string" then error("String expected",1) end fin = fin .. tostring(v) end return fin end --- math.concat(...) function math.concat(...) local fin = "" for i,v in pairs(t(...)) do if r(v) ~= "number" and r(v) ~= "string" then error("String or Number expected",1) end fin = fin .. tostring(v) end return fin end
CustomEvent library
This library _should_ allow you to create, fire, connect, etc. custom events.
local g = {} local events = { -- eventName = { firstConnections, connections, lastConnections } } function g:CreateEvent(eventName) if not eventName then error("String expected for eventName when calling CreateEvent",1) end if events[tostring(eventName)] ~= nil then error("There is already a customevent with name " .. '"' .. tostring(eventName) .. '".',1) end events[tostring(eventName)] = {{},{},{}} function events[tostring(eventName)]:connect(func) local index = #events[2]+1 local r = {} function r:disconnect() events[2][index] = nil end r.connected = false events[2][#events[2]+1] = {func,r} return r end function events[tostring(eventName)]:connectFirst(func) local index = #events[1]+1 local r = {} function r:disconnect() events[1][index] = nil end r.connected = false events[1][#events[1]+1] = {func,r} return r end function events[tostring(eventName)]:connectLast(func) local index = #events[3]+1 local r = {} function r:disconnect() events[3][index] = nil end r.connected = false events[3][#events[3]+1] = {func,r} return r end function events[tostring(eventName)]:fire() for i,v in pairs(events[1]) do v[2].connected=true v[1]() v[2].connected=false end for i,v in pairs(events[2]) do v[2].connected=true v[1]() v[2].connected=false end for i,v in pairs(events[3]) do v[2].connected=true v[1]() v[2].connected=false end end end function g:GetEvent(eventName) return events[tostring(eventName)] or nil end return g -- This may not work yet...