Jump to content




Struggeling struggle


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

#1 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 11 October 2012 - 06:59 PM

Simple and small question:
So in a program that I'm making I want to be able to jump back to a certain line of the code.
Is this possible? Or do I have to redo my script to make this work?

EDIT: Btw, the file I'm working on is the startup file, so I can do a os.reboot()...

EDIT 2: Yeah well I went with the os.reboot(), just because it was the simplest way of handeling it.

#2 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 11 October 2012 - 07:01 PM

Use functions? :P/>

#3 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 11 October 2012 - 07:03 PM

not possible because lua isnt inturpreted directly from a file, it compiles individual chunks, loads them, and runs them
think with subroutines and or tables to acomplish what you are trying to do :P/>

#4 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 11 October 2012 - 07:09 PM

View PostSnakybo, on 11 October 2012 - 07:01 PM, said:

Use functions? :P/>
Well yeah, but I don't think that would work that great in the code.
You see, the code checks the answer for the menu, and then goes with options from there. But in one point in the code I got so that if the method falies then retry.

So for a clearer view of the code (step by step):
  • checks for servers (in-game, via rednet)
  • gives you the option to select a server
  • once a server is selected a certain code will execute
  • now, if it didn't find any servers it gives you the option to retry
  • if you say "yes" it should run from step 1 again, else it will run some other code...


#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 11 October 2012 - 07:17 PM

I saw a thread about a week ago where someone asked this, I remember that no one gave him an answer. So I doubt you can ;)/> Gna have to use functions :P/>

#6 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 11 October 2012 - 07:26 PM

it might be harder than you think, if you pile functions you will get a stack overflow
im working on an api to make this easier to do, basically it will allow you to use a sort of goto command that will run functions but not use up the stack
also a os modification that will essentially make the stack go on forever

#7 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 11 October 2012 - 07:41 PM

View PostPixelToast, on 11 October 2012 - 07:26 PM, said:

it might be harder than you think, if you pile functions you will get a stack overflow
im working on an api to make this easier to do, basically it will allow you to use a sort of goto command that will run functions but not use up the stack
also a os modification that will essentially make the stack go on forever

That would be awesome :3

#8 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 11 October 2012 - 07:49 PM

I got it working as I wanted, but maybe not the same way as I wanted...
But basically I just added some extra code to skip the other code if you reboot.

#9 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 11 October 2012 - 08:14 PM

Wait, I was wondering, can you run a function within a function?
That would actually do as I want (kind of)!
So for example if you got
local input = ""
function password()
	if input == "MyPassword" then
		return true
	else
		input = read("*")
		password()
	end
end

if password() then
    print("Correct password!")
end
Will this ^ work?

#10 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 11 October 2012 - 08:20 PM

View Postjag_e_nummer_ett, on 11 October 2012 - 08:14 PM, said:

Wait, I was wondering, can you run a function within a function?
That would actually do as I want (kind of)!
So for example if you got
local input = ""
function password()
	if input == "MyPassword" then
		return true
	else
		input = read("*")
		password()
	end
end

if password() then
	print("Correct password!")
end
Will this ^ work?

Calling 'password' from within 'password' is a bad idea. Recursion might overflow the stack. (Though you'd have to give input to read() a lot then :P/>)

#11 faubiguy

  • Members
  • 213 posts

Posted 11 October 2012 - 08:23 PM

You can use
local input = ""
function password()
    if input == "MyPassword" then
        return true
    else
        input = read("*")
        return password()
    end
end

if password() then
    print("Correct password!")
end

That's tail recursion and won't cause a stack overflow.

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 11 October 2012 - 09:10 PM

Guys, this is exactly what loops are for.

local serverFound, serverStuff = false, nil
while not serverFound do
  print("Server?")
  serverStuff = attemptToFindServer(read())
  if serverStuff then
    serverFound = true
  else
    print("Try again?")
    local input = read()
    if string.lower(input) ~= "yes" then break end
  end
end
--Here, serverFound can be tested. It will be true if we found one and false if we gave up.


#13 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 11 October 2012 - 09:21 PM

View PostLyqyd, on 11 October 2012 - 09:10 PM, said:

Guys, this is exactly what loops are for.

local serverFound, serverStuff = false, nil
while not serverFound do
  print("Server?")
  serverStuff = attemptToFindServer(read())
  if serverStuff then
	serverFound = true
  else
	print("Try again?")
	local input = read()
	if string.lower(input) ~= "yes" then break end
  end
end
--Here, serverFound can be tested. It will be true if we found one and false if we gave up.
I didn't want to use a loop, but I guess it's the most compact option





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users