Forum
CS2D Scripts Is there goto statement?Is there goto statement?
11 replies 1
What it does is what it says, it goes (jumps) to a 'labeled' location in your code and skips over everything inbetween.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
goto gohere local a = 3 print('This will never be printed!') ::gohere:: -- Jumps here and skips everything inbetween -- works for both directions (up and down) ::gethere:: --This will pretty much be an infinite loop, because it runs everything inbetween here --and then the goto statement jumps back to '::gethere::' and this part runs again goto gethere
There are only few cases were goto is a good option (e.g. leaving nested loops). Even for these cases there are ways to do it without goto.
TrialAndError has written
There is a goto statement in Lua >=5.2 and in the LuaJIT version, CS2D Uses Lua 5.1, that means we cant use that
Is it possible to put this feature into CS2D?
DC has written
Even for these cases there are ways to do it without goto.
What exactly are these ways? Could you show me an example, please?
The Dark Shadow has written
Is it possible to put this feature into CS2D?
Sorry, but if you want to have gotos in your code, your code is horrible. There is no case in which you want to have goto. Write cleaner code. Forget all about goto. All of it. Don't even remember that it has existed at one point TrialAndError has written
There is a goto statement in Lua >=5.2 and in the LuaJIT version, CS2D Uses Lua 5.1, that means we cant use that
Is it possible to put this feature into CS2D?
Edited TrialAndError's sample.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
goto gohere local a = 3 print('This will never be printed!') function test(parameter) print(parameter) end ::gohere:: -- Jumps here and skips everything inbetween -- works for both directions (up and down) ::gethere:: --This will pretty much be an infinite loop, because it runs everything inbetween here --and then the goto statement jumps back to '::gethere::' and this part runs again goto gethere
gotostatement was added to enhance clarity in breaking out of nested loops, cleanly implement state machines and their close relative, interpreters.
Of course, you can't use GOTO indiscriminately. Hohndel gave a few simple rules for the correct use of GOTO:
Only jump forward, never backward.
Only jump to the end of a block. (It doesn't have to be the end of the function, but usually is.)
Use good names for the label (e.g. goto error_cleanup;).
Use it consistently.
A conditional goto (if X jump to Y) is the most basic and fundamental control structure and it can be used to implement all other control structures.
People quickly noticed that goto leads to bad code in most cases and so they replaced it with better, cleaner control structures like if, else, elseif, for, while etc. These highly improve the readability of code.
I'm not sure why so many languages still allow to use goto to be honest. It's not bad in general but the risk that it is misused is very high.
@ The Dark Shadow: That sample is impracticable and makes no sense but whatever. A simple approach to simulate goto without using goto is to use a loop and a state variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- skip initial code; equal to your 'goto gohere' local state = 2 while true 	if state == 1 		local a = 3 		print('This will never be printed!') 		function test(parameter) 			print(parameter) 		end 	-- this is your '::gohere::' 	elseif state == 2 		-- do things, continue with state 3 afterwards 		 state = 3 	-- this is your '::gethere::' 	elseif state == 3 		-- do things. infinite loop unless state is changed inside 	end end
In this code, setting state to 1, 2 or 3 is nearly equal to using a goto in your code. To make it entirely equal when called anywhere in the loop you also would have to use "continue" - which does not exist in Lua. Of course there are ways around that as well. e.g. put the code into methods and use return instead of continue.
The thing is: If you need to jump around in your code like crazy with goto then your code is very bad and you should restructure it.
EDIT: One more question, Is it possible to put lua 5.3.5 into CS2D like the LuaJIT? If LuaJIT is possible so Lua 5.3.5 should be possible too? Sorry, I have no knowledge about it.
See Update LUA
1