Page 3 of 5

### Re: Mini Functions Repository

Posted: Sun May 01, 2016 3:52 pm
Doctory wrote:Most likely a typo, meant to be tostring.
I don't think so, tostring() doesn't take two parameters. Most likely a recursive call, but the names differ which puzzles me.

### Re: Mini Functions Repository

Posted: Tue May 03, 2016 12:13 am
pgimeno wrote:
CrackedP0t wrote:Here's a nice function that turns any value except for threads, functions, and (probably) userdata into Lua and human-readable strings:

Code: Select all

``````[...]
s = s .. string.rep(indent, depth + 1) .. "[" .. stostring(k) .. "]" .. " = " .. stostring(v, depth + 1) .. ",\n"
``````
What is stostring?
pgimeno wrote:
Doctory wrote:Most likely a typo, meant to be tostring.
I don't think so, tostring() doesn't take two parameters. Most likely a recursive call, but the names differ which puzzles me.
stostring is supposed to be bettertostring; fixed it. Thanks!

### Re: Mini Functions Repository

Posted: Tue May 03, 2016 12:16 am
Inny wrote:
While hilarious, let me just set the record straight that string.format is what you want to use for your string padding. The format codes are a little weird at first and I always have to look them up myself: https://en.wikipedia.org/wiki/Printf_fo ... cification
Fascinating! I had no idea that format was so powerful.

### Scissor the scissor!

Posted: Sat May 07, 2016 12:39 pm
This little function applies a scissor to the current scissor, so that the new one is the intersection of the rectangles for the old scissor and the new scissor, and returns the previous scissor:

Code: Select all

``````-- Apply a scissor to the current scissor (intersect the rects)
function clipScissor(nx, ny, nw, nh)
local ox, oy, ow, oh = love.graphics.getScissor()
if ox then
-- Intersect both rects
nw = nx + nw
nh = ny + nh
nx, ny = math.max(nx, ox), math.max(ny, oy)
nw = math.max(0, math.min(nw, ox + ow) - nx)
nh = math.max(0, math.min(nh, oy + oh) - ny)
end
-- Set new scissor
love.graphics.setScissor(nx, ny, nw, nh)
-- Return old scissor
return ox, oy, ow, oh
end
``````
This is handy in all situations where a previous scissor must be respected, but a new scissor must be applied. For example, think of a small, scrollable GUI window inside the main LÖVE window. If you want to apply a scissor to something inside the scrollable window (for example an edit control), the new scissor would overwrite the previous scissor, possibly drawing outside the window. The above function prevents this. Example usage:

Code: Select all

``````function edit:drawText()
local ox, oy, ow, oh = clipScissor(self.x, self.y, self.w, self.h)
love.graphics.print(self.text, self.x - self.startVisible, self.y)
love.graphics.setScissor(ox, oy, ow, oh) -- restore previous scissor
end
``````

### Re: Mini Functions Repository

Posted: Fri May 20, 2016 12:58 pm
It's super simple, but I end up using this in everything because I just love dealing with 2-dimensional arrays.

Code: Select all

``````local zArray = {}

function zArray.set(array, x, y, value)
if not array[x] then array[x] = {} end
array[x][y] = value
end

function zArray.check(array, x, y)
if array[x] then
return(array[x][y])
end
end

function zArray.length(array)
local len = 0
for x in pairs(array) do
for y in pairs(array[x]) do
if array[x][y] ~= nil then
len = len + 1
end
end
end
return(len)
end

_zArray = zArray
return(zArray)
``````

### Re: Mini Functions Repository

Posted: Sun Jun 12, 2016 4:15 pm
Here is a large list of functions I tried creating.
Hint: The good stuff is below!

Code: Select all

``````seeds={}
function createSeed(name,n)
for i = 1,#seeds do
if seeds[i][1] == name and seeds[i][2] == n then print("Failed to create a new seed.") return end
end
seeds[#seeds+1] = {name,n}
print("Seed: "..name..":"..n.." is created.")
end

function setSeed(name)
for i = 1,#seeds do
if seeds[i][1] == name then print("Seed: "..name.." is set.") math.randomseed(seeds[i][2]) return end
end
print("Failed to set a seed.")
end

function removeSeed(name)
for i = 1,#seeds do
if seeds[i][1] == name then print("Seed: "..name.." is removed.") table.remove(seeds,i) return end
end
print("Failed to remove a seed.")
end

function math.digits(number)
return string.len(tostring(number))
end

--"n" is any number you want to put
--Make sure your results don't become too large
--Results in a greatly "random" number
function math.erratic(n)
local c = os.clock()
local t = os.time()
local n = math.abs(n) / 10
local num = math.floor(((c%t)%(t%c))^n)
return math.random(-num,num)
end

--Puts "c" between every character in "name"
function string.space(name,c)
local s = ""
for i = 1,#name do
s=s..string.sub(name,i,-#name+(i-1))
if i ~= #name then s=s..c end
end
return s
end

--Finds a single character where n tells it to be
function string.getChar(name,n)
return string.sub(name,n,-#name+(n-1))
end

--Finds a single character, "c", and returns it's position in a string
function string.getPlace(name,c)
for i = 1,#name do
if string.sub(name,i,-#name+(i-1)) == c then return i end
end
end

--"Lifts" "name" with "c" until "n" equals the amount of "name"
--"Side" can be "left" or "right"
function string.lift(name,c,n,side)
local s = ""
if #name ~= n then
for _ = 1,n-#name do s=s..c end
end
if side == "left" then
return s..name
elseif side == "right" then
return name..s
end
end

--Warps a string if the amount of characters exceeds "n"
function string.limit(name,n)
local s = {"",0}
for i = 1,#name do
s[2]=s[2]+1
if s[2] > n then s[2]=0 s[1]=s[1].."\n" end
s[1]=s[1]..string.sub(name,i,-#name+(i-1))
end
return s[1]
end

--Separates a string every time there is a gap between characters
function string.spaceSeparate(name)
local s = ""
for i = 1,#name do
local char = string.sub(name,i,-#name+(i-1))
if char == " " then s=s.."\n"
else s=s..char end
end
return s
end

function string.corrupt(name,seed)
local s = {"",{}}
math.randomseed(seed)
for i = 1,#name do
s[2][i]=string.sub(name,i,-#name+(i-1))
s[2][i],s[2][math.random(i)]=s[2][math.random(i)], s[2][i]
s[1]=s[1]..s[2][i]
end
return s[1]
end

function string.shuffle(name,seed)
local s = {"",{}}
math.randomseed(seed)
for i = 1,#name do s[2][i] = string.sub(name,i,-#name+(i-1)) end
for i = 1,#s[2] do
local r = math.random(i)
local n = {s[2][i],s[2][r]}
s[2][i] = n[2]
s[2][r] = n[1]
end
for i = 1,#s[2] do s[1] = s[1] .. s[2][i] end
return s[1]
end

--"Scrolls" "name" by the amount of "n"
--"a" is the amount of text viewable
function string.scroll(name,n,a)
while (math.abs(n)-1)+math.abs(a) > #name do
n=n-1
end
if math.abs(a) > #name then a = #name end
return string.sub(name,n,-#name+(n-2)+math.abs(a))
end
``````

### Re: Scissor the scissor!

Posted: Sun Jun 12, 2016 4:38 pm
pgimeno wrote:This little function applies a scissor to the current scissor, so that the new one is the intersection of the rectangles for the old scissor and the new scissor, and returns the previous scissor.
Mmmm... have you heard that 0.10.0 shipped with [wiki]love.graphics.intersectScissor[/wiki] which pretty much does the same thing

Code: Select all

``````function edit:drawText()
local ox, oy, ow, oh = love.graphics.getScissor()
love.graphics.intersectScissor(self.x, self.y, self.w, self.h)
love.graphics.print(self.text, self.x - self.startVisible, self.y)
love.graphics.setScissor(ox, oy, ow, oh) -- restore previous scissor
end
``````

### Re: Mini Functions Repository

Posted: Sun Jun 12, 2016 5:59 pm
Here is another list of more functions I tried creating.
Hint: The good stuff is below!

Code: Select all

``````function valueToKey(t,i)
for k,v in pairs(t) do
if v == i then
return k
end
end
return "nil"
end

function keyToValue(t,i)
for k,v in pairs(t) do
if k == i then
return v
end
end
return "nil"
end

--Returns a converted table. It does variables also.
--You can display your table with print(returnTable(table name,"table name"))
--If "name" is nil then the name of the table will be tostring(table name)
function returnTable(t,name)
local c = {"",""}
for k,v in next,t do
if type(k) ~= "number" then
c[1]=k.."=" else c[1]="" end
if type(v) == "number" then
c[1]=c[1]..v.."," c[2]=c[1]..c[2]
elseif type(v) == "string" then
c[1]=c[1].."\""..v.."\"," c[2]=c[1]..c[2]
elseif type(v) == "boolean" or type(v) == "userdata" then
c[1]=c[1]..tostring(v).."," c[2]=c[1]..c[2]
elseif type(v) == "table" then
c[1]=returnTable(v,k).."," c[2]=c[1]..c[2]
end
end
if type(name)~="number" then name=(name or tostring(t)).."=" else name = "" end
return name.."{"..string.sub(c[2],1,-2).."}"
end

--if "key" is true then anything with a key that is not a number will be listed
--if "key" is false then anything with a key that is a number will be listed
--"name" is the type of the object you want to list (Some are not compatible with this)
function returnList(t,key,name)
local c = {}
for k,v in pairs(t) do
function s()
if name == "key" then c[#c+1] = k
elseif name == "number" and type(v) == "number" then c[#c+1] = v
elseif name == "table" and type(v) == "table" then c[#c+1] = returnTable(v,k)
elseif name == type(v) then c[#c+1] = tostring(v)
end
end
if key == true and type(k) ~= "number" or key == false and type(k) == "number" then s() end
end
return c
end

--This organizes your memory if desired so that it is easier to create your game.
--"action" can be "create","recieve","replace", or "remove"
--main = {}
--pocket("create",{"main",main},{"name here",table here})
--pocket("remove",{"main",main},{"name here"})
--pocket("replace",{"main",main},{"name here",table here})
function pocket(action,t,s)
local var = 0
for i = 1,#t[2] do
local a,b = string.gsub(tostring(t[2][i][0]),"PocketName=","")
if a == s[1] then var = i end
end
if action == "create" then
if var == 0 then
s[2][0] = "PocketName=" .. s[1]
s[2][-1] = "PocketId=" .. string.gsub(tostring(s[2]),"table:","")
s[2][-2] = "PocketDate=" .. os.date("%c")
t[2][#t[2]+1] = s[2]
print("Pocket: \""..s[1].."\" is created in \""..t[1].. "\"| "..os.date("%c"))
else print("Pocket: \""..s[1].."\" is already present in \""..t[1].."\"") end
if var ~= 0 then
print("Pocket: \""..s[1].."\" is received from \""..t[1].. "\"| "..os.date("%c")) return t[2][var]
else print("Pocket: \""..s[1].."\" can't be received from \""..t[1].. "\"") end
elseif action == "replace" then
if var ~= 0 then
print("Pocket: \""..s[1].."\" is replaced in \""..t[1].. "\"| "..os.date("%c"))  t[2][var] = s[2]
else print("Pocket: \""..s[1].."\" can't be replaced in \""..t[1].. "\"") end
elseif action == "remove" then
if var ~= 0 then
print("Pocket: \""..s[1].."\" is removed from \""..t[1].. "\"| "..os.date("%c")) table.remove(t[2],var)
else print("Pocket: \""..s[1].."\" can't be removed from \""..t[1].. "\"") end
end
end
``````

### Re: Mini Functions Repository

Posted: Sun Jun 12, 2016 6:26 pm
Fastest directed rounding functions:

Code: Select all

``````-- math.floor rounds to negative infinity, built-in function
-- math.ceil rounds to positive infinity, built-in function
math.trunc = function(n) return n >= 0.0 and n-n% 1 or n-n%-1 end -- rounds towards zero from both infinities
math.round = function(n) return n >= 0.0 and n-n%-1 or n-n% 1 end -- rounds away from zero, towards both infinities -- better name needed :3
--math.integer would be round to nearest integer with special tie breaking rules for the .5 case;
``````

### Re: Scissor the scissor!

Posted: Mon Jun 13, 2016 1:03 am
Positive07 wrote: Mmmm... have you heard that 0.10.0 shipped with [wiki]love.graphics.intersectScissor[/wiki] which pretty much does the same thing
Eep, missed that one! Thanks!

I created it for Gspöt, which aims to remain compatible with 0.9, so unfortunately I can't take advantage of it for that project.