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

Loading Multiple Files?

Started by brett122798, 09 January 2013 - 05:45 PM
brett122798 #1
Posted 09 January 2013 - 06:45 PM
Hey, I need to somehow, if possible, load multiple files, or scripts, at the same time for certain multitasking. The parallel API will not do what I would need to happen, if you know what I mean..

Edit:

Here's an example:


print("Test")
runScript("Scripts/test") -- This script will not wait for that one, resulting in multitasking
print("Test2")
runScript("Scripts/test2") -- So now three scripts are running at the same time!
print("Test3")
imef #2
Posted 09 January 2013 - 11:52 PM
I've never really worked with multitasking but I guess that if the parallel API doesn't work you should try looking at the coroutine API…
theoriginalbit #3
Posted 09 January 2013 - 11:54 PM
I've never really worked with multitasking but I guess that if the parallel API doesn't work you should try looking at the coroutine API…

parallel api is just managed coroutines. takes the work out of you having to do it manually….. in all honesty I cannot think of a way to do this, everything yields, everything runs on its own. there is no way to have something truly run at the same time (that I know of)…
brett122798 #4
Posted 10 January 2013 - 01:14 PM
Please read my edit.


Can't type much, I'm on a mobile device so it's hard.
theoriginalbit #5
Posted 10 January 2013 - 01:18 PM
how does runScript work? Because it should be waiting…….. unless its being called from within a parallels… say if you have this


function one()
  shell.run( "something" )
end

function two()
  print( "something else" )
end

parallel.waitForAll( one, two )

in that case two would still be running while one has called the other script, to stop that from happening you would have to do manual coroutines and yield the others while the script runs, then resume them when the script is done…
brett122798 #6
Posted 10 January 2013 - 01:25 PM
how does runScript work? Because it should be waiting…….. unless its being called from within a parallels… say if you have this


function one()
  shell.run( "something" )
end

function two()
  print( "something else" )
end

parallel.waitForAll( one, two )

in that case two would still be running while one has called the other script, to stop that from happening you would have to do manual coroutines and yield the others while the script runs, then resume them when the script is done…

Problem with that is that I just want to call scripts whenever I want. Like I may be in the middle of a function and cannot restart it or the world would end.
theoriginalbit #7
Posted 10 January 2013 - 01:29 PM
you dont have to yield the current coroutine as it is 'running' the script… only the other coroutines would need to be yielded. also just incase its causing confusion, yielding pauses the coroutine, nothing more.
brett122798 #8
Posted 10 January 2013 - 01:39 PM
you dont have to yield the current coroutine as it is 'running' the script… only the other coroutines would need to be yielded. also just incase its causing confusion, yielding pauses the coroutine, nothing more.

So I have to pause the current script in order to run others?
theoriginalbit #9
Posted 10 January 2013 - 01:41 PM
only if you are using the parallels api to run your script….


Side note: this help may be quicker if you show code
brett122798 #10
Posted 10 January 2013 - 01:46 PM
only if you are using the parallels api to run your script….


Side note: this help may be quicker if you show code

How else can I do it?

I do not have any code.. yet. I am planning on making an OS and knowing this would make it 10x easier.
brett122798 #11
Posted 10 January 2013 - 01:55 PM
Edited previous post.
theoriginalbit #12
Posted 10 January 2013 - 01:56 PM
well if you aren't multitasking in your own code, using shell.run( scriptPath ) will run a script and not continue to run yours until that script is done.
brett122798 #13
Posted 10 January 2013 - 02:01 PM
well if you aren't multitasking in your own code, using shell.run( scriptPath ) will run a script and not continue to run yours until that script is done.

But I need the initial script and the other one to run at the same time.
theoriginalbit #14
Posted 10 January 2013 - 02:23 PM
then doing this


local run = false
local path = ""
local function runScript( )
  while true do
    if run then shell.run( path ) end
    sleep( 0 )
  end
end

local function runLocal()
  -- doing program stuff
  run = true
  path = somePath
  -- do more stuff
end

parallel.waitForAll( runLocal, runScript )

is the only way i can really think of… its a little messy, typed it quick, but you should get the idea.
brett122798 #15
Posted 10 January 2013 - 06:01 PM
Finally back on my computer.


Yeah, still not exactly what I need.. I think I need to wait for someone that is more professional at knowing how to run multiple things at once without using the parallel API. Thanks for the effort though, I know it's hard. :P/>
crazyguymgd #16
Posted 10 January 2013 - 06:21 PM


function foo1()
  local time1 = os.clock()
  for i = 1, 5 do
	sleep(1)
	local time2 = time1
	time1 = os.clock()
	print("One second "..time1 - time2)
  end
end

function foo2()
  local time1 = os.clock()
  for i = 1, 10 do
	sleep(0.50)
	local time2 = time1
	time1 = os.clock()
	print("Half second "..time1 - time2)
  end  
end

parallel.waitForAll(foo1,foo2)

The output of this is:

Half second 0.55
One second 1.0
Half second 0.55
Half second 0.55
One second 1.0
Half second 0.55
Half second 0.55
One second 1.0
Half second 0.55
Half second 0.55
One second 1.0
Half second 0.55
Half second 0.55
One second 1.0
Half second 0.55

Note that when you run foo2 by itself it will print 0.55 10 times as well.
So this makes me think the parallel api does a pretty good job at running the two at the same time no?
theoriginalbit #17
Posted 10 January 2013 - 06:22 PM
"someone that is more professional"… Well that sounds rude, at least you lightened it up by saying thanks :)/>
Just a small retort though… what are your qualifications? mine is a Bachelor of Science Majoring in Professional Software Development…

Now back to the issue at hand… In Lua its coroutines or parallel api. the parallel api is managed coroutines. And if i was suggesting in other languages i would suggest threading. however this is not another language, so I can only suggest what Lua offers. However if you want proof from someone more "professional" then have a read of this thread, this guy has multitasking and uses parallels, and has the Cloudy stamp of approval! If that still isn't helpful, maybe the developers of Lua can be more a little more illuminating on the issue here

Sorry if any of this reply came across harsh…
theoriginalbit #18
Posted 10 January 2013 - 06:35 PM
Just a side note (after crazyguymgd posted an example with sleep() ): The sleep function doesn't actually 'sleep' for the given time, its more of a "I would like to sleep for this long", sometimes if a coroutine, or in CC's case a computer ( since they all have to yield to let the next run), fails to yield or is slow to yield it will mess with the sleep time. However lets say that theoretically the computer running never has to yield and it calls sleep(1) after long enough of doing this timing can be thrown off since at an assembly code level you can never get it perfect, you can get it close to it, but not spot on. For example I had to write a stop watch in assembly code. Using nop and goto $+1, which use 1 and 2 instruction cycle respectively, to have a 1 second timer it was actually about a 0.999999 second timer, while thats a small difference it means that after a long enough period of time the stop watch would actually have not been timing correctly and would have lost seconds (or more depending on how long it ran).
Lyqyd #19
Posted 10 January 2013 - 06:42 PM
So you're looking for a coroutine management system that lets you add peer coroutines from the currently executing script. Have you read through Gopher's goroutine API? It may be instructive.
brett122798 #20
Posted 10 January 2013 - 06:52 PM
"someone that is more professional"… Well that sounds rude, at least you lightened it up by saying thanks :)/>
Just a small retort though… what are your qualifications? mine is a Bachelor of Science Majoring in Professional Software Development…

Now back to the issue at hand… In Lua its coroutines or parallel api. the parallel api is managed coroutines. And if i was suggesting in other languages i would suggest threading. however this is not another language, so I can only suggest what Lua offers. However if you want proof from someone more "professional" then have a read of this thread, this guy has multitasking and uses parallels, and has the Cloudy stamp of approval! If that still isn't helpful, maybe the developers of Lua can be more a little more illuminating on the issue here

Sorry if any of this reply came across harsh…
Oh no no, I had no intentions of an insult or anything, I meant that everyone probably has their things in a language they're rather better at and they like more. Please do not take that as an insult, it could have been better worded though.

I have no special degrees or whatever of any kind. I am only fourteen and plan on learning professional Java programming. Lua is just kind of a warm-up I suppose since it's very easy.
brett122798 #21
Posted 10 January 2013 - 06:56 PM
So you're looking for a coroutine management system that lets you add peer coroutines from the currently executing script. Have you read through Gopher's goroutine API? It may be instructive.
WOAH! Does that do exactly what I was saying? Runs a function but still continue the code?
crazyguymgd #22
Posted 10 January 2013 - 06:58 PM
Just a side note (after crazyguymgd posted an example with sleep() ): The sleep function doesn't actually 'sleep' for the given time, its more of a "I would like to sleep for this long", sometimes if a coroutine, or in CC's case a computer ( since they all have to yield to let the next run), fails to yield or is slow to yield it will mess with the sleep time. However lets say that theoretically the computer running never has to yield and it calls sleep(1) after long enough of doing this timing can be thrown off since at an assembly code level you can never get it perfect, you can get it close to it, but not spot on. For example I had to write a stop watch in assembly code. Using nop and goto $+1, which use 1 and 2 instruction cycle respectively, to have a 1 second timer it was actually about a 0.999999 second timer, while thats a small difference it means that after a long enough period of time the stop watch would actually have not been timing correctly and would have lost seconds (or more depending on how long it ran).

First of all, blah to assembly. (One semester away from a Bachelor of Science in computer systems. I see plenty more assembly in my future)
Second of all, and while trying to not sound like an ass, I do know about the inaccuracies of the sleep function. Given that this is ComputerCraft and we're not going to be able to use more than one cpu, we'll never get two of our programs truly running in parallel.

Edit - although apparently I still need a class in grammar
theoriginalbit #23
Posted 10 January 2013 - 06:59 PM
I have no special degrees are whatever of any kind. I am only fourteen and plan on learning professional Java programming. Lua is just kind of a warm-up I suppose since it's very easy.
Well good luck in your future endeavours, make sure you start looking into Java and some OO concepts as soon as possible, will make it way easier come the time to learn :)/>
brett122798 #24
Posted 10 January 2013 - 07:06 PM
I have no special degrees are whatever of any kind. I am only fourteen and plan on learning professional Java programming. Lua is just kind of a warm-up I suppose since it's very easy.
Well good luck in your future endeavours, make sure you start looking into Java and some OO concepts as soon as possible, will make it way easier come the time to learn :)/>
I'm trying.. I still do not even know the definition of "Object-Oriented" after being told a million times. And the language is just so.. hard looking, so I always put it off and go back to simple Lua. J:


OT: The goroutine API is EXACTLY(well, 99.99%) what I wanted! This is wonderful! Topic is finished!
theoriginalbit #25
Posted 10 January 2013 - 07:21 PM
- snip -

First of all, blah to assembly. (One semester away from a Bachelor of Science in computer systems. I see plenty more assembly in my future)
Second of all, and while trying to not sound like an ass, I do know about the inaccuracies of the sleep function. Given that this is ComputerCraft and we're not going to be able to use more than one cpu, we'll never get two of our programs truly running in parallel.

Edit - although apparently I still need a class in grammar
It was more of a general pointed statement, not directed at you, you just reminded me about it when posting the reply and I figured I should say it since we were helping with a system that has to be so precise.