Jump to content

Frage an Lua-Spezialisten


EASY

Recommended Posts

Hallo,

ich habe ein Script, das sich in mehrere Blöcke aufteilt die nacheinander abgearbeitet werden sollen.
Je nach Anbangsbedingung soll an einer bestimmten Stelle jedoch abgebrochen werden...

Ein einfaches Beispiel wäre:
 

function test(x)
if x==0 then return end 
print("Block1")
if x==1 then return end
print("Block2")
if x==2 then return end
print("Block3")
if x==3 then return end
print("Block4")
if x==4 then return end
print("Block5")
end

test(1):
Block1

test(4):
Block1
Block2
Block3
Block4

test(5):
Block1
Block2
Block3
Block4
Block5

test(0):
sofortiger Abbruch

... gibt es da noch eine andere Vorgehensweise, wie man das umsetzen könnte (... ohne if... if... if ..)

Gruß
EASY

Link to comment
Share on other sites

tab = {
 function() print("Block 1") end,
 function() print("Block 2") end,
 function() print("Block 3") end,
 function() print("Block 4") end,
 function() print("Block 5") end
}

function test(x)
  for i=1, x do
    tab[i]()
  end
end

test(1)
print()
test(4)
print()
test(5)
print()
test(0)

:D

Link to comment
Share on other sites

Deine Tabelle ist okay, Andy, aber die Konstruktion deiner Testfunktion entspricht nicht dem, was EASY sucht.

Mein Fehler! Deine Funktion arbeitet richtig, meine hingegen falsch! (Nach dem vierten Mal hinschauen habe ich es dann endlich begriffen. :D )

Sorry!

Edited by Goetz
Falschaussage korrigiert
Link to comment
Share on other sites

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
×
×
  • Create New...