Jump to content




unkown reason for returning nil


  • You cannot reply to this topic
6 replies to this topic

#1 Hydrotronics

  • Members
  • 131 posts
  • LocationThe hall of 1000 monkeys and only 1 typewriter

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))


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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.

#3 Hydrotronics

  • Members
  • 131 posts
  • LocationThe hall of 1000 monkeys and only 1 typewriter

Posted 22 February 2016 - 06:07 PM

Quote


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 by Hydrotronics, 22 February 2016 - 06:08 PM.


#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 22 February 2016 - 06:08 PM

So what in your code differs from the way mine is written?

#5 Hydrotronics

  • Members
  • 131 posts
  • LocationThe hall of 1000 monkeys and only 1 typewriter

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

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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.

#7 Hydrotronics

  • Members
  • 131 posts
  • LocationThe hall of 1000 monkeys and only 1 typewriter

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 by Hydrotronics, 22 February 2016 - 06:13 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users