This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
digital_light's profile picture

Enhanced Portals 3 Wireless controller [java error]

Started by digital_light, 06 March 2015 - 11:29 PM
digital_light #1
Posted 07 March 2015 - 12:29 AM
Hey I am new to lua but i know enough, or have researched on how to do things, but total confused. I am trying to make a Enhanced Portals 3 hub that is controlled remotely by computer craft. It has its own api. I have a small program that is nothing fancy. It just sends the name of the portal through the wireless handheld to the main computer which then tells the portal what to do. When i call the function to open the portal (the open function) it throws a java error in the computer. If you know anything about this please help. The weirdest thing is I can make it work using numbers but when i try to use names it breaks.

modem=peripheral.wrap("top")
dial=peripheral.wrap("ep_dialling_device_0")
--dial uses the enhanced portals api which controls the portal through a wired connection with the computer.
modem.open(5)
function list()
local out={}
for i=0,dial.getStoredCount() do
  out[i]=dial.getStoredName(i)
end
return out
end
function open(msg)
dial.terminate()
for i,check in ipairs(list()) do
  if string.find(msg,check) then
   dial.dialStored(i)
   return true
  end
end
return false
end
 
while not redstone.getInput("back") do
event,side,sent,reply,msg,distance=os.pullEvent("modem_message")
if not( string.find(msg,"close"))and open(msg) then
  modem.transmit(reply,sent,msg.." has opened")
elseif not string.find(msg,"close") then
  modem.transmit(reply,sent,msg.." not found in list,  please try again")
else
  modem.transmit(relpy,sent,"Portal Closed")
end
end
the code on the handheld just sends what ever is typed into on channel 5.
KingofGamesYami #2
Posted 07 March 2015 - 01:16 AM
Question: string.find( msg, check ) what's check? I don't see it.
digital_light #3
Posted 07 March 2015 - 04:44 AM
It checks msg to see if it contains any one of the strings in the array returned by the list function which gets the names of all the stored locations in the daillier. It goes through each one individually as it goes through the for loop.
valithor #4
Posted 07 March 2015 - 06:31 AM
Question: string.find( msg, check ) what's check? I don't see it.

for i,check in pairs its the second variable in the for loop.

My one question would be what would happen if you terminated dialing if there was nothing being dialed in the first place (I have never used the mod). I would put a few unique print statements in that funciton to see exactly what line is erroring, whether it be where you do dial.terminate, or dial.dialStored.
digital_light #5
Posted 07 March 2015 - 03:38 PM
I
Question: string.find( msg, check ) what's check? I don't see it.

for i,check in pairs its the second variable in the for loop.

My one question would be what would happen if you terminated dialing if there was nothing being dialed in the first place (I have never used the mod). I would put a few unique print statements in that funciton to see exactly what line is erroring, whether it be where you do dial.terminate, or dial.dialStored.
I have been working the mod for a couple days and both dial.terminate and dial.dialStored both work. though i will print a couple variables to find out what is causing the error. the weird part is it is not a lua error it is a java one.

oh i am sorry i didn't answer your question about the mod. It literally does nothing. dial.terminate is basically a shutdown for the portal while any of the other dial functions are a sort of boot up. I have to put the dial.terminate in otherwise it will through the Enhanced portals error portal already in use.
Bomb Bloke #6
Posted 07 March 2015 - 11:21 PM
the weird part is it is not a lua error it is a java one.

In the majority of cases, this means that a peripheral's code is bugging out, typically because you're calling one of its functions incorrectly.

My assumption is that the peripheral only takes numbers, not strings, and was never supposed to work using names. I'm not sure why you'd want to use names in the first place.
digital_light #7
Posted 07 March 2015 - 11:25 PM
the weird part is it is not a lua error it is a java one.

In the majority of cases, this means that a peripheral's code is bugging out, typically because you're calling one of its functions incorrectly.

My assumption is that the peripheral only takes numbers, not strings, and was never supposed to work using names. I'm not sure why you'd want to use names in the first place.
no I know it only works with numbers as the argument. I am sending one. I equate one portal name with its corresponding number in the list. Because I build the list from the peripheral the strings are in the same order. what i meant was when i had the numbers as the input rather than the names it worked without error.
Edited on 07 March 2015 - 10:25 PM
Bomb Bloke #8
Posted 07 March 2015 - 11:51 PM
Ah, I getcha.

Reckon the problem is in your list() function. This bit:

for i=0,dial.getStoredCount() do
  out[i]=dial.getStoredName(i)
end

Let's say there are ten names. The loop will iterate from 0 to 10, repeating eleven times. Odds are either dial.getStoredName(0) or dial.getStoredName(10) will be triggering your Java error.

Bear in mind that your ipairs loop in your open() function will ignore index 0 in your table (Lua tables generally start at index 1). So you'll either need to rebuild the list() loop like this:

for i=1,dial.getStoredCount() do
  out[i]=dial.getStoredName(i)
end

… or this:

for i=0,dial.getStoredCount()-1 do
  out[i+1]=dial.getStoredName(i)
end

… depending on whether dial.getStoredName() starts from 0 or not.

… though I still reckon it'd be easier to use numbers directly! The only reason I can see to do things in terms of strings is if your client code is getting the user to type the names in, which sounds painful.
Edited on 07 March 2015 - 10:54 PM
digital_light #9
Posted 08 March 2015 - 04:29 AM
Ah, I getcha.

Reckon the problem is in your list() function. This bit:

for i=0,dial.getStoredCount() do
  out[i]=dial.getStoredName(i)
end

Let's say there are ten names. The loop will iterate from 0 to 10, repeating eleven times. Odds are either dial.getStoredName(0) or dial.getStoredName(10) will be triggering your Java error.

Bear in mind that your ipairs loop in your open() function will ignore index 0 in your table (Lua tables generally start at index 1). So you'll either need to rebuild the list() loop like this:

for i=1,dial.getStoredCount() do
  out[i]=dial.getStoredName(i)
end

… or this:

for i=0,dial.getStoredCount()-1 do
  out[i+1]=dial.getStoredName(i)
end

… depending on whether dial.getStoredName() starts from 0 or not.

… though I still reckon it'd be easier to use numbers directly! The only reason I can see to do things in terms of strings is if your client code is getting the user to type the names in, which sounds painful.
wait a second…
(Lua tables generally start at index 1)
every other programming language I have learned starts from zero always. any way i will try your suggestions. thank you for your help.
digital_light #10
Posted 08 March 2015 - 05:19 AM
It worked. Thank you so much. It was trying to get a value that didn't exist. Why does lua start at 1 anyway? It is fairly standard to start at 0. Oh well once I accounted for that error it worked as expected. And to explain why I am using names rather than numbers, it is so that say i was out and about in my world. I wanted to get home. so I go to my portal that is closest and then i would have to remember which number it was. I would much rather have it so I can enter the name of the place i am at rather memorize a bunch of numbers for what each place was. plus i would like to make a gui for this at some point.
Bomb Bloke #11
Posted 08 March 2015 - 08:06 AM
Certain languages (eg BASIC, Lua, etc) aren't aimed at experienced coders, and so stick to what makes sense to "regular people" instead - and for "regular people", counting starts at one.

The idea regarding the strings vs numbers thing is that you have the server provide a list of names to the client, which displays them alongside their numbers and asks the user to pick one. The user then need only enter a number, or click on the relevant name; the client can then send the number back to the server, and… well I'm sure you get the idea. ;)/>