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

[Question]How to close a running program.

Started by whatsfast, 27 November 2012 - 11:55 AM
whatsfast #1
Posted 27 November 2012 - 12:55 PM
I am trying to run a program that gets a code from another computer and then runs that code. Whenever I run the code it gives me the bios:26:Attempt to write to global. My guess is that the programs are intefering with each other. So my question is how would I have the program I don't need any more to stop?
KaoS #2
Posted 27 November 2012 - 12:59 PM
the issue there is that you are trying to make a variable that is not local (in other words is global) where it is not allowed, this happens in the loadstring function. example


loadstring('me=read()')()

you are not allowed to make the global variable me so it errors

EDIT: a way around this is to write your information to a file and execute it. rather than

loadstring(yourcode)()
use

local f=io.open('running','w')
f:write(yourcode)
f:close()
shell.run('running')
fs.delete('running')

if you do this you should also add something at the start of your program that checks if 'running' exists and either runs or deletes it (based on what you want it to do) because if you terminate, shutdown or restart the computer while it is running it will then remain, in turtles it is a bad idea to then start from the beginning again because they may be half way through making stairs to your base or something, restarting would then make them dig stairs through your base :| something I have done all too often. if it is a computer you may want to just restart running to do whatever was planned…. up to you
whatsfast #3
Posted 27 November 2012 - 01:06 PM
Ya I considered saving it but this will be on a server. Is there a way to stop a program?
KaoS #4
Posted 27 November 2012 - 01:08 PM
just call error() or if you have not prevented ctrl+t termination use os.queueEvent('terminate') to fake a manual termination, this is good because all other events in the queue will be processed before your program dies. if you are using a constant loop in your program you can also use break to end it
Tiin57 #5
Posted 27 November 2012 - 01:10 PM
KaoS, why not just use the simpler method of local variables?

local x = 4
as opposed to

x = 4
KaoS #6
Posted 27 November 2012 - 01:18 PM
in some cases global changes are necessary for the code to work, while my method is longer it always works. also the loadstring command does not have access to the global environment, it only loads the existing APIs so any function manually added by a custom API loader or by a program as well as any global variable you need is no longer accessible (this includes all shell commands as shell is run, not loaded as an API). an example of this problem is in my game I have a central PC that operates several others, in one computer's startup it opens the modem (of course so it can get the commands from the server) and wraps mon to the monitor beneath it so I can send information there easily, if I used loadstring to execute the remote commands the mon variable would not be usable and so every time I wanted to change something on the screen I would either have to re-wrap it or use a peripheral call…

that isn't a great example as you can just term.redirect it but you get the general idea. in order for it to be executed exactly like local code it must be run from a program.

PS: if you know an alternate method PLEASE post it, I hate having to save files all the time, it's messy
whatsfast #7
Posted 27 November 2012 - 01:31 PM
Ok it helped me with the error thanks KaoS. But one more question. It may be a bit off topic to this specific post but how would you print for example a time in your program but have it update all the time so its like a clock?
KaoS #8
Posted 27 November 2012 - 01:37 PM
while true do term.clear() term.setCursorPos(1,1) print(os.time()) sleep(0) end
whatsfast #9
Posted 27 November 2012 - 01:41 PM
I see thanks.
KaoS #10
Posted 27 November 2012 - 01:44 PM
no problem. good luck with the remote system, it has great potential (install a hidden control coroutine in their computers…. endless laughs as you type 'I am gay' into their code… WHILE THEY WATCH :D/> haha I always end up using great systems for pointless activities)
whatsfast #11
Posted 27 November 2012 - 02:00 PM
WOW. I am definitely going to do that. The system works perfectly. My next job is to add more scripts. I got a login, programs, and planned email and notification servers. What else do you think I should add?

Lights and controlling stuff like that will be in a another system/building.
KaoS #12
Posted 27 November 2012 - 03:43 PM
I think one of the coolest things to do is to just keep adding more and more into every PC using coroutines. eventually you should have a networking coroutine for wireless terminals, position tracking coroutine/BIOS hack with GPS and radar coroutine so all turtles can find their position, a turtle auto-fuelling coroutine, a remote override WITH THE ABILITY TO REMOTELY ADD MORE COROUTINES TO THE STACK and include in the remote system a status updater so a central PC always knows if a turtle is busy or not, where it is etc

if you are feeling super ambitious you can try a project I abandoned due to a loss of ccSensors, basically you have sensors everywhere in your base, recording everything, when you take something from a chest, where you stand, when you put something in a chest or anything like that, all of your activities should be recorded and then you can ask the server to assign a turtle that is not busy to perform those tasks for you, it can follow the path you took, take the items out of chests and load them in other places, I am not sure how to detect crafting recipes though to mimic that :(/> but yeah, you get the general idea

do you know how coroutines work?
whatsfast #13
Posted 27 November 2012 - 03:48 PM
I like the idea of a status updater for everything. Nice idea. If I know how coroutines work. NO. Never heard. I have no idea even how to detect a if an item is taken out let alone a if a chest is open. You have some crazy ideas. I guess I could try to make one and then post it on the forums. But I will need some help. Also how does a internet sound? Also what would I put on the internet…..like a moving nyan cat :P/>
KaoS #14
Posted 27 November 2012 - 03:52 PM
hahaha. why make the internet when you have access to the real one? in order to detect such things you need ccSensors, it was not updated for a long time, now they are working on open ccSensors but for some reason it will not work on FTB :(/>

coroutines are Lua's way of imitating multitasking, running multiple codes almost simultaneously… strictly speaking it isn't simultaneous but it swaps between the threads so fast you cannot notice unless you do not yield
whatsfast #15
Posted 27 November 2012 - 03:57 PM
Right now im on tekkit build. So maybe it will work. If tekkit would update to the advanced computers, I probably doubt I will use them because I like the feel and usage of the normal computers. It kinda sound stupid but there a lot simpler is what I like. I just rehearsed coroutines and I kinda now understand them.The only problem is how would you detect a chest opening and for example the amount of items took out? That seems kinda complex in my opinion. Like how would you do it and what detects it? A computer or a turtle?
whatsfast #16
Posted 27 November 2012 - 03:59 PM
Screw that though about how it works. Just researched a bit.

My second question is that having your own internet would allow people to search useful stuff/random stuff. And everything would be compatible.
KaoS #17
Posted 27 November 2012 - 04:00 PM
neither. lemmi look up the ccSensors thread and give you a link, it is an addon that is already in tekkit although one of the proximity sensor's probes are broken…. and that probe would be so useful….

edit: here is the link… I hope it's the right one
KaoS #18
Posted 27 November 2012 - 04:03 PM
erm is that a question or a statement?
whatsfast #19
Posted 27 November 2012 - 04:03 PM
Question :P/> in the terms of would be a good idea?
KaoS #20
Posted 27 November 2012 - 04:06 PM
well just like on the real internet you would have to set up an entire hosting system where you can put your content online etc… in the end it is really complicated and just not worth it. another useful coroutine to have in that I have never actually used in coroutine form is remote file browsing. I made a script where another computer can request file lists in certain directories and request a copy of a file as well, it would be a great coroutine….
whatsfast #21
Posted 27 November 2012 - 04:06 PM
Well I will pick on this idea tomorrow.
KaoS #22
Posted 27 November 2012 - 04:08 PM
good plan. it is 5am here and I'm not feeling tired but I will later on at work :unsure:/>
whatsfast #23
Posted 27 November 2012 - 04:09 PM
Also can you give the code for the detecting of everything around the world like chest opened and stuff. Also I though that maybe you could have a basic server that reads the url. given from a client. And then sends back text of like a webpage or something. And the text could be saved in a file on the computer or I could have another computer as the website. Il think about this tomorrow.
Orwell #24
Posted 27 November 2012 - 06:04 PM
in order to detect such things you need ccSensors, it was not updated for a long time, now they are working on open ccSensors but for some reason it will not work on FTB :(/>
OpenCCSensors should work just fine for FTB when they update to MC 1.4.5 (they say that this should be soon). It's just not included by default.
whatsfast #25
Posted 28 November 2012 - 02:31 AM
Well I will try my best later today to try and code the cc sensors and do a bunch of stuff with it. Is there like a api or documentation ccsensors?
KaoS #26
Posted 28 November 2012 - 02:48 AM
there are a few youtube videos, give it a search. here is a thread about it with quite a good tutorial