13 posts
Posted 10 January 2014 - 09:03 AM
Hi,
I'm a newbie when it comes to programing so if this is a dumb question sorry.
I made a program to register the things my turtles say into a monitor, but I would like to change the id that shows up for the label of the turtle, is that possible?
Here's my code:
while true do
rednet.open("right")
event, id, text = os.pullEvent()
if event == "rednet_message" then
monitor = peripheral.wrap("back")
monitor.write(id.." says:"..text)
end
end
758 posts
Location
Budapest, Hungary
Posted 10 January 2014 - 10:44 AM
You can't change the ID of a computer (but you can create a new computer and copy everything from the old one to the new one). The label… Well, you can change the label of the computer (
os.setComputerLabel), but you did nothing with it in the code you posted.
175 posts
Posted 10 January 2014 - 10:52 AM
Another way is to create a table.
local names = {
[1] = "I LIKE TURTLES",
[2] = "Miner",
[941] = "Slave"
};
string.format("%s (%d): %s",
names[id], id, message);
You'll get lines like "Slave (941): finished task" or whatever.
This is probably better if you don't have any extra security in place.
Edited on 10 January 2014 - 09:54 AM
13 posts
Posted 10 January 2014 - 12:29 PM
oh that's even better than "Waterbottler:" or "Miner:" Thanks man :)/>/> oh and what do you mean by security? There are no griefers on the server I play on
doesn't work, gives me a bad argument : string expected, got nil
local names = {
[20] = "Waterbotler"
};
string.format ("%s (&d): %s",
names[id], id, text);
while true do
rednet.open("right")
event, names[id], text = os.pullEvent()
if event == "rednet_message" then
monitor = peripheral.wrap("back")
monitor.write(names[id].." says:"..text)
end
end
Edited on 10 January 2014 - 12:40 PM
8543 posts
Posted 10 January 2014 - 01:47 PM
local names = {
[20] = "Water bottler",
}
local monitor = peripheral.wrap("back")
while true do
rednet.open("right")
event, id, text = os.pullEvent()
if event == "rednet_message" then
local name
if names[id] then name = names[id] else name = id end
term.redirect(monitor)
print(name.." says:"..text)
term.restore()
end
end
end
You have to actually adapt it to your code, not just copy and paste. I also moved the peripheral wrapping out of the loop, since you only need to do it once. I also added a redirect so that you can use print on the monitor.
13 posts
Posted 10 January 2014 - 02:58 PM
Am I required to make a new variable(name)?If so could you explain why? Thanks in advance
8543 posts
Posted 10 January 2014 - 03:32 PM
It should be pretty clear in the next line why we declare that variable. It is used to ensure that whether there is an entry in the names table for the ID that sent the message or not, there will still be a value for the sender name when we concatenate the line together for the print statement.
13 posts
Posted 10 January 2014 - 04:26 PM
Ah I see. Also, I had to remove one "end" because was getting eof error.
Thanks for the help.
13 posts
Posted 10 January 2014 - 06:19 PM
I have a question is it possible to tell a turtle to do a certain action once in say 10 minutes?
Edit: Maybe timers can do it going to take a look
Edit2: Didn't, to get an event pull the whole system would stop.Help please?
Edited on 10 January 2014 - 05:32 PM
7083 posts
Location
Tasmania (AU)
Posted 10 January 2014 - 07:08 PM
What's the turtle going to be doing the rest of the time? Sitting there, or working on other tasks?
159 posts
Location
A Chair
Posted 11 January 2014 - 08:16 AM
@ GreatShooter – I would use a os.startTimer(600) then just have a check in your code that uses an if statement to trigger that event.
os.startTimer(600)
while true do
event,id,message,sender = os.pullEvent()
if event == "timer" then
-- your 10 minute code here
end
if event == "rednet_message" then
-- Your rednet message code here
end
end
That is a simple piece of code that would do what you ask, even if the turtle performs other tasks.
There is another way that is simply a "event = os.pullEvent(1) " This will pause your script for 1 second pulling the next event from the stack. (0.1 would work too ) – I think this is wrong.
Edited on 11 January 2014 - 07:19 AM
175 posts
Posted 11 January 2014 - 08:31 AM
That IS wrong, so in that sense, you're right. :P/>
sleep(1) - sleep for one sec.
13 posts
Posted 11 January 2014 - 08:52 AM
I think I miss explained it, sorry. My turtle isn't always working, but when it does it spams the monitor with messages, going to try making a variable, and each time the timer runs out I add +1 to i and each time it starts I -1 to i. Testing it now
Edit: making it so the turtle only messages each 10 minutes, not the computer.
Edited on 11 January 2014 - 07:55 AM
159 posts
Location
A Chair
Posted 11 January 2014 - 09:14 AM
The sleep function pauses the script, which is what GreatShooter here is trying to avoid.
I guess another way would be to record the system time, then add a line of check code.
Here is a little sample I wrote to do just that on a basic check.
while true do --// loop here for ever.
local executetime = os.time()
local executetime = executetime + 12 --// This is 10 minutes in os.time
while executetime >= os.time() do --// Loop untill our 10 minutes have gone.
sleep(0.1) --// my current prevention for yielding during testing
term.setCursorPos(1,5)
print(executetime)
print(os.time)
end
tenminjob() --// call the 10 minute function
end
Currently this will print the time and time that the counter will trigger. To make it run your normal programs just call your normal programs inside the 10 minute loop. Once it finishes the last task and the 10 minutes have passed it will exit the loop and then continue with the command you wish to run after 10 minutes.
13 posts
Posted 11 January 2014 - 01:07 PM
The turtle isn't connected to a monitor so isn't term.setCursorPos going to give me an error? (server down can't check it) once server comes back up I'll post the turtle code if that helps
Edited on 11 January 2014 - 12:09 PM
13 posts
Posted 11 January 2014 - 02:13 PM
Here is my turtle's code:
rednet.open("right")
while true do
local executetime = os.time()
local executetime = executetime + 12
while executetime >= os.time() do
turtle.place()
end
rednet.send(22, "Finished some bottles")
end
And my computer's code:
local names = {
[20] = "Water bottler",
}
local monitor = peripheral.wrap("back")
os.startTimer(600)
while true do
rednet.open("right")
event, id, text = os.pullEvent()
if event == "rednet_message" then
local name
if names[id] then name = names[id] else name = id end
term.redirect(monitor)
print(name.." says:"..text)
term.restore()
print(os.pullEvent())
local executetime = os.time()
local executetime = executetime + 12
while executetime == os.time() do
term.redirect(monitor)
term.clear()
term.restore()
end
end
end
Do you think they are ok/ need tweaking?
159 posts
Location
A Chair
Posted 14 January 2014 - 01:55 PM
And my computer's code:
local names = {
[20] = "Water bottler",
}
local monitor = peripheral.wrap("back")
os.startTimer(600)
while true do
rednet.open("right")
event, id, text = os.pullEvent()
if event == "rednet_message" then
local name
if names[id] then name = names[id] else name = id end
term.redirect(monitor)
print(name.." says:"..text)
term.restore()
print(os.pullEvent())
local executetime = os.time()
local executetime = executetime + 12
while executetime == os.time() do
term.redirect(monitor)
term.clear()
term.restore()
end
end
end
Do you think they are ok/ need tweaking?
The server code here will not work correctly. There is only a fraction of a tick where executetime and os.time() will match, the timer updates faster than the tick on the program so it could actually pass the matching time before it activates, hence I used >= in my check system.
Also due to the fact that our turtle is only sending a message once 10 minutes passes, you should have the server running on the os.pullEvent() as a wait and not the os.time() way. as waiting on os.time() will only trigger on one event.
local names = {
[20] = "Water bottler",
}
local monitor = peripheral.wrap("back")
os.startTimer(600)
while true do
rednet.open("right")
event, id, text = os.pullEvent()
if event == "rednet_message" then
local name
if names[id] then name = names[id] else name = id end
term.redirect(monitor)
print(name.." says:"..text)
term.restore()
print(event) --// print(os.pullEvent()) will make the system pause for a event to print, not print the captured event.
--// Removed the section that paused script for 10 minutes, this would have errored as well, because there was no yielding events.
end
end
13 posts
Posted 15 January 2014 - 10:44 AM
Oh! That's just how I am, distracted, I was copying the code to a disk to save it and somehow put it on the computer. Thanks for everything.