Jump to content

goto line


Castillo

Recommended Posts

Posted

hey, i have a question, i want to create a system for "goto line" like in notepad, but my question is how to? if someone could give me a example of something i will be very appreciated.

Thanks in advance.

Posted

Zango got the point ;) i'm creating a in-game script editor, so to be easier to go to a line i need something like notepad has, goto line, but my problem is how to make that? i'm using a memo btw.

  • Discord Moderators
Posted

are lines just separated by enter? Or do they properly include \n (newline)

If not, when client presses enter and the memo is in focus you could set it to secretly add \n to the string

this way, you can count the amount of times \n has been used in your string, or alternatively plus a variable 'lines' by 1 each time

Posted

i've tried this,

guiMemoSetCaretIndex(script_memo,tonumber(line)) 

but when i set to example: line 50 it doesn't sends me there :roll: but at same time when i do line 1 it moves to start of script o_O

Posted
i've tried this,
guiMemoSetCaretIndex(script_memo,tonumber(line)) 

but when i set to example: line 50 it doesn't sends me there :roll: but at same time when i do line 1 it moves to start of script o_O

index is not line index, its character index.

you can calcutate positions of line breaks (\n) and move to the next symbol by guiMemoSetCaretIndex

Posted
  
function explode(div,str) 
  if (div=='') then return false end 
  local pos,arr = 0,{} 
  -- for each divider found 
  for st,sp in function() return string.find(str,div,pos,true) end do 
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider 
    pos = sp + 1 -- Jump past current divider 
  end 
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider 
  return arr 
end 
  
local mystring = "some\ntext\nbla\nbla\nbla" -- this text have 5 lines 
local exp = explode("\n", mystring) 
  
local targetLine = 3 
  
local index=0 
for i=1, targetLine-1 do 
  index = index + exp[i].len 
end 
  
guiMemoSetCaretIndex(memo, index) -- i have my brain overheated, maybe you will need to add 1 to index to make it work correctly (but probably not) 
  

sorry if its buggy ;]

Posted

can be used instead, but explode will split via any string (like "i rule-:-and this is 2nd string", then explode("-:-", string)), this requires ASCII number (for new line it will be 10 BTW (or 13? but 13 was for \r afair)

Posted

i don't get how to implement your code varez in my script.

btw it gives an error at this line: index = index + exp.len

attempt to perform arithmetic on field 'len' (a function value)

edit: fixed error, index = index + exp:len()

edit2:

i've tryed to implement it, question: do i have to add:

local mystring = guiGetText(script_memo) 
local exp = explode("\n", mystring) 
local targetLine = tonumber ( guiGetText ( script_memo ) ) 
local index=0 
for i=1, targetLine-1 do 
  index = index + exp[i]:len() 
end 
guiMemoSetCaretIndex(script_memo,index) 

in the event when click "goto line" button? or let it external?

Posted
in the event when click "goto line" button? or let it external?

add it as a function and call it to get index for guiMemoSetCaretIndex when "goto" button clicked:

function getLineIndex(mystring, targetLine) 
  local exp = explode("\n", mystring) 
  local index=0 
  for i=1, targetLine-1 do 
    index = index + exp[i]:len() 
  end 
  return index 
end 
  
-- onClientGUIClick your "goto line" button: 
guiMemoSetCaretIndex(script_memo, getLineIndex(guiGetText(script_memo), guiGetText(someLineNumberEditBox))) 

Posted (edited)

i get an error: attempt to perform arithmetic on local 'targetLine' (a nil value)

code:

function explode(div,str) 
  if (div=='') then return false end 
  local pos,arr = 0,{} 
  for st,sp in function() return string.find(str,div,pos,true) end do 
    table.insert(arr,string.sub(str,pos,st-1)) 
    pos = sp + 1 
  end 
  table.insert(arr,string.sub(str,pos)) 
  return arr 
end 
  
local mystring = guiGetText(script_memo) 
local exp = explode("\n", mystring) 
local targetLine = tonumber ( guiGetText ( script_memo ) ) 
  
local index=0 
for i=1, targetLine-1 do 
  index = index + exp[i]:len() 
end 
  
function getLineIndex(mystring, targetLine) 
  local exp = explode("\n", mystring) 
  local index=0 
  for i=1, targetLine-1 do 
    index = index + exp[i]:len() 
  end 
  return index 
end 

Edit: i set targetLine to 3 and no errors, i do goto line 1 and it moves to start of script, but my problem is: how i can do something to select the line?

Edited by Guest
Posted

well i dont know how your editBox called where client puts line number to go to. replace someLineNumberEditBox with that variable

function explode(div,str) 
  if (div=='') then return false end 
  local pos,arr = 0,{} 
  for st,sp in function() return string.find(str,div,pos,true) end do 
    table.insert(arr,string.sub(str,pos,st-1)) 
    pos = sp + 1 
  end 
  table.insert(arr,string.sub(str,pos)) 
  return arr 
end 
  
--[[-- you don't need this part:  
local mystring = guiGetText(script_memo) 
local exp = explode("\n", mystring) 
local targetLine = tonumber ( guiGetText ( script_memo ) ) 
  
local index=0 
for i=1, targetLine-1 do 
  index = index + exp[i]:len() 
end  
--]]-- since its in a function now 
  
function getLineIndex(mystring, targetLine) 
  local exp = explode("\n", mystring) 
  local index=0 
  for i=1, targetLine-1 do 
    index = index + exp[i]:len() 
  end 
  return index 
end 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...