Jump to content




x and y argument in term functions


5 replies to this topic

#1 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 21 October 2017 - 07:09 AM

It annoys me everytime i use it; the term.write, term.blit… functions don't have x and y arguments. You always have to set the cursor position to where you want to write, and then use the function. So many times i find myself with a code looking like this:
term.setCursorPos(1, 1)
term.write("text")
term.setCursorPos(1, 2)
term.write("another text")
term.setCursorPos(5, 1)
term.write("another another text")
So it would be cool to be able to use this code instead:
term.write("text", 1, 1)
term.write("another text", 1, 1)
term.write("another another text", 5, 1)


#2 CLNinja

  • Members
  • 191 posts

Posted 21 October 2017 - 11:01 AM

You could totally rewrite the term api and make this change yourself

function _G.term.write(string,x,y)
	 term.setCursorPos(x,y)
	 term.write(string)
end


#3 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 21 October 2017 - 11:13 AM

View PostCLNinja, on 21 October 2017 - 11:01 AM, said:


function _G.term.write(string,x,y)
	 if type(x) == "number" and type(y) == "number" then
		  term.setCursorPos(x,y)
	 elseif x ~= nil and y ~= nil then
		  error("x and y must be numbers or nil", 2)
	 end
	 term.write(string)
end

Improvement, make x and y optional

#4 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 21 October 2017 - 05:45 PM

If I'm not completely wrong neither of those will work because they will just call themself forever, solution:

local oldWrite = term.write
function _G.term.write(string,x,y)
		 if type(x) == "number" and type(y) == "number" then
				  term.setCursorPos(x,y)
		 elseif x ~= nil and y ~= nil then
				  error("x and y must be numbers or nil", 2)
		 end
		 oldWrite(string)
end


#5 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 22 October 2017 - 06:02 AM

I want to be able to use the function like it was before, without having to specify the x and y.
So my code looks like this:
local oldWrite = term.write
function _G.term.write(string,x,y)
  if not x or not y then
    local cur_x, cur_y = term.getCursorPos()
    x = x or cur_x
    y = y or cur_y
  end

  term.setCursorPos(x,y)
  oldWrite(string)
end

Edited by Jummit, 23 October 2017 - 04:37 PM.


#6 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 22 October 2017 - 03:04 PM

View PostJummit, on 22 October 2017 - 06:02 AM, said:

I want to be able to use the function like it was before, without having to specify the x and y.
So my code looks like this:
local oldWrite = term.write
function _G.term.write(string,x,y)
  local cur_x, cur_y = term.getCursorPos()
  x = x or cur_x
  y = y or cur_y

  term.setCursorPos(x,y)
  oldWrite(string)
end

The code in the post written by Luca_S is more efficient as it reduces function calls. term.write or oldWrite already uses the current cursor position is none is provided, therefore you don't need to set it in your replacement function if no x or y are provided. Additionally it will handle incorrect values for x and y correctly.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users