multidimensional tables/arrays

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
dbltnk
Prole
Posts: 21
Joined: Thu Dec 10, 2009 8:23 pm

Re: multidimensional tables/arrays

Post by dbltnk »

Thanks for the help, kikito and Robin. This worked. Now that I've gotten around that obstacle I've run into another problem. I'm trying to use FOR to iterate through my four objects but again the compiler tells me that I'm attempting to index field 'object' (a nil value). The object variable should be defined though, so I guess it's the use of . Got any idea how I messed up again? The LUA documentation didn't really solve my problem here.

Code: Select all

objects = {
		object1 = {
			type = circle,
			sprite = images.circle1, 
			x = math.random(50,350), 
			y = math.random(50,350), 
			s = math.random(50,150)/100},
		object2 = {
			type = circle,
			sprite = images.circle2, 
			x = math.random(50,350), 
			y = math.random(50,350), 
			s = math.random(50,150)/100},
		object3 = {	
			type = box,
			sprite = images.box3, 
			x = math.random(50,350), 
			y = math.random(50,350), 
			s = math.random(50,150)/100},
		object4 = {
			type = box,
			sprite = images.box4, 
			x = math.random(50,350), 
			y = math.random(50,350), 
			s = math.random(50,150)/100},
	}
	
	for i = 1,4 do
		if math.random(1,10) <= 5 then
		objects.object[i].type = circle
		else
		objects.object[i].type = box
		end
	end
	
	for i = 1,4 do
		if objects.object[i].type == circle
		then
			if math.random(1,10) <= 5 then
			objects.object[i].sprite = images.circle1
			else
			objects.object[i].sprite = images.circle2
			end
		else
			if math.random(1,10) <= 5 then
			objects.object[i].sprite = images.box3
			else
			objects.object[i].sprite = images.box4
			end
		end
	end
	
	for i = 1,4 do
		if objects.object[i].sprite == images.circle1 then
		objects.object[i].r = 78*objects.box[i].s
		end
	end
	
	for i = 1,4 do
		if objects.object[i].sprite == images.circle2 then
		objects.object[i].r = 57*objects.box[i].s
		end
	end
	
	for i = 1,4 do
		if objects.object[i].sprite == images.box3 then
		objects.object[i].w = 100*objects.object[i].s
		objects.object[i].h = 100*objects.object[i].s
		end
	end
	
	for i = 1,4 do
		if objects.object[i].sprite == images.box4 then
		objects.object[i].w = 140*objects.object[i].s
		objects.object[i].h = 70*objects.object[i].s
		end
	end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: multidimensional tables/arrays

Post by bartbes »

No, your code is valid for objects = { object = { { --[[first object]]}, {--[[second object]]} } }, which isn't your situation, I'll give you two options:

Code: Select all

    objects = {
          object1 = {
             type = circle,
             sprite = images.circle1,
             x = math.random(50,350),
             y = math.random(50,350),
             s = math.random(50,150)/100},
          object2 = {
             type = circle,
             sprite = images.circle2,
             x = math.random(50,350),
             y = math.random(50,350),
             s = math.random(50,150)/100},
          object3 = {   
             type = box,
             sprite = images.box3,
             x = math.random(50,350),
             y = math.random(50,350),
             s = math.random(50,150)/100},
          object4 = {
             type = box,
             sprite = images.box4,
             x = math.random(50,350),
             y = math.random(50,350),
             s = math.random(50,150)/100},
       }
       
       for i = 1,4 do
          if math.random(1,10) <= 5 then
          objects["object" .. i].type = circle
          else
          objects["object" .. i].type = box
          end
       end
       
       for i = 1,4 do
          if objects["object" .. i].type == circle
          then
             if math.random(1,10) <= 5 then
             objects["object" .. i].sprite = images.circle1
             else
             objects["object" .. i].sprite = images.circle2
             end
          else
             if math.random(1,10) <= 5 then
             objects["object" .. i].sprite = images.box3
             else
             objects["object" .. i].sprite = images.box4
             end
          end
       end
       
       for i = 1,4 do
          if objects["object" .. i].sprite == images.circle1 then
          objects["object" .. i].r = 78*objects.box[i].s
          end
       end
       
       for i = 1,4 do
          if objects["object" .. i].sprite == images.circle2 then
          objects["object" .. i].r = 57*objects.box[i].s
          end
       end
       
       for i = 1,4 do
          if objects["object" .. i].sprite == images.box3 then
          objects["object" .. i].w = 100*objects.object[i].s
          objects["object" .. i].h = 100*objects.object[i].s
          end
       end
       
       for i = 1,4 do
          if objects["object" .. i].sprite == images.box4 then
          objects["object" .. i].w = 140*objects.object[i].s
          objects["object" .. i].h = 70*objects.object[i].s
          end
       end
or (shortened, this time)

Code: Select all

    objects = {
          {
             type = circle,
             sprite = images.circle1,
             x = math.random(50,350),
             y = math.random(50,350),
             s = math.random(50,150)/100},
          {
             type = circle,
             sprite = images.circle2,
             x = math.random(50,350),
             y = math.random(50,350),
             s = math.random(50,150)/100},
          }
       
       for i = 1,4 do
          if math.random(1,10) <= 5 then
          objects[i].type = circle
          else
          objects[i].type = box
          end
       end
User avatar
dbltnk
Prole
Posts: 21
Joined: Thu Dec 10, 2009 8:23 pm

Re: multidimensional tables/arrays

Post by dbltnk »

Thanks again. That worked and I even understand where I was wrong. Now I'm gonna fix one annoying bug and then I'll port it to 0.6.0.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests