Having *unique* objects on screen

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Having *unique* objects on screen

Post by Positive07 »

I usually write my code as libraries so it lets me do testing specs and reuse lots of it later on.

I usually do the check by myself so I can provide correct errors (This is for a utf library that complements utf8):

Code: Select all

utf.sub = function (str, a, b)
	-- Default for finish is the end of the string
	local start, finish = tonumber(a), tonumber(b or -1)

	-- Type checking
	if type(str) ~= "string" then
		error("bad argument #1 to 'sub' (string expected got "..type(str)..")", 2)
	elseif type(start) ~= "number" then
		error("bad argument #2 to 'sub' (number expected got "..type(a)..")", 2)
	elseif type(finish) ~= "number" then
		error("bad argument #3 to 'sub' (number expected got "..type(b)..")", 2)
	end

	local len = u.len(str)

	-- Negative values count from the end
	-- Minimum value for start is 1 and maximum value for finish is len
	start  = math.max(start  < 0 and len + 1 + start  or start,	1)
	finish = math.min(finish < 0 and len + 1 + finish or finish,	len)

	-- Start comes after finish
	if start > finish then return "" end

	-- This lines fixes the position so that they are in bytes for string.sub
	return string.sub(str, u.offset(str, start), finish == len and -1 or u.offset(str, finish + 1) - 1) or ""
end
This error are pretty much the same as Lua errors, they handle default values and valid numbers (to Lua 10 and "10" are both valid numbers, except when the argument accepts a string in which case the value is interpreted as string and not converted to a number), you can also check multiple types and perform modifications to the data before performing the check and the thing I like the most is that 2 passed as second argument to error.

Asserts make the function error and the error report points to the line where the assertion is, so if you want to see what called that function you have to check the stack overflow, the 2 in the error function actually tells Lua to pop 1 from the stack and instead of saying that the error is there, it tells you that the error is where you called the parent function

Also note that this is my coding style for functions, functions do one thing and one thing only and as I said before, files are modules, each handle one thing and can be used, pretty much like libraries (except most of the time they depend on other modules)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Having *unique* objects on screen

Post by zorg »

Positive07 wrote:(to Lua 10 and "10" are both valid numbers, except when the argument accepts a string in which case the value is interpreted as string and not converted to a number)
To be a bit more precise, lua's not a statically typed language, so the function, nor the program will ever know what type of parameters you want to be passed into the function; as Positive07 wrote in the code snippet, he tests for types. It also won't implicitly convert from string to numbers (or vice versa even), he uses the tonumber() function, so that strings that may represent correct numbers will be converted to numbers.
Positive07 wrote:if you want to see what called that function you have to check the stack overflow.
Or just check the traceback in the error message, and don't bother the people on that site with trivial things :3

Oh, and "correct errors" are a bit of an oxymoron, i'd call it "accurate errors", but i digress.
Me and my stuff :3True 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.
User avatar
Sulunia
Party member
Posts: 203
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

Re: Having *unique* objects on screen

Post by Sulunia »

To each their own style huh.
I usually just want to avoid the "galaxy traveling nil" as i call it (when the error that happens somewhere originated on a whole other place).

Still, nice usage of the "error()" function, i actually didn't know that was possible.
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! :neko:
https://github.com/Sulunia
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Having *unique* objects on screen

Post by Positive07 »

zorg wrote:
Positive07 wrote:if you want to see what called that function you have to check the stack overflow.
Or just check the traceback in the error message, and don't bother the people on that site with trivial things :3
I prefere to keep my stack traceback clean (plus I have some code snippets in my setup that let me go to the exact file and line the error reports so pointing to the right place helps!)

I should have used accurate you are right, didn't find the word at that time.

And yes, great description of what the function is doing, entirely right! Thanks
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot], TheJayDizzle, TurboYeti and 8 guests