Objects & Arrays

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
MrPickle
Prole
Posts: 24
Joined: Thu Jul 10, 2008 11:43 pm
Location: United Kingdom, Lincolnshire

Objects & Arrays

Post by MrPickle »

I have this:

Code: Select all

Cube = { Square = { x = {}, y = {}, Length = 30, Height = 10 } }
I want Square to be an single dimension array, how do I do this?

I tried just assigning stuff to Cube.Square[1].x[1] but I get errors. Do I have to do something special because Square has stuff inside it?
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Objects & Arrays

Post by rude »

I think you would have to do this for that code to work:

Code: Select all

Cube.Square[1] = Cube.Square.x -- [1] and x will point to the same table.
Anything can be a key in Lua, even functions, so Cube.Square[1] is a different element from what you would think is the "first" element.

AFAIK, that is.
User avatar
mastastealth
Prole
Posts: 30
Joined: Thu Jul 03, 2008 2:44 am
Location: Barranquilla, Colombia
Contact:

Re: Objects & Arrays

Post by mastastealth »

I thought I read in the Lua manual that table order isn't necessarily linear if you don't specify the index. Like [1] wouldn't always be x, unless you did:

Square = {
[1] = x,
[2] = y,
["Length"] = 30,
["Height"] = 10 }
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Objects & Arrays

Post by surtic »

Do you mean something like

Code: Select all

Cube = { Square = {{ x = {}, y = {}, Length = 30, Height = 10 }} }
Now Cube.Square[1].x[1] is valid.
But Square is only a single-element array (and one dimensional).

It'll be easier to help if you write what you wanted to do with Cube - just write the expression you would like to use (even if it doesn't work).

Do you want to have Cube.Square[1], Cube.Square[2] etc. (if so, how many squares)? And what do you want to put in x[1], x[2], etc? My question is really - what does Cube hold? What sort of information do you want to store in Cube?
MrPickle
Prole
Posts: 24
Joined: Thu Jul 10, 2008 11:43 pm
Location: United Kingdom, Lincolnshire

Re: Objects & Arrays

Post by MrPickle »

I want to have Cube.Squares[1], etc up to 3

I have already wrote the code that fills in x & y, that works fine, but then I added cube to it because I wanted it to hold 3 squares, which will make up an isometric cube. (I wanted to see if I could draw them without using pictures, which I have done.)
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Objects & Arrays

Post by surtic »

In that case you can do:

Code: Select all

Cube = { Square = { {}, {}, {} } }
for i = 1, 3 do
  Cube.Square[i].x = {}
  Cube.Square[i].y = {}
end
And then fill the rest yourself, or create the Squares first:

Code: Select all

sq1 = {x1, y1, width1, height1}
sq2 = {x2, y2, width2, height2}
sq3 = {x3, y3, width3, height3}
Cube = { Square = { sq1, sq2, sq3 } }
Does that help? Is that what you meant?
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests