Page 1 of 2

How to handle collision resolutions?

Posted: Sun Jul 01, 2012 1:41 am
by onedaysnotice
||===========onedaysnotice help thread============||

**I moved the thread to this board since its more appropriate :D**

Before i infuriate the whole love2d community with my incessant making of 'help me' and 'how to' threads, I've decided to create a centralised thread xD

Current problem: viewtopic.php?p=60456#p60456

------------------------------------------------------------------------------------------------------------

Issue #1
--------------------------------
STATUS: resolved.

if you remove elements of a table using table.remove(), does the next existing element become index 1, or does it remain the same index and new elements will just replace the empty indexes?

like if I have a table:

a = {}
a[1] = 4
a[2] = 2
a[3] = 7

if I remove a[1] and a[2], does a[3] become index 1 afterwards?

thanks :D Sorry for the noob question xD.

Re: table.remove question

Posted: Sun Jul 01, 2012 1:46 am
by TechnoCat
onedaysnotice wrote:if I remove a[1] and a[2], does a[3] become index 1 afterwards?
http://codepad.org/dd5AWIeT

Code: Select all

sample = {"hello", "felicia", "day"}
print(sample[1])
table.remove(sample,1)
table.remove(sample,1)
print(sample[1])

Code: Select all

hello
day

Re: table.remove question

Posted: Sun Jul 01, 2012 1:57 am
by onedaysnotice
TechnoCat wrote:
onedaysnotice wrote:if I remove a[1] and a[2], does a[3] become index 1 afterwards?
http://codepad.org/dd5AWIeT

Code: Select all

sample = {"hello", "felicia", "day"}
print(sample[1])
table.remove(sample,1)
table.remove(sample,1)
print(sample[1])

Code: Select all

hello
day
ok good. thanks :D

finally had the sense to check the tables library tutorial at:
http://lua-users.org/wiki/TableLibraryTutorial

and it clearly states "The remaining elements are reindexed sequentially and the size of the table is updated to reflect the change."

*facepalm*. Sorry for the stupid questions guys :S.

Issue #2

Posted: Sun Jul 01, 2012 5:04 am
by onedaysnotice
STATUS: resolved.


How can you print the value of input_time in this table?

Code: Select all

table.insert(box[i].keyCompare, {input_time = hidden_timer})
I've tried:

Code: Select all

	for i = 1,2 do 
		if #box[i].keyCompare.input_time > 0 then
			table.foreachi(box[i].keyCompare.input_time, print)
		end
	end

Code: Select all

	for i = 1,2 do
		if #box[i].keyCompare.inputTimer > 0 then
			love.graphics.print(box[i].keyCompare.inputTimer[1], 100, 170)
			if #box[i].keyCompare.inputTimer > 1 then
				love.graphics.print(box[i].keyCompare.inputTimer[2], 100, 170)
			end
		end
	end
and

Code: Select all

	for j = 1,2 do
		for i,v in ipairs(box[j].keyCompare.input_time) do
			love.graphics.print(v.keyCompare.input_time, 100, 170)
		end
	end
but all of them have either gotten nil error, or just printed the table id in terminal. :S

Re: how to print the values inside a sub, sub, sub table? lo

Posted: Sun Jul 01, 2012 9:47 am
by bartbes
You shouldn't use table.foreach, btw, that's deprecated.

Code: Select all

for i, v in ipairs(box[i].keyCompare) do
    print(v.input_time)
end
If you check your table.insert you can see you're inserting into box.keyCompare, so that's what you should be interating over.

Re: how to print the values inside a sub, sub, sub table? lo

Posted: Sun Jul 01, 2012 10:10 am
by onedaysnotice
bartbes wrote:You shouldn't use table.foreach, btw, that's deprecated.

Code: Select all

for i, v in ipairs(box[i].keyCompare) do
    print(v.input_time)
end
If you check your table.insert you can see you're inserting into box.keyCompare, so that's what you should be interating over.


I tried plugging that in, but it just gets the nil value error as well :S

Re: how to print the values inside a sub, sub, sub table? lo

Posted: Sun Jul 01, 2012 10:23 am
by Zeliarden
Try this one. It uses a "table to string" to show what happens when you press keys. Do you realy need a table for input_time?

Re: how to print the values inside a sub, sub, sub table? lo

Posted: Sun Jul 01, 2012 10:07 pm
by onedaysnotice
Zeliarden wrote:Try this one. It uses a "table to string" to show what happens when you press keys. Do you realy need a table for input_time?
Oh yay :D That works perfectly :D Thanks so much!

and ummm...I probably don't need to but...idk xD

hrmm to continue with this version or my new simplified one...

EDIT: Now that I can see the values of the input_time, it looks like keyCompare is not doing what I want it to. So I guess I might as well just stick to my simplified version.

Issue #3 - How to make a wait function?

Posted: Wed Jul 04, 2012 7:31 am
by onedaysnotice
STATUS: Scrapped.

I am trying to make a wait function in an external file that outputs false until the specified time, at which it becomes true. I want it to be easily integrateable to any part of any code. I am almost certain that there is a much, much easier way of doing this but this is what I've got so far. (Hint: It doesn't work xD)

Code: Select all

local time = {}
time.__index = time

function newWait(seconds)
	local t = {}
	t.elapsed = 0
	t.total = seconds
	t.waiting = false
end

function time:update(dt)
	if self.waiting then
		self.elapsed = self.elapsed + dt
		if self.elapsed >= self.total then 
			return true end
			self.elapsed = 0
			self.waiting = false
		end
	end
end

function time:reset()
	self.elapsed = 0
end

function time:wait()
   self:reset()
	self.waiting = true
end
I have no idea what most of the code means btw, I was just tailoring Bartbes' AnAL code to suit this function lmao.

Can you please tell me how to change the code so that it works, or an easier method of solving this issue? :)

Re: How to make a Wait function? :s

Posted: Wed Jul 04, 2012 7:47 am
by Robin
First:

Code: Select all

function newWait(seconds)
   local t = setmetatable({}, time) -- you need to set the metatable of t to time
   t.elapsed = 0
   t.total = seconds
   t.waiting = false
   return t -- if you don't return the table, you can't use it
end
Fun fact, you can make the above one line:

Code: Select all

function newWait(seconds)
   return setmetatable({elapsed = 0, total = seconds, waiting = false}, time)
end
Secondly, time.update should be different:

Code: Select all

function time:update(dt)
   if self.waiting then
      self.elapsed = self.elapsed + dt
      if self.elapsed >= self.total then
         self.elapsed = 0
         self.waiting = false
         return true -- NOT "return true end", that makes no sense
                     -- also notice you need to return AFTER you reset those properties, not before
      end
   end
end