I don't think so, tostring() doesn't take two parameters. Most likely a recursive call, but the names differ which puzzles me.Doctory wrote:Most likely a typo, meant to be tostring.
Mini Functions Repository
Re: Mini Functions Repository
- CrackedP0t
- Citizen
- Posts: 69
- Joined: Wed May 07, 2014 4:01 am
- Contact:
Re: Mini Functions Repository
pgimeno wrote:What is stostring?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"
stostring is supposed to be bettertostring; fixed it. Thanks!pgimeno wrote:I don't think so, tostring() doesn't take two parameters. Most likely a recursive call, but the names differ which puzzles me.Doctory wrote:Most likely a typo, meant to be tostring.
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
Here, have an umlaut. Ö
- CrackedP0t
- Citizen
- Posts: 69
- Joined: Wed May 07, 2014 4:01 am
- Contact:
Re: Mini Functions Repository
Fascinating! I had no idea that format was so powerful.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 ... cificationleftpad, rightpad, luarocks, breaking the internet
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
Here, have an umlaut. Ö
Scissor the scissor!
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:
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
-- 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
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
- DanielPower
- Citizen
- Posts: 50
- Joined: Wed Apr 29, 2015 5:28 pm
Re: Mini Functions Repository
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)
-
- Prole
- Posts: 39
- Joined: Sat Nov 28, 2015 10:13 am
Re: Mini Functions Repository
Here is a large list of functions I tried creating.
Hint: The good stuff is below!
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
Last edited by ZBoyer1000 on Sun Jun 12, 2016 6:31 pm, edited 7 times in total.
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Scissor the scissor!
Mmmm... have you heard that 0.10.0 shipped with [wiki]love.graphics.intersectScissor[/wiki] which pretty much does the same thingpgimeno 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.
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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
-
- Prole
- Posts: 39
- Joined: Sat Nov 28, 2015 10:13 am
Re: Mini Functions Repository
Here is another list of more functions I tried creating.
Hint: The good stuff is below!
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)
--"t" is your table
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)
--"t" is your table
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"})
--local e = pocket("receive",{"main",main},{"name here"})
--pocket("replace",{"main",main},{"name here",table here})
--"t" is your table
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
elseif action == "receive" then
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
Last edited by ZBoyer1000 on Tue Jun 14, 2016 1:08 am, edited 2 times in total.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Mini Functions Repository
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;
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Scissor the scissor!
Eep, missed that one! Thanks!Positive07 wrote: Mmmm... have you heard that 0.10.0 shipped with [wiki]love.graphics.intersectScissor[/wiki] which pretty much does the same thing
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.
Who is online
Users browsing this forum: Majestic-12 [Bot] and 4 guests