Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to properly END the game?

Author
Message
The Slayer
Forum Vice President
16
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 1st Apr 2010 22:27
Is there a way to exit or end programmes in DBPro properly? I mean, I know that the command END is used to end the program, and my code is doing that, except for the fact that I always get an error screen saying that my Dark Basic Pro project is'nt working anymore and that it is searching a solution for the problem.

Here's a part of the Main Menu, where upon choosing choice 6 (Exit) and pressing the returnkey, the program ends:

Is there anything wrong with my code, or are there better ways for ending programs (without getting the error) ?



Thanx in advance!

Cheers

Slayer rules!!! Yeaaah, man!
No Time To Code
17
Years of Service
User Offline
Joined: 22nd Dec 2008
Location:
Posted: 1st Apr 2010 22:49 Edited at: 1st Apr 2010 22:49
I dont know if it's the reason for the error, but I would use a Repeat...Until loop as follows:



Also, I'm not sure what the return command is doing if it's not a subroutine???

It is not from the benevolence of the butcher, the brewer, or the baker, that we expect our dinner, but from their regard to their own interest. --Adam Smith
Math89
22
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 1st Apr 2010 22:57
I'm pretty sure the problem comes from the return, it may corrupt something somewhere.
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 1st Apr 2010 23:18 Edited at: 1st Apr 2010 23:21
I couldn`t test exactly your code as I don´t have your media, but I´v done some test in my laptop, and returnkey didn´t work, but controlkey or spacekey works fine. Any way, I would prefer just end the program using a variable...if health=0 then end


cheers!

I'm not a grumpy grandpa
No Time To Code
17
Years of Service
User Offline
Joined: 22nd Dec 2008
Location:
Posted: 1st Apr 2010 23:57 Edited at: 1st Apr 2010 23:58
Quote: "I couldn`t test exactly your code as I don´t have your media, but I´v done some test in my laptop, and returnkey didn´t work, but controlkey or spacekey works fine. "

I didn't notice the returnkey() command. I agree with chafari, just use spacekey() or you could use the keystate command. This example checks for the F1 key being pressed:



It is not from the benevolence of the butcher, the brewer, or the baker, that we expect our dinner, but from their regard to their own interest. --Adam Smith
The Slayer
Forum Vice President
16
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 1st Apr 2010 23:58
Hey guyz, thanks for helping! I've tried all those suggestions, but they did'nt work.

I think the main reason that my code does'nt work as it should, is because I gosub to the 'Main Menu'-code, BEFORE the main loop. Maybe it would be better if I make a function for the Main Menu, instead of a gosub routine?

Any other suggestions?

Cheers

Slayer rules!!! Yeaaah, man!
The Slayer
Forum Vice President
16
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 2nd Apr 2010 00:07
Hey guyz, I've just figured it out! Whoohoooooo!

At the start of my program, I deleted the command DISABLE SPECIAL KEYS, and now it works like a charm! Whoohooooo!

Cheers

Slayer rules!!! Yeaaah, man!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 2nd Apr 2010 00:26
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Apr 2010 01:04
Quote: "except for the fact that I always get an error screen saying that my Dark Basic Pro project is'nt working anymore and that it is searching a solution for the problem."


I've been getting that problem intermittently on a variety of projects when I try to exit a program but haven't found a common link yet.

Quote: "Darn, that's one of mine"


That can't be the cause, can it?
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 2nd Apr 2010 15:58
DISABLE SPECIAL KEYS doesn't work on Vista or Win7.... I don't think that would be the problem.

Am I missing something? The code snippet does nothing without the rest of the code....

I think ending the code like that
should work fine, but I would do the "end" statement outside of the loop.

I think it looks neater and if you want to unload something that DBP has no control over (not sure what that could be), then that's a good place for it.

The fastest code is the code never written.
The Slayer
Forum Vice President
16
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 2nd Apr 2010 19:55
Quote: "DISABLE SPECIAL KEYS doesn't work on Vista or Win7.... I don't think that would be the problem."

Yeah, your right, it does'nt work.

@IanM:
Maybe that's what caused the program to give me an error upon ending the game, IanM? Maybe worth checking that out?

Quote: "Am I missing something? The code snippet does nothing without the rest of the code...."

That bit of code was just a part of my Main Menu.

Quote: " but I would do the "end" statement outside of the loop."

Hum, working with goto's is'nt a good idea. I always try with subroutines or functions.

Cheers

Slayer rules!!! Yeaaah, man!
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Apr 2010 21:16
Why not use repeat/until or while/endwhile instead of do/loop? Then you can set the condition in the loop at the appropriate time and end neatly without messy GOTOs.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Apr 2010 01:20
Quote: "I don't think that would be the problem"

Yes it could be. If the DLL is unloaded without the key-capture I use being shut down correctly, you will get errors.

I'll check it out, however I'm also considering removing the command and its partner because it's getting harder and harder to keep it working correctly as MS tightens up security in their OS's ... not that it's working correctly ATM.

Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 3rd Apr 2010 05:43
Quote: "working with goto's is'nt a good idea"

I see that a lot on this forrum.... Is everyone afraid of GOTOs? Please give me a reason to not use them. I use them from time to time to go from tricky areas that I don't want to have several encapsulating IFs to accomplish something. I know that DBP stores the locataion of the goto for return purposes, but when you use them at the end of the program, you don't need to worry about it.

I would love to use something else if there is a good replacement for GOTO.

The fastest code is the code never written.
tiresius
23
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 3rd Apr 2010 08:27
Someone already gave a good replacement for the goto in this case, it was the suggestion to use repeat...until for the loop instead of do...loop. People aren't scared of goto's they just choose not to use them and suggest to others they shouldn't use them. There are very few cases where a goto is needed (and even that may be debatable), and their overuse can make code unmanageable.

There are a ton of goto vs gosub vs function threads on the board, you can search on goto and find a bunch of them for some fun reading.


A 3D marble platformer using Newton physics.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Apr 2010 16:37
@Hawkblood,
Quote: "I see that a lot on this forrum.... Is everyone afraid of GOTOs?"

Honestly, yes I think that they are.
I also wonder how many people have actually read the original article of 'GOTO considered harmful', and understand the context under which it was written, or have just read or heard the title (set by an editor with IMO an agenda, and not the original writer) and have taken to parroting that.

I suspect that by writing that, that I've insulted a large majority of my fellow forum-goers ... oops!

Anyhow, now it's time to start filling in that hole I've been digging a little.

I very very rarely use GOTO. I use it for jumping out of nested loops and flattening deeply nested if's, and for jumping to exception-handling code, but only where it makes the code easier to read/understand. All of the jumps are forward but within the same function, which makes my use of it predictable.

That said, I hardly ever use it in DBPro, because for the most part it's not necessary - you have EXIT (a forward-goto in disguise), or you can use EXITFUNCTION and RETURN in ways that cover mostly the same uses, but those last ones tend to obfuscate the code, so I try to avoid those.

Finally, I find your example of GOTO in your code unnecessary, therefore I would eliminate it too.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Apr 2010 17:46
Quote: "I suspect that by writing that, that I've insulted a large majority of my fellow forum-goers ... oops!"


Don't worry. I'm not sure whether you have or not. I'm still puzzling over whether or not the

Law of excluded middle

applies to your statement.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Apr 2010 18:38
Nope, I think that the truth of 'GOTO is bad' is less boolean, and more fuzzy, which means that law doesn't apply to my position.

In a nutshell, I don't believe that the statement 'Overuse of 'A' is bad' is equivalent to 'All use of 'A' is bad', and I don't accept that in the case of the GOTO statement.

I take a pragmatic view of the GOTO statement and not a dogmatic view - I'm willing to be swayed either way by the evidence, where other people appear to be nailed to their positions. I agree that there are bad uses of it, and that overuse is bad, but I've seen good uses of it too.

I understand that it's always possible to avoid the use of GOTO, but it's got to be worth the effort too, and it's got to be readable and understandable at the end of it ... and understandable in a years time too.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 3rd Apr 2010 18:46 Edited at: 3rd Apr 2010 18:50
Quote: "I know that DBP stores the locataion of the goto for return purposes, but when you use them at the end of the program, you don't need to worry about it."


Actually you're thinking of 'gosub'. Goto doesn't store any information for return purposes. Hence, you can't return from a goto.

In this case your use of 'goto' is fine, if a little pointless.

Unless you are going to immediately end the program you shouldn't use goto (or gosub) to:
- Jump between/out of/into functions or subroutines because it will corrupt the stack possibly causing a stack overflow/underflow
- Replace structured code (eg. if/else/while/do/for/repeat) because it will make the code unreadable

Overall, it's probably better to tell inexperienced coders to simply not use 'goto' at all since they are extremely unlikely to encounter a valid reason to using it, and are probably not able to judge for themselves whether or not it is OK to use a goto in a particular situation.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Apr 2010 20:38
Quote: "I take a pragmatic view of the GOTO statement and not a dogmatic view - I'm willing to be swayed either way by the evidence"


Actually, so am I - but I still have a definite preference to avoid it, swayed partly by the desire to avoid encouraging others into bad habits.

Quote: "I understand that it's always possible to avoid the use of GOTO, but it's got to be worth the effort too, and it's got to be readable and understandable at the end of it ... and understandable in a years time too."


Agreed - and every time I try to read someone's code with more than one GOTO in it I groan.

Quote: "Overall, it's probably better to tell inexperienced coders to simply not use 'goto' at all since they are extremely unlikely to encounter a valid reason to using it, and are probably not able to judge for themselves whether or not it is OK to use a goto in a particular situation."


I've been tempted by that argument - but the downside is that people new to coding often find GOTO easier to understand than repeat/until, while/endwhile, select/case/endcase/endselect, etc. You can warn them of the potential downsides of GOTOs but let them do some coding they understand first.

Ancient FORTRAN used to have an arithmetic IF statement (I forget the precise syntax now) which allowed a three-way conditional GOTO. It was something like



That was probably just as readable as modern (i.e. since about 1970 ) more structured code.

I can't remember the last time I deliberately used a GOTO.
Wobbelwap
16
Years of Service
User Offline
Joined: 24th Apr 2010
Location: Metropolitan Soesterberg
Posted: 25th Apr 2010 00:02
Hi there, just to respond to the original topic. I had the same problem until 2 minutes ago.

I'm now using DB Pro in compatibility mode Win XP Sp2.

Hope this will help.

Login to post a reply

Server time is: 2026-07-26 21:11:57
Your offset time is: 2026-07-26 21:11:57