Jump to content




Learning lua (Thread formerly: Help resolving lua VM error)

help

11 replies to this topic

#1 L5132

  • New Members
  • 9 posts

Posted 16 May 2012 - 10:26 PM

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 :P/>
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

#2 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 16 May 2012 - 10:33 PM

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().

#3 L5132

  • New Members
  • 9 posts

Posted 16 May 2012 - 10:42 PM

Ah, I see where I messed up. I was going to include sleep functions, would that prevent this as well?

#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 16 May 2012 - 10:44 PM

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:
local function doSomething()
  -- do stuff
  doSomething()
end
You should do:
local function doSomething()
  while true do
    -- do stuff
  end
end


#5 L5132

  • New Members
  • 9 posts

Posted 16 May 2012 - 11:04 PM

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 :P/>
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!

#6 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 16 May 2012 - 11:38 PM

What is the exact error you are getting?

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 then
won'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


#7 L5132

  • New Members
  • 9 posts

Posted 17 May 2012 - 12:05 AM

View PostCloudy, 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
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
won'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 &gt;= 1 and i &lt;= 16 then
Ah, sorry about that. I am getting the error " doorlock:176: bad argument: number expected, got function "
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 &gt;= 1 and i &lt;=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.

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 17 May 2012 - 12:12 AM

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
...


#9 L5132

  • New Members
  • 9 posts

Posted 17 May 2012 - 12:21 AM

View PostMysticT, 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
Yep, that's what I had done, and I also went back to using some of the functions at the top of the program instead of independently setting the table down below now.
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 :P/>

#10 L5132

  • New Members
  • 9 posts

Posted 17 May 2012 - 04:00 AM

Ok, thanks for the help! I got the program working exactly as intended, the final code ended up being
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*

#11 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 17 May 2012 - 11:28 AM

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 :P/>

#12 L5132

  • New Members
  • 9 posts

Posted 17 May 2012 - 05:10 PM

View PostCloudy, 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 :P/>
Thanks for the advice. I am actually writing this program in anticipation of needing it soon, although I currently do not. Seeing as I essentially wrote this to learn how to use tables in the first place I am going to take your advice and try to use tables and otherwise condense the program. I am not sure if I mentioned this but lua is the first programming language I have tried to learn and so far I find it rather nice and fairly easy to understand now that I realize what some of the errors mean. If it's alright I think I will keep this thread open to updates if I need help with anything. Also, I changed the thread title since I figured that was probably making my issues seem more serious than they are. I really do appreciate the help you guys are giving me, and if there's anything in particular you think I should check out feel free to mention it.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users