Posted: 28th Jun 2007 21:23
Hello, Please forgive my simple natured question as I am new. I was going to post under the 'newcomers' thread but after reading some posts there about people wanting to use DBP to make PS3 games with hundreds of models etc, (royalty/license free might i add), I realized it was not the place to ask. LOL

My question is how do end a prgram so that it returns to and continues to exectute from Line 1 again?
Posted: 28th Jun 2007 21:38
One way is to use do...loop
+ Code Snippet
do
  [insert your code here]  
loop
Posted: 28th Jun 2007 21:48
There's only one problem with working this way - all of your variables will still be set, and all of your media will still be loaded (resulting in 'Object already exists' type errors).

You can use that to your advantage though by only loading your media if a certain variable is set to zero, and after loading setting it to a non-zero value.
Posted: 29th Jun 2007 2:58
Thank you, but can you have a loop within a loop? Sort of like:

DO
DO
CODE
LOOP
LOOP
Posted: 29th Jun 2007 4:48
Here is a code example of mine to better explain. I would like it to clear screen (CLS) and continue again from line 1 instead of terminating.

BTW, how funny is Russian reversal?

+ Code Snippet
Rem Project: Russian Reversal by Chernobyl
Rem Created: 6/27/2007 7:56:59 PM

INK RGB(92,136,237),1
PRINT "In USA you <VERB> (a/an/the) <NOUN>!"
INK RGB(255,255,255),1

INPUT "Enter a verb for the sentence, then push [ENTER] ";verb$
INPUT "Enter a noun for the sentence, then push [ENTER] ";noun$

PRINT " "
INK RGB(92,136,237),1
PRINT "In USA you "; verb$; " (a/an/the) "; noun$
INK RGB(255,255,255),1
PRINT "Press [ENTER] to see the Russian Reversal..."
WAIT KEY

PRINT " "
INK RGB(255,0,0),1
PRINT "In Soviet Russia "; noun$; " "; verb$; " YOU!!!"
INK RGB(255,255,255),1
PRINT " "
PRINT " "

INK RGB(162,162,162),1
PRINT "For an explanation of this Russian Reversal"
PRINT "phenomenon, press [i]. Press [o] for more"
PRINT "options or [ESC] to exit..."

DO
 IF INKEY$() = "o" THEN PRINT "Some day there will be options"
 IF INKEY$() = "i" THEN EXECUTE FILE "http://en.wikipedia.org/wiki/Russian_Reversal","",""
LOOP

END

Posted: 29th Jun 2007 12:49
The use of GOTO is never suitable. If you used a GOTO, get rid of it. If you can't you wrote your code wrong.

There are thouands of ways of programming the same task. Not all off them are equal, some run slower than others, some are less clear than others, and some use GOTOs.

There is only one circumstance you will find yourself needing a GOTO: when you didn't properly plan your program.
Posted: 29th Jun 2007 13:59
Your tiny program using GOTOs will devolve into a ghastly mess if you try to expand it. If your little "cool idea" program into a finished implementation of twenty thousands lines (or a million, or five million, or larger), those GOTOs will ruin your project. I've worked on project with several million lines of code. I know of what I speak.

I have made my living as a professional programmer for 15 years, and have been programming for 25. How you write your programs is your affair. You asked us for advice, not the other way round. If you applied for a job and as part if your interview process I found you using GOTOs, you would not be employed. END OF STORY. If you slipped though the next and attempted to use GOTOs (or a local language equivalent) you would be considered a POOR programmer by all of your peers. Be under no illusions about that.

I have given you my advice, you can do with it as you like.

The worst of you, a n00b with a GOTO and an attitude is using GOTOs will make you a lazy programmer, and stop you thinking about what you want to program head of time. If you produce a complex project without GOTOs, your coding skills will have improved because of it. It's up to you whether you consider good programming skills to be an asset to you or not. You seem to think you'd rather be a poor programmer than not a programmer. I think you are aiming your sights too low. You should aim to be an excellent programmer. I have yet to meet an excellent programmer who doesn't scoff at GOTOs.

You might be the first. Good luck on that mate!
Posted: 29th Jun 2007 20:26
Thanks for the input, i guess a goto would work here since this is just a silly joke program, but now id like to know other ways to go about it since this seems to be a bad programming habbit as you point out. Can you point me somewhere where I can look this up in general?

thanks
Posted: 29th Jun 2007 20:42
I agree with vibe 100%. you should never get into the habit of using goto in your programs. It's like taking cocaine. Might seem like a harmless good idea at the time, but it'll slowly destroy your life, the automobile industry and maybe even the earth orbit!

(Ok, so I was kidding about the destruction of the automobile industry and the earths orbit, but you catch my drift.)

Rather that use goto, use do loop, possibly even gosub return, (but I wouldnt even use that.) Just put your initial variable settings and media loading outside of a do loop. There are many program examples which can be used as a starting point. Just read the help files included with DBPro. The fps tutorial as a great step by step introduction for new starters.

I hope that your problem was answered and that your program rocks.
Posted: 29th Jun 2007 21:49
Thanks Mobiius! By the way, what about calling up the code as a function (a very big function), is that done?

BTW you need some more RAM
Posted: 29th Jun 2007 22:17
I'm into DBPro and the idea of coding in general, although I'm not very good at it. Why do you say that Goto and any equivalent commands are bad? You've said that they're lazy and entangling and such, but I'm still unsure why you say they're actually... well, bad. Don't get me wrong, I'm not pretending to know anything about this, but I'd be interested to know why it is a bad thing. Is it because, if you use Goto too much, you'd get a sort of spaghetti/spiderweb arrangement, with bits of code suddenly leaping into other bits of code?
Posted: 29th Jun 2007 22:31
i am a living example of vibes explanation. i was making a cool game called snack attack, consisted of u running around and collecting food, dodge baddies, and flying forks. i used gotos cause no one ever told me why not to. every thing was going fine until level 4. then when i tried to use a goto to go to level 1, i went to level 4, when i tried to go to level 2 and 3, it was level 4. back to the menu, level 4. and to any other option or code section, i always went to level4. PAIN IN THE behind, i stopped the entire program out of frustrasion. (it wasnt the only huge problem it had)

attachted is the 3 levels of the game before it went craptacular on me.

EDIT: now, i use function a tun. i have main game loop where lots of functions are called when varirables are 1; like menu functions, health checks, enemy spawning, almost everything.
Posted: 29th Jun 2007 23:13
You're going to need to learn loops, subroutines, and functions. I strongly recommend the Beginner's Guide to DarkBASIC Programming, or alternatively check out the Hands-On DarkBASIC book available here on TGC. Either of these will give you a great understanding of how subroutines and functions work, and they'll help you make your games and apps 10x better .

A subroutine is essentially a block of code that you can run whenever you need it. When you call a subroutine using the gosub command, your program will go to that subroutine, execute each line in that subroutine, and branch back to where you started. Let's say we wanted to make an overly-complicated rendition of Hello World. This is what your main block of code would look like:
+ Code Snippet
cls
gosub hello
wait key
end

Then you'd need to create the subroutine somewhere below that. Always remember that subroutines should go at the end of your program, and functions (which I'll get to shortly) should go below subroutines. Anyway, the subroutine would look like this:
+ Code Snippet
hello:
print "hello world"
return

Notice the command named "hello:" -- it has a colon ( : ) as the end of it... that will define the name of the subroutine. Then, I used the command "return" to tell the program to go back to the line where it last came from. When you use "return," it will take you back to the line directly below the "gosub" command you used earlier. And unlike the testy "goto" command, the "gosub/return" commands will return you to wherever it came from, regardless of the line number, so you'll never need to worry about expanding your code. Some people will tell you subroutines are worthless... they're wrong. Just about any game or piece of software you've ever seen uses subroutines. Even FPS Creator's engine uses them .

Next there's functions. Functions are like subroutines, but they're intended to be used for repetetive tasks. For instance, you might put mouse or keyboard control into a single function, and display information into another. Functions are a little more complicated and to some, they can seem downright confusing at first, but once you start using them you won't be able to stop.

Okay, let's make a quick function that prints "hello world." First we'll need some code that calls the function:
+ Code Snippet
cls
hello()
wait key
end

We replaced "gosub hello" with "hello()"... this is what we're going to call our function. Now let's create the function:
+ Code Snippet
function hello()
print "Hello World!"
endfunction


It's that easy. You should experiment with subroutines and functions and learn how to use them in your program, it's much more effective than using "goto" line after line, even in smaller programs

Why do you say that Goto and any equivalent commands are bad?

Well, saying that using "gosub/return" is bad is rubbish. Subroutines are perfectly adequate and in larger programs they're pretty much mandatory. And in some instances, functions won't be able to do what you need them to do... like, for instance, returning more than one value .

But using goto is a horrible coding practice, even for short programs. Let's take a look at hello world again:
+ Code Snippet
cls
print "Hello World!"
wait key
end

Simple enough, right? Well, let's say I want my little program to loop. I'll use a goto command:
+ Code Snippet
cls
print "Hello World!"
wait key
goto 1

Okay, no trouble so far. But wait! I want to change the color of my text to red, because white is boring. And I'll want to do that before the CLS command so I'll have to write:
+ Code Snippet
ink rgb(255,0,0),rgb(0,0,0)
cls
print "Hello World!"
wait key
goto 1

But now, my pointer isn't pointing where I need it to anymore. I'd have to change "goto 1" to "goto 2." And what if my program starts getting bigger? Like, let's say I used "goto" while making Cheney Hunter. The game only has 845 lines of code... but if I had to change something at line 6, then add a bunch of lines before it, I'd have to go and find every "goto" command and replace them, rather than just using subs and functions. I hope that explains it and doesn't just get people more confused lol.
Posted: 29th Jun 2007 23:27
Thankyou! I already knew about subroutines (god bless that FPS tutorial included with DBPro) and I only just realized the staggering implications of functions, but you've nicely clarified everything, and the basics are always good to refresh in my mind.
Posted: 29th Jun 2007 23:29
Thanks MATT!! This really helps!
Posted: 29th Jun 2007 23:41
no problem Yeah, functions seem kind of confusing at first but they're so outrageously useful. But be wary of overusing them... some people will put a single command in a function, and while it works great for my little tutorial there, in all practicality it's actually a bit of a waste. My rule with functions is, if something is going to be performed constantly, go with the function, but if it's just something that will be used once or twice, or something that will repeat only when the user wants to (say, a budget screen in an RTS game), then use the subroutine. But that's just me, everyone finds their niche eventually.
Posted: 30th Jun 2007 3:18
I've been programming for more than 30 years myself and I wouldn't dream of using a Goto in any of my programs.

I'm not saying that you can't write a program using Goto - of course you can. You just don't need to!

What I am saying is that in a procedural programming language, Goto is a legacy command and a throwback to the days when we had no functions or procedures. It was Goto or nothing back then.

Using Goto goes against the ethic of procedural programming.

The reason I (and millions of other programmers) preach against using it is because it's an open-ended, unconditional branch. In other words, it can send the program counter absolutely anywhere in your program and doesn't return!

So, say it jumps to execute 20 lines of code at the end of your program. What next? Well, you have to use another Goto to send it back. Then another, and another and so on. Before you know it, you have dozens of them!

Then try tracing through the flow of your program - leaping all over the place! In other words, 'Spaghetti code'...

Procedures and Functions do the same thing, but automatically return to where they were called from so you don't have to add return labels and Goto's to jump to them.

If you really can't be bothered making your programs procedural, then no problem - use Goto. Please, just don't tell others that it's a good way to do it.

You just need to put a few seconds more effort into designing your program with a proper layout and make it procedural - by learning how to use functions and procedures. To this end, try tutorial number 2 here:

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

A program which can only be written with Goto either does not exist or has no real-world use. I've not seen one yet though...

TDK_Man
Posted: 30th Jun 2007 14:12
Ah, the good old 'goto issue'. Or should I say bad instead?
Here's my essay for the year on that very subject:

[center]Use or non-use of GOTO is a religious choice[/center]

The source of the 'goto issue' is an open letter written in 1968 titled 'A Case Against the Goto Statement', which was renamed to 'Go To Statement Considered Harmful' when it was published. I wonder why, after all of these years, there are still discussions, arguments, and even flame wars over goto, when 'everyone knows' that you shouldn't use goto statements. Perhaps it's because 'goto is bad' is not a provable fact one way or the other. Maybe? At least consider it.

In a large majority of cases, there is indeed no need to use GOTO. It can lead to a mess if not used carefully. It can be replaced in *every case* by the application of flags, and wrapping of code into functions or subroutines.

That said though, it can also reduce complexity of code in some instances by avoiding those unnecessary 'flag' variables, or unnecessary comparisons in 'if', 'while', or 'repeat/until' blocks, or unnecessary wrapping of pieces of code into functions that may make a mess of your code, turning it into so much ... spaghetti. Huh? Did I really say that? Yup! Using goto can avoid spaghettifying your code.

See how that works? Either way you can have a mess of spaghetti. One can be the fix for the other. That's what programming is about - it's all a series of trade-offs where you might do something you don't like in one place in your code to make things easier in lots of other places elsewhere in your code. Or maybe you might use a command you wouldn't normally because it uncovers code that was hidden by all of the flags/comparisons etc that were used to avoid a goto.

IMO, the real cause of spaghetti code is lack of design, not the goto statement.

As for providing 'real world' examples of the goto statement in action, that's an easy one. Download the source code for the Linux kernel, search all of the C source files for 'goto' and see how many occurences you get. And this is written in a language that arguably has more flexible conditional statements than DBPro has.


If you write a piece of code that would be simplified with a goto statement, add it and comment it... Or not if your religious persuasion doesn't allow you to. It's your choice, but at least consider the options. And don't accept dogmatic statements as fact without at least a little investigation.

Oh, and don't make dogmatic statements as if they were fact.

Finally, try to avoid the use of goto's (Note, I'm not say 'Do not use them', I said 'avoid'). Good code will generally have very few goto's in.


Summary:
Goto isn't evil: It's a tool in the DBPro toolbox. Sometimes what you want/need is 'an open-ended, unconditional branch' that goes precisely where you want it to.
Posted: 30th Jun 2007 17:22
nicely said.
Posted: 30th Jun 2007 18:18
I had no idea that 'goto' was such an issue :S Only time I've used them though was to check and avoid Key Spam.

Example:
+ Code Snippet
Rem Camera Key Skip Check ********************
If Camera_Key = Scancode() Then Goto Camera_Skip
Rem Set Camera Angle *************************
   If LeftKey()=1 Then Camera_A#=Wrapvalue(Camera_A#+90) `rotate camera clockwise
   If RightKey()=1 Then Camera_A#=Wrapvalue(Camera_A#-90) `rotate camera counter clockwise
Rem Camera Key Skip **************************
Camera_Skip: `end key spam
Camera_Key = Scancode() `get key for spaming


So I'll be spending the afternoon taking out my key spam code and finding an alternate route.

EDIT:
+ Code Snippet
Rem Camera Key Skip Check ********************
If Camera_Key <> Scancode()
Rem Set Camera Angle *************************
   If LeftKey()=1 Then Camera_A#=Wrapvalue(Camera_A#+90) `rotate camera clockwise
   If RightKey()=1 Then Camera_A#=Wrapvalue(Camera_A#-90) `rotate camera counter clockwise
Rem Camera Key Skip **************************
Endif `end key spam
Camera_Key = Scancode() `get key for spaming

only took 5 secs to find a solution probably not the best way, but it will work for me now

BWM