[SOLVED] List + Math

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.
Post Reply
User avatar
var77
Citizen
Posts: 52
Joined: Wed May 02, 2018 1:57 pm

[SOLVED] List + Math

Post by var77 »

Hi everyone well i'm back here with an interesting problem about list (array).

Well i try to test something that look like that:

Code: Select all

--on load
nblist=0

--on update
list={}
var1 = 0
var2 = 0
var1max = 100
var2max = 100

if var1 < var1max then var1 = var1 + 1 end
if var2 < var2max then var2 = var2 + 1 end

if var1 > var1max then 
	var1 = var1max
	table.insert(list, newobj) 
	if nblist < 3 then nblist = nblist +1 end
end
if var2 > var2max then 
	var1 = var1max
	table.insert(list, newobj)
	if nblist < 3 then nblist = nblist +1 end
end

--on update graphics
if nblist > 0 then
	for i, obj in ipairs(nblist) do		
		yvariable = 15*i	
		lg.print(tostring(nblist[i]),100,100+yvariable)
	end
end
My issue on this is that the result isn't what i expected.
I only got 1 object in my list. If two object got the same
result at the same time they aint both add in the list
only one of them.

How do you think we can solve this to have both of them in the list ?
For example in race if both arrived at the same time how can we handle it ?
(with a precise timer (0.0000001s) maybe ?)

Thanks in advance :)
Last edited by var77 on Tue Sep 25, 2018 4:05 pm, edited 1 time in total.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: List + Math

Post by grump »

Code: Select all

if var1 < var1max then var1 = var1 + 1 end
...
if var1 > var1max then
...
var1 can never be greater than var1max with this code.

Code: Select all

if var2 > var2max then 
	var1 = var1max
You probably meant to write var2 = var2max here.

Also, you can query the length of an array with #list. No need to track the length separately.

Code: Select all

-- update
var1 = math.min(var1max, var + 1)
if var1 == var1max then
	table.insert(list, newobj)
end

-- draw
if #list > 0
...
end

-- no need to check for #list > 0, you can simply write
for i, obj in ipairs(list) do
...
end
User avatar
var77
Citizen
Posts: 52
Joined: Wed May 02, 2018 1:57 pm

Re: List + Math

Post by var77 »

thanks @grump for answering fast.

Yes i made some mistake with my previous code.
So i add here the new one without mistake you noticed.

Code: Select all

--on load
var1 = 0
var2 = 0
var1max = 100
var2max = 100
list={}

--on update
if var1 < var1max then
 var1 = var1 + 1 
end
if var2 < var2max then 
var2 = var2 + 1 
end

if var1 > var1max then 
	var1 = var1max
	table.insert(list, newobj) 	
end
if var2 > var2max then 
	var2 = var2max
	table.insert(list, newobj)	
end

--on update graphics
for i, obj in ipairs(list) do	
	yvariable = 15*i	
	lg.print(tostring(nblist[i]),100,100+yvariable)
end
So my result is that each variable goal to their max value
but not both of them were add in the list if they reach it at the same time.

Is that normal or did i mess with something ?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: List + Math

Post by grump »

There shouldn't be anything in the list. See my previous answer.
User avatar
var77
Citizen
Posts: 52
Joined: Wed May 02, 2018 1:57 pm

Re: List + Math

Post by var77 »

ok so i totaly mess and i miss something important sorry grump

this new one correct a lot and print what i expected

Code: Select all

--on load
var1 = 0
var2 = 0
var1max = 100
var2max = 100
list = {}

-- update
if #list < 2 then
	var1 = math.min(var1max, var1 + 1)
	if var1 == var1max then	
		newobj = {id="1"}
		table.insert(list, newobj)
	end
	var2 = math.min(var2max, var2 + 1)
	if var2 == var2max then
		newobj = {id="2"}
		table.insert(list, newobj)
	end
end

--on update graphics
lg.print(tostring(var1),10,10)
lg.print(tostring(var2),10,50)	
lg.print(tostring(#list),50,50)	

for i, obj in ipairs(list) do	
	yvariable = 15*i	
	lg.print(tostring(list[i].id),10,100+yvariable)
end
thanks for all
Post Reply

Who is online

Users browsing this forum: No registered users and 74 guests