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

can some 1 help me shorten my program :)

Started by monkeybee11, 04 January 2013 - 01:32 PM
monkeybee11 #1
Posted 04 January 2013 - 02:32 PM
hello :P/> me am trying to make a useless program that works with rednet just for fun but me want to know if theres a way to compact it me copyed part of my program its basicly the same just copyed 16 times (for each inv slot)

if message == "1" then -- using rednet to send signle to turtle
turtle.select(1) -- music cd slot
turtle.dropDown() -- put cd in diskdrive
shell.run(dj)  -- exsample it playing cat
sleep(187) -- wait for it to finsh playing
turtle.suckdown() -- picks up the disk

if message == "2" then 
turtle.select(2)
turtle.dropDown()
shell.run(dj) -- playing 13
sleep(180)
turtle.suckdown()

if message == "3" then 
turtle.select(3)
turtle.dropDown()
shell.run(dj) --chirp
sleep(187)
turtle.suckdown()

if message == "4" then 
turtle.select(4)
turtle.dropDown()
shell.run(dj) -- playing stal
sleep(150)
turtle.suckdown()

-- the same up to 16

end


just wondering if theres a way to shorten the program instead of having this copyed and pasted 16 times :P/>

me have not tested this program at all ingame me only wright it in notepad++(pass my bedtime)
ChunLing #2
Posted 04 January 2013 - 03:22 PM
for i=1,16 do
    if message == tostring(i) then
        turtle.select(i) -- music cd slot
        turtle.dropDown() -- put cd in diskdrive
        shell.run(dj)  -- exsample it playing cat
        sleep(187) -- wait for it to finsh playing
        turtle.suckdown() -- picks up the disk
    end
end
If your sleep times need to be specific to each slot and you can't come up with a simple math that lets you derive the value from the slot, then use a table.
stime = {187,180,187,150,} --and the other 12 entries
Then replace the sleep(187) with sleep(stime)
monkeybee11 #3
Posted 04 January 2013 - 04:03 PM
thx me am really new to lua XD and me cant do simple math had to google howmeny sec in 3mins XD
so with that
stime = {187 , 180 , 187 ,150 }
it will tell what cd its playing and will wait for it to stop?
ChunLing #4
Posted 04 January 2013 - 04:32 PM
It won't tell what cd is in there, but it does associate each number from 1 to 4 with a number in the table. In other words:
stime = {187 , 180 , 187 ,150 }
basically does the same thing as:
stime = {}
stime[1] = 187
stime[2] = 180
stime[3] = 187
stime[4] = 150
That is, it creates a table, and then stores values in that table, indexed by numerical order, so you can get a given value out of the table by supplying the table reference and the index value.

And…now is probably time for a link to something that might actually make sense.

Also, you should probably make the table local in scope when you declare it, as in "local stime = {187,180,187,150,}"
monkeybee11 #5
Posted 04 January 2013 - 11:22 PM
errrrr can u dum that down a bit :S didt understand any of that D:


ok me done the program like u said thx for the help but it dont seem to work S: me got my
avance computer doing rednet.broadcast("i") and the turtle dose nothing
remiX #6
Posted 05 January 2013 - 12:12 AM
The only way I see to make it easier:


tableOfStuff = {
    [1] = "First index",
    [2] = "Second Index",
    [3] = "Third index"
    }
-- and so on

As you can see the first item is 'First Index' which is the first index of the table "tableOfStuff".

Not sure how to explain it easier…
Kingdaro #7
Posted 05 January 2013 - 12:37 AM
Why even loop it, though?

turtle.select(tonumber(message)) -- music cd slot
turtle.dropDown() -- put cd in diskdrive
shell.run(dj)  -- exsample it playing cat
sleep(187) -- wait for it to finsh playing
turtle.suckdown() -- picks up the disk
With the whole stime thing.
monkeybee11 #8
Posted 05 January 2013 - 02:06 AM
me just want to have a wireless turtle hidden some where with the music disks in it inv and have a computer send a rednet signle to the turtle and it plays the music disk


but the program dont seem to work me am sending the message "i" on rednet and my turtle just dose nothing S:

The only way I see to make it easier:


tableOfStuff = {
	[1] = "First index",
	[2] = "Second Index",
	[3] = "Third index"
	}
-- and so on

As you can see the first item is 'First Index' which is the first index of the table "tableOfStuff".

Not sure how to explain it easier…

oh ok so using that will be like


tableOfStuff = {
	[1] = "cat",
	[2] = "13",
	[3] = "wait"
	}
-- and so on
Kingdaro #9
Posted 05 January 2013 - 02:11 AM
Whoops, made some derps.

times = {187, 180, 187, 150}
while true do
  local id, msg = rednet.receive()
  msg = tonumber(msg)
  if msg then
    turtle.select(msg) -- music cd slot
    turtle.dropDown() -- put cd in diskdrive
    shell.run("dj","down")  -- example it playing cat
    sleep(times[msg]) -- wait for it to finish playing
    turtle.suckDown() -- picks up the disk
  end
end

Went ahead and made it into a full script anyway.
remiX #10
Posted 05 January 2013 - 02:13 AM
me just want to have a wireless turtle hidden some where with the music disks in it inv and have a computer send a rednet signle to the turtle and it plays the music disk


but the program dont seem to work me am sending the message "i" on rednet and my turtle just dose nothing S:

The only way I see to make it easier:


tableOfStuff = {
	[1] = "First index",
	[2] = "Second Index",
	[3] = "Third index"
	}
-- and so on

As you can see the first item is 'First Index' which is the first index of the table "tableOfStuff".

Not sure how to explain it easier…

oh ok so using that will be like


tableOfStuff = {
	[1] = "cat",
	[2] = "13",
	[3] = "wait"
	}
-- and so on

Yes :)/> But adding the [1] [2] [3] is optional, it does that if you do not add it automatically.

And the print the stuff:

print(tableOfStuff[index])
-- where in this case, index can be 1, 2, or 3
print(tableOfStuff[2])
-- outcomes
13 (for your program)
monkeybee11 #11
Posted 05 January 2013 - 02:17 AM
Whoops, made some derps.

times = {187, 180, 187, 150}
while true do
  local id, msg = rednet.receive()
  msg = tonumber(msg)
  if msg then
	turtle.select(msg) -- music cd slot
	turtle.dropDown() -- put cd in diskdrive
	shell.run("dj","down")  -- example it playing cat
	sleep(times[msg]) -- wait for it to finish playing
	turtle.suckDown() -- picks up the disk
  end
end

Went ahead and made it into a full script anyway.

will try that but me got a tiny problem atm

me found out that the turtle for some reason is not reciving any rednet signle at all S:
tested this by telling it to print("playing") if it gets the message sent the message on my computer over rednet and the turtle screen was blank S:
ChunLing #12
Posted 05 January 2013 - 02:25 AM
And…now is probably time for a link to something that might actually make sense.
I put the link to the page about tables because I knew I was failing to explain them. It is a slightly involved topic, but one that is essential to proficiency in Lua.

If we were to rely more heavily on tables, then we could do something like:
local mdisk = {187 , 180 , 187 ,150,187 , 180 , 187 ,150,187 , 180 , 187 ,150,187 , 180 , 187 ,150,} --only with the actual lengths of the songs on the disks
while true do
  local id, msg = rednet.receive()
  id = tonumber(msg)
  if mdisk[id] then
   turtle.select(id) -- music cd slot
   turtle.dropDown() -- put cd in diskdrive
   shell.run("dj","down")  -- example it playing cat
   sleep(mdisk[id]) -- wait for it to finish playing
   turtle.suckDown() -- picks up the disk
  end
end
That is, we are now using the table for logic control as well as for storing data on music disk play length. Not a grand change, but a method that has important implications.
monkeybee11 #13
Posted 05 January 2013 - 02:27 AM
um guys can we forget the table thing for a moment here and try to figer out y the turtle wont lissen to rednet?

if u want to pop on the server and mess with the turtle need ign for whitelist S:
remiX #14
Posted 05 January 2013 - 02:44 AM
Are you using ChunLing's code?

Btw ChunLing, this:
id = tonumber(msg)
Why are you changing id to msg? :P/> Were you not meant to
msg=tonumber(msg)

And monkey, which minecraft are you playing?
monkeybee11 #15
Posted 05 January 2013 - 02:49 AM
mindcrack ftb on my public server
remiX #16
Posted 05 January 2013 - 03:02 AM
Ok, whitelist me (sidekick_)

I'll most probably lag though, because of where I live :P/>
monkeybee11 #17
Posted 05 January 2013 - 03:04 AM
ip is monkeyscraft.no-ip.org
the server is hosted in england
remiX #18
Posted 05 January 2013 - 03:08 AM
Will join soon, busy updating the mod pack :P/> I play direwolf20 so haven't got mindcrack up to date
monkeybee11 #19
Posted 05 January 2013 - 03:11 AM
np me am so glad that me didt want to use the avance computer to make like a select program knowing my luck that will never work @_@
Kingdaro #20
Posted 05 January 2013 - 03:14 AM
Are you opening rednet?
ChunLing #21
Posted 05 January 2013 - 03:27 AM
I wasn't using id for anything, and it was already a number. I prefer to keep variables the same type when I can, cause that can help avoid generating garbage. I also like using whatever is handy, which is more of a personal quirk.

Anyway, opening your modem is important.
monkeybee11 #22
Posted 05 January 2013 - 04:43 AM
yes me did rednet.open("right")
and its ok now remieX fixed the code when he was on the server :P/> and now hes upgrading it so any 1 can select what song they want by using the avance computer :)/>