Learning lua (Thread formerly: Help resolv...
L5132 16 May 2012
Spoiler
This code I wrote is throwing the error " doorlock2:176: vm error:java.lang.ArrayIndexOutOfBoundsException: 256"
I would like help from people who have been working with this longer than I have to resolve it. I am running CC on minecraft 1.1 as of now since I have been running a Tekkit server since it's easier for my friends (Read: noobs) to join. Otherwise, I would not be using it, as I don't want or need a lot of what they threw in.
Anyways, the program is intended to use bundled cable on the back of the computer to control all 16 colors of insulated wire to some redpower AND gates which would open iron doors only if the computer is sending a signal and the button by the door is pressed. I would also probably switch to a password protected computer for the second signal at some point.
I've written a few other programs but this is the first time I've used tables to store variables, so if you could also help with the coding I would appreciate it.
I've been using Notepad++ to code it, its definitely much easier than the in game editor />
Also, I have been looking into it with various searches and didn't come up with much, however I am fairly sure it is simply an error with how I coded something. Thanks, L5132
Cloudy 16 May 2012
The issue is, you are running the changeDoor() function within changeDoor() - and you may be doing that fast, filling the Lua stack of functions over its limit.
Try refactoring the code so that you don't have to run changeDoor() inside changeDoor().
Try refactoring the code so that you don't have to run changeDoor() inside changeDoor().
L5132 16 May 2012
Ah, I see where I messed up. I was going to include sleep functions, would that prevent this as well?
MysticT 16 May 2012
No, using sleep wouldn't prevent that error. You need to use a loop instead of recursive functions (functions calling themselves).
So, instead of doing something like:
So, instead of doing something like:
local function doSomething() -- do stuff doSomething() endYou should do:
local function doSomething() while true do -- do stuff end end
L5132 16 May 2012
Spoiler
So I revised the code, and it has also solved my original question. However, I am now encountering an error the previous error had convinced me I had solved />This is that on line 176, in the changeDoor() function, specifically in table.insert() I am using two variables to edit the table with the updated values, hopefully causing it to pull the new states for the redwire. However this appears to be something that is not allowed, and I would like to ask for help with that as well, thanks!
Cloudy 16 May 2012
What is the exact error you are getting?
Also, I'd like to point out that this line of code
Also, I'd like to point out that this line of code
elseif i == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 thenwon't do what you think it will - it will check if i = 1, and if those numbers exist - which they of course do - so it will always return true. You will be much better doing this:
elseif i >= 1 and i <= 16 then
L5132 17 May 2012
Cloudy, on 16 May 2012 - 11:38 PM, said:
What is the exact error you are getting?
Also, I'd like to point out that this line of code
Also, I'd like to point out that this line of code
elseif i == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 thenwon't do what you think it will - it will check if i = 1, and if those numbers exist - which they of course do - so it will always return true. You will be much better doing this:
elseif i >= 1 and i <= 16 then
And as to you pointing out the numbers, I had been doing what you suggested, but it threw an error, I will try it again, however. I take it that that would essentially cripple the program since it would always default to that option? Thanks for helping so quickly!
EDIT: I just edited the file and ran it, the exact error I am getting on line 175 where I changed the list of numbers to "i >= 1 and i <=16" and it is throwing the error
"doorlock:175: attempt to compare function with number"
EDIT2: I also realized what that error meant and corrected it, now I think I may be on my way to fixing this thing.
MysticT 17 May 2012
I think you forgot the quotes for the strings and the parentheses for the read() call in these lines:
... i = read -- missing () if i == lock then -- should be "lock" (with quotes) c = t[i]+1 t = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} elseif i == unlock then -- again the quotes. "unlock" t = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} elseif i == refresh then -- "refresh" runDoorCheck() elseif i == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 then -- what cloudy said, and i is a string, so compare with strings or convert to number table.insert(t,i,c) -- the if returns always true, and you assigned the function read to the variable i, that's why you get the error if t[i] >= 2 then table.insert(t,i,0) runDoorCheck() else runDoorCheck() end else print("Command Not Recognized") end end ...
L5132 17 May 2012
MysticT, on 17 May 2012 - 12:12 AM, said:
I think you forgot the quotes for the strings and the parentheses for the read() call in these lines:
Spoiler
And as to the rest of your fixes, those are probably what caused almost all of the errors. I guess that's what comes from writing the majority of this on a netbook during the school day and not being able to test until I had reached the code in the first post, lol />
L5132 17 May 2012
Ok, thanks for the help! I got the program working exactly as intended, the final code ended up being
*cringes at self for double posting*
Spoiler
As you may have noticed, being new to programming I called a ton of functions that I am sure I could have avoided with a little thought, I am wondering if I did alright at programming this. To test if it was functioning correctly, I was using bundled cable on the back splitting to each of the 16 colors of insulated wire with the corresponding colored lamp attached to give a visual for how it was working.*cringes at self for double posting*
Cloudy 17 May 2012
Hey, as far as I've concerned, if your code works you've succeeded. However, I think you under use tables. For instance, checkDoor could have been made into one function with a table defined containing the door colour. When you'd pass an argument to checkDoor it would check the correct door.
Also, checki checks each value in a table, where you could use the pairs or ipairs iterator to do it in a loop, instead of checking each value in the table manually.
Food for thought for future learning />
Also, checki checks each value in a table, where you could use the pairs or ipairs iterator to do it in a loop, instead of checking each value in the table manually.
Food for thought for future learning />
L5132 17 May 2012
Cloudy, on 17 May 2012 - 11:28 AM, said:
Hey, as far as I've concerned, if your code works you've succeeded. However, I think you under use tables. For instance, checkDoor could have been made into one function with a table defined containing the door colour. When you'd pass an argument to checkDoor it would check the correct door.
Also, checki checks each value in a table, where you could use the pairs or ipairs iterator to do it in a loop, instead of checking each value in the table manually.
Food for thought for future learning />
Also, checki checks each value in a table, where you could use the pairs or ipairs iterator to do it in a loop, instead of checking each value in the table manually.
Food for thought for future learning />