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

External Program Functions Problem

Started by BlueZero, 05 February 2013 - 11:05 PM
BlueZero #1
Posted 06 February 2013 - 12:05 AM
Title: External Program Functions Problem

So I've set up a little program and had some changes made to it with a little help from the IRC users but can't seem to figure out how to get this to work. So here's my code and I'll explain what I'm doing:

Program 1:


function pasteCall()
output = http.request("http://pastebin.com/raw.php?i=sZ8apNAt")
return output
end

pasteCall()

Pastebin Program 2:

function pasteRespond()
shell.run("clear")
print("We're here, waddup?")
sleep(2)
shell.run("clear")
end

pasteRespond()

So far what you can see from this code, I've set it up so it takes the raw paste information and loads it as a function externally and runs the function without having it in the code itself. This kind of setup is what I'll be using in one of my projects but I can't seem to figure out how to get Program 2's print functions to actually come through to the shell. So far what it seems to do is just run in the background and when the program's done, it finishes like a program normally would. This stuff works with making instructions for creating files and directories without having them having the file itself, but the interactive portion is what's being left out.

I'm probably doing it wrong, or something is being coded incorrectly for what I'm trying to achieve, but hopefully someone can help me figure this out.
Doyle3694 #2
Posted 06 February 2013 - 04:03 AM
I'm pretty sure http.request() sends a event when done rather than returning to a variable
BlueZero #3
Posted 06 February 2013 - 02:24 PM
So it's not possible to run it like I'm wanting it to?
theoriginalbit #4
Posted 06 February 2013 - 02:36 PM
Here is the solution for pasteCall in the simple way you are wanting to do it

function pasteCall()
  local output = http.get("http://pastebin.com/raw.php?i=sZ8apNAt")
  if output then
	local contents = output.readAll()
	output.close()
	return contents
  else
	print("Empty response")
	return false
  end
end

Here is the way it needs to be done with request

local function pasteCall()
  local requestUrl = "http://pastebin.com/raw.php?i=sZ8apNAt"
  http.request(requestUrl)
  while true do
	local event, url, content = os.pullEventRaw()
	if event == "http_success" and url == requestUrl then
	  if content then
		local data = content.readAll()
		content.close()
		return data
	  else
		print("No content")
		return false
	  end
	elseif event[1] == "http_failure" and url == requestUrl then
	  print("No response from server")
	  return false
	end
  end
  error("Error: Something went really wrong, it should never get here")
end
Edited on 06 February 2013 - 02:53 PM
BlueZero #5
Posted 06 February 2013 - 02:51 PM
Seems both those ways end up not doing anything. This is basically what I'm seeing on my end when I do that:


CraftOS v1.4
> cd bluesoftware
bluesoftware> pastecall
bluesoftware>

And that's running both ways.
Orwell #6
Posted 06 February 2013 - 02:58 PM
Seems both those ways end up not doing anything. This is basically what I'm seeing on my end when I do that:


CraftOS v1.4
> cd bluesoftware
bluesoftware> pastecall
bluesoftware>

And that's running both ways.
You need to append the line

pasteCall()
at the bottom of the program… It calls the defined function, like in your opening post.
theoriginalbit #7
Posted 06 February 2013 - 02:58 PM
Thats because what I wrote are functions. you need to call the functions in the program.

EDIT: Damn ninja'd
BlueZero #8
Posted 06 February 2013 - 03:06 PM
Naw, what I stated above does for the first one, but the second one just stalls all to crap and doesn't do anything.

What I'm doing with it is that's the whole program in itself. I listed code that isn't just a small part of a huge program, that literally is the program.

Then again, I'm also using CCEmulator for my programming too, though that shouldn't really be a problem. The second one is really weird though with how it reacts. I have to restart the computer for it to get out of the program itself seeing as it doesn't take me back to a shell.

Edit: Though I notice in the first one there's a slight pause before it pops back into the shell after seemingly not doing anything. I wonder if it's still running the code and pausing like I'm telling it to within the other function but not displaying anything.
Orwell #9
Posted 06 February 2013 - 03:09 PM
Naw, what I stated above does for the first one, but the second one just stalls all to crap and doesn't do anything.

What I'm doing with it is that's the whole program in itself. I listed code that isn't just a small part of a huge program, that literally is the program.

Then again, I'm also using CCEmulator for my programming too, though that shouldn't really be a problem. The second one is really weird though with how it reacts. I have to restart the computer for it to get out of the program itself seeing as it doesn't take me back to a shell.
It's this line in TheOriginalBit's second script:

local event, url, content = { os.pullEventRaw() }
Change that to:

local event, url, content = os.pullEventRaw()
BlueZero #10
Posted 06 February 2013 - 03:11 PM
Naw, what I stated above does for the first one, but the second one just stalls all to crap and doesn't do anything.

What I'm doing with it is that's the whole program in itself. I listed code that isn't just a small part of a huge program, that literally is the program.

Then again, I'm also using CCEmulator for my programming too, though that shouldn't really be a problem. The second one is really weird though with how it reacts. I have to restart the computer for it to get out of the program itself seeing as it doesn't take me back to a shell.
It's this line in TheOriginalBit's second script:

local event, url, content = { os.pullEventRaw() }
Change that to:

local event, url, content = os.pullEventRaw()

Yeah, it seems to do the same thing. It runs it, but pauses for a sec, then brings me back to the shell. Doesn't clear the shell or anything or do the print command.
Orwell #11
Posted 06 February 2013 - 03:13 PM
* snip *

Yeah, it seems to do the same thing. It runs it, but pauses for a sec, then brings me back to the shell. Doesn't clear the shell or anything or do the print command.
Do you actually print it then? Like so:

print( pasteCall() )

The function pasteCall itself doesn't print the received content.
BlueZero #12
Posted 06 February 2013 - 03:17 PM
* snip *

Yeah, it seems to do the same thing. It runs it, but pauses for a sec, then brings me back to the shell. Doesn't clear the shell or anything or do the print command.
Do you actually print it then? Like so:

print( pasteCall() )

The function pasteCall itself doesn't print the received content.

No, I'm referring to my second program, pasteRespond. That's the thing it's calling on, but it doesn't seem to be "responding" so to speak. I notice it processes it though, which is what that pause means, but it doesn't pop through and run it's print("Sup.") like it should.
Orwell #13
Posted 06 February 2013 - 03:21 PM
* snip *

Yeah, it seems to do the same thing. It runs it, but pauses for a sec, then brings me back to the shell. Doesn't clear the shell or anything or do the print command.
Do you actually print it then? Like so:

print( pasteCall() )

The function pasteCall itself doesn't print the received content.

No, I'm referring to my second program, pasteRespond. That's the thing it's calling on, but it doesn't seem to be "responding" so to speak. I notice it processes it though, which is what that pause means, but it doesn't pop through and run it's print("Sup.") like it should.
Could you please post your exact code again? (Both programs if you use them both) Because I can't follow what adjustment you have made by now. (And the second program in your OP should work just fine as it's written there).
BlueZero #14
Posted 06 February 2013 - 03:25 PM

local function pasteCall()
  local requestUrl = "http://pastebin.com/raw.php?i=sZ8apNAt"
  http.request(requestUrl) 
  while true do
	    local event, url, content = os.pullEventRaw()
	    if event == "http_success" and url == requestUrl then
		  if content then
			    local data = content.readAll()
			    content.close()
			    return data
		  else
			    print("No content")
			    return false
		  end
	    elseif event[1] == "http_failure" and event[2] == url then
		  print("No response from server")
		  return false
	    end
  end
  error("Error: Something went really wrong, it should never get here")
end
pasteCall()


function pasteRespond()
shell.run("clear")
print("We're here, waddup?")
sleep(2)
shell.run("clear")
end
pasteRespond()

Have you given it a try on your end? Maybe something might just be bugged on my end.
theoriginalbit #15
Posted 06 February 2013 - 03:28 PM

local function pasteCall()
  local requestUrl = "http://pastebin.com/raw.php?i=sZ8apNAt"
  http.request(requestUrl)
  while true do
    local event, url, content = os.pullEventRaw()
    if event == "http_success" and url == requestUrl then
      if content then
        local data = content.readAll()
        content.close()
        return data
      else
        print("No content")
        return false
      end
    elseif event[1] == "http_failure" and url == requestUrl then
      print("No response from server")
      return false
    end
  end
  error("Error: Something went really wrong, it should never get here")
end

-- From here down is changed
local program = pasteCall()
local file = loadstring(program)
file()
BlueZero #16
Posted 06 February 2013 - 03:35 PM
Hmm, I gave that a try and it pauses for a second, then gives me: string:2: attempt to index ? (a nil value)
theoriginalbit #17
Posted 06 February 2013 - 03:41 PM
Hmm, I gave that a try and it pauses for a second, then gives me: string:2: attempt to index ? (a nil value)
Ugh sorry thats my fault. I forgot that the other program has shell.run in it

change the other one to this (removes calling the clear program)

function pasteRespond()
  term.clear()
  term.setCursorPos(1,1)
  print("We're here, waddup?")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
end

pasteRespond()
BlueZero #18
Posted 06 February 2013 - 03:44 PM
Holy crap, it worked! Haha! Thanks man, this is cool. Thanks for the help. Now I can get workin' on my project. Thanks again!
Orwell #19
Posted 06 February 2013 - 03:49 PM
The line

elseif event[1] == "http_failure" and event[2] == url then
should probably be this though:

elseif event == "http_failure" and url == requestUrl then
It's a mix-up from packing the event into a table or not. :P/> This will only have impact when you get no valid response.
theoriginalbit #20
Posted 06 February 2013 - 03:51 PM
The line

elseif event[1] == "http_failure" and event[2] == url then
should probably be this though:

elseif event == "http_failure" and url == requestUrl then
It's a mix-up from packing the event into a table or not. :P/> This will only have impact when you get no valid response.
I had actually changed that just like I changed the packing into the event, clearly I copied it from the text editor before I fixed those and forgot to copy it again… :/