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

unkown reason for returning nil

Started by Hydrotronics, 22 February 2016 - 05:02 PM
Hydrotronics #1
Posted 22 February 2016 - 06:02 PM
so, i'm tying to make an interaction API for the advanced computer to make making thigns like buttons and centered text etc. a bit easier.
While creating a function for finding the center of the screen, i come across an issue.
I read up on the return statement to see if I was using it wrong and from what I could gather I was using it correctly.

Code:

function term.getCenter()
  local hscreenX = screenX / 2
  local hscreenY = screenY / 2
  do return hscreenX, hscreenY end
end

screenX and screenY are called earlier with

screenX, screenY = term.getSize()


When I test this function out, it returns Y as nil.

Code in test program:

local x, y = term.getCenter()

print(tostring(x))
print(tostring(y))
KingofGamesYami #2
Posted 22 February 2016 - 06:06 PM
I placed your code in a test file:


screenX, screenY = term.getSize()

function term.getCenter()
  local hscreenX = screenX / 2
  local hscreenY = screenY / 2
  do return hscreenX, hscreenY end
end

local x, y = term.getCenter()

print(tostring(x))
print(tostring(y))

Exactly as you stated. I got two numbers.
Hydrotronics #3
Posted 22 February 2016 - 06:07 PM
Exactly as you stated. I got two numbers.

But i didn't :(/>
I was supposed to but i only got 1 number, which was the X axis. I never got the Y axis one

edit: When I run it on a normal computer I get 26 then nil
Edited on 22 February 2016 - 05:08 PM
KingofGamesYami #4
Posted 22 February 2016 - 06:08 PM
So what in your code differs from the way mine is written?
Hydrotronics #5
Posted 22 February 2016 - 06:09 PM
the function and the program are in different programs. the function in .IAPI and the test program in test
KingofGamesYami #6
Posted 22 February 2016 - 06:11 PM
My new setup is:

api file

screenX, screenY = term.getSize()

function term.getCenter()
  local hscreenX = screenX / 2
  local hscreenY = screenY / 2
  do return hscreenX, hscreenY end
end

test file:


os.loadAPI( "api" )

local x, y = term.getCenter()

print(tostring(x))
print(tostring(y))

..still getting two numbers.
Hydrotronics #7
Posted 22 February 2016 - 06:12 PM
for some reason after renaming the function to term.getCentter() it worked…
why? XD

I now name it back and it works….
well this is interesting! case closed?
Edited on 22 February 2016 - 05:13 PM