PDA

View Full Version : 2D Arrays



LuNcHb0x
December 10th, 2005, 16:58
I was wondering how 2D arrays work because I can't find alot of information of how psp lua works.

Say I have a 2D array:

pieces = {0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}

How would I find out what is in the array?
When I try pieces[1][2] it gives me a problem saying: Attempt to index field '?' (a number value)

Any help with this would be appreciated.

b8a
December 10th, 2005, 21:52
Hey there LuNcHbOx...

I haven't tried my hand at lua yet, but looking at your sample code, one possibility springs immediately to mind (drawing from my experience with other programming/scripting languages)

1)"pieces" is an array with four nested arrays, right? If it is then you need to initialize pieces as an array, as well as the four nested arrays. i.e. pieces should be defined as (changed code is in bold font):
pieces = {{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}}

In other words, those four sets of brackets need to be in an all-incompasing set of brackets themselves (hence the term "nested array" -- all array values are "nested" in the array brackets, so any array which is in itself another array's value is a "nested array")
in JavaScript you would define it this way:
pieces = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]];

and then when you try to acces pieces[1][2], it will return the correct value. (Except that JS arrays are 0-based and lua arrays appear to be 1-based, but aside from that technical difference, which results in a different value being returned, I expect that the underlying methoud for storing and accessing values is pretty much the same.)

I found basic lua programming instructions at:
http://www.lua.org/pil/

with specific information about lua arrays at:
http://www.lua.org/pil/11.1.html
...Doesn't look like it has any information directly pertaining to your problem, but I hope what I wrote above will help solve it for you. If it doesn't, my appologies!! Does anyone else out there have any input to solve this problem? As soon as I can find information about running lua in OSX I hope to have a go at the language myself and it would be nice to know about any stumbling blocks in the coding process beforehand.

Good luck

LuNcHb0x
December 11th, 2005, 17:37
Thanks for your reply it helped me figure out the problem. I wasn't even thinking that anything was wrong with the array declaration, because I assumed that it would give me an error if there was something wrong with it. I'm not used to declaring whole arrays at once, like in C i'll make and array like int pieces[4][4] and then have two nested for loops to put in the necessary values, I guess my way is slower but it's just the way i'm used to.

As for getting lua to work on OSX..
If your just trying to make it so you can make lua apps for the psp all you have to do is get your computer to recognize the psp and put lua player on there and then make a new file with the extension of .lua and put it in a new folder in the applications folder of lua player on your psp and you will be able to see it on your psp.
If you're trying to get lua to run in OSX I am no help there, I don't know much about OSX or lua on OSX.

Oh and about those stumbling blocks...
I've found out that if you make functions with capital letters on the psp lua like:
Function ValidateMove()

End

It will give you an error because Function and End have to be lowercase, very annoying.

Thanks for your help again.

b8a
December 11th, 2005, 19:12
Glad I could be of help. It sounds like you have other programming experience, so I hope my reply didn't sound too pandering. You just never know the experience level of the person on the other end.

And thanks for the info in your reply. I had been looking for a lua player that runs in OSX, but what you mentioned makes more sense since the functions available for the player are probably going to differ from platform to platform. So, if I'm targeting the PSP anyway, I'm deffinately better off using the native player to test my scripts.

I also had a hard time clarifying whether or not lua scripts have to be compiled. I know that most scripting languages that I have had any experience with generally run from plain text files and I expected lua to do the same, but, like I said, I was having a hard time finding a deffinitive answer. Thanks for straightening that out for me.