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 / going to new section

Author
Message
Jon wu
12
Years of Service
User Offline
Joined: 4th Nov 2013
Location:
Posted: 13th Feb 2014 17:10
hi I was wondering how I could go into a new section because my game has an enemy character in which a math problem comes out. I want to do it when the enemy disapers which is pressing the "a" key since it gets rid of the enemy. I want to do it so that when that happens it goes into a new section with new different maps and characters. All help is appreciated!


Hasty
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 13th Feb 2014 17:31
The first thing you need to do this, is the right structure in your program. So for example, take this piece:



and replace it with:



At the end of your source code, create this function:



Now, it is easier to create an associated function, DeInitLevel():



This may seem like a long way to do this, but as your game gets more complicated it will make it easier to manage the code changes, and find the bugs.

There are many more things you need to do to make this code work correctly on other PCs etc, but this is just a start.

Jon wu
12
Years of Service
User Offline
Joined: 4th Nov 2013
Location:
Posted: 18th Feb 2014 17:02
sorry im a little confused because im not really advanced with this program. Do you think you can give me an example

Hasty
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 18th Feb 2014 20:44
Quote: "sorry im a little confused because im not really advanced with this program. Do you think you can give me an example"



I will give you an example of a Subroutine, which is very similar to a function:




Jon wu
12
Years of Service
User Offline
Joined: 4th Nov 2013
Location:
Posted: 19th Feb 2014 17:16
I tried something similar to that but it wont load it just shows the image we loaded in which is a black screen. In our code when the score gets to 20, that's when we want to goto a new section

Hasty
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 19th Feb 2014 17:50 Edited at: 19th Feb 2014 17:55
Quote: "I tried something similar to that but it wont load it just shows the image we loaded in which is a black screen. In our code when the score gets to 20, that's when we want to goto a new section"


I noticed you're using gosub to go to your new section. This is incorrect. Gosub is used to branch out momentarily to a subroutine and then return to your loop, as in the example I posted.

To go to a completely new loop use GOTO (I know some will say to never use goto but I disagree. If used singularly it shouldn't hinder your program.)

Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 20th Feb 2014 17:01
Quote: " (I know some will say to never use goto but I disagree. If used singularly it shouldn't hinder your program.)"

No. GOTO should never be used.

Jon wu
12
Years of Service
User Offline
Joined: 4th Nov 2013
Location:
Posted: 20th Feb 2014 17:16
it just confusing because it is not working no matter what I try

Hasty
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 20th Feb 2014 21:08
Quote: "No. GOTO should never be used."


LoL! Fine, I'll bite...

How should one get from one loop to another then, if there are multiple loops in a program? Just don't use multiple loops?

Quote: "it just confusing because it is not working no matter what I try"


You'll hate me for saying this, but sometimes it's better to just start over from scratch. It's not that big a program so far anyway.

wattywatts
17
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 20th Feb 2014 22:39
Quote: "How should one get from one loop to another then, if there are multiple loops in a program? Just don't use multiple loops?"

Just my 2 cents, but I definitely think multiple loops are advantageous if you're working with lots of variables that need to be checked every cycle. And I personally use GOSUB with no Return. I don't think GOTO is dependable.

New sig coming soon..
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Feb 2014 08:46
@Derek Darkly. In a properly structured Program you wouldn't 'go' to another loop. You would make a call to the next loop which would then return to the point at which you called it. This is what is called 'embedded' programming structure and is the accepted way to do things.

@watty watts. You should never use Gosub without return. This is the same as GOTO but with potentially devastating results. A Gosub is placed on the stack, and popped back off when you return. If you don't return your Program will eventually suffer from a stack overflow and crash.

There is nothing inherently wrong with GOTO. It just make very hard to read code and this becomes a problem as the Program grows. Personally I never use it, there are a dozen different ways to avoid it.

Jon wu
12
Years of Service
User Offline
Joined: 4th Nov 2013
Location:
Posted: 21st Feb 2014 17:04
whats a return

Hasty
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 21st Feb 2014 17:36 Edited at: 22nd Feb 2014 06:23
Quote: "@Derek Darkly. In a properly structured Program you wouldn't 'go' to another loop. You would make a call to the next loop which would then return to the point at which you called it. This is what is called 'embedded' programming structure and is the accepted way to do things."


What does this entail, command-wise? Could I see a small example of this?

What if you don't want it to return, but rather move to a loop permanently until another loop is required?

Quote: "Programming style, like writing style, is somewhat of an art and cannot be codified by inflexible rules, although discussions about style often seem to center exclusively around such rules. In the case of the goto statement, it has long been observed that unfettered use of goto's quickly leads to unmaintainable spaghetti code. However, a simple, unthinking ban on the goto statement does not necessarily lead immediately to beautiful programming: an unstructured programmer is just as capable of constructing a Byzantine tangle without using any goto's (perhaps substituting oddly-nested loops and Boolean control variables, instead). Many programmers adopt a moderate stance: goto's are usually to be avoided, but are acceptable in a few well-constrained situations, if necessary: as multi-level break statements, to coalesce common actions inside a switch statement, or to centralize cleanup tasks in a function with several error returns. (...) Blindly avoiding certain constructs or following rules without understanding them can lead to just as many problems as the rules were supposed to avert. Furthermore, many opinions on programming style are just that: opinions. They may be strongly argued and strongly felt, they may be backed up by solid-seeming evidence and arguments, but the opposing opinions may be just as strongly felt, supported, and argued. It's usually futile to get dragged into "style wars", because on certain issues, opponents can never seem to agree, or agree to disagree, or stop arguing.
"

http://en.wikipedia.org/wiki/Goto#Criticism_and_decline

Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 21st Feb 2014 17:39 Edited at: 21st Feb 2014 17:39
Quote: "whats a return"


Look at the sample code i gave you - don't just run it, study it.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Feb 2014 20:41
Quote: "How should one get from one loop to another then"


By putting your code into a more modular structure like this:



Technically, you'd want to use constants in place of those mode numbers for the sake of clarity, but you get the point.

Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 21st Feb 2014 21:49 Edited at: 21st Feb 2014 22:11
Right, but there's only one DO/LOOP in that code...
I'm talking about a code with more than one:



What's better than GOTO in this example?

I'm not trying to cling to GOTO, I rarely ever use it, just want to understand why it's considered so bad. Common sense use should produce desirable results, and the end result is all that should matter, i.e., it either works or doesn't work.

Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 21st Feb 2014 22:56
@Derek, your code is less efficient than Phaelax's. You've turned one simple, easy to follow loop, into 3 loops.

If you feel goto is an answer to your coding problem, then your code is wrong.

I never use goto, or gosub. I'd use something similar to Phaelax's code, but in my games, all menu inputs are handled by the UI engine so my main loop looks something like this.



This way, all my subsections can be in their own file, so I can update my engine by simply updating one file. (I can also reduce the size of my engine by simply not including a particular file into the project.

zeroSlave
17
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 22nd Feb 2014 02:48 Edited at: 22nd Feb 2014 03:23
I'll bite: I know that goto has it's specific purpose, but I would not recommend it at all. Maybe used as a temporary tool for experimentation, or a quick and dirty hack in a tiny app, but not for real applicable purposes. If it is being used, I believe it's because the solution to a problem hasn't been fully explored. I think the use of goto leads into bad programming habits. If used once, why can't it be used again? And before you know it, your code would be terribly hard to follow. Spaghetti code. It will be hard for others to interpret as well as being hard to work on it after leaving it for a few days and coming back to it.

I do something akin to this:

I think a case select setup would be a preferred option due to potentially having multiple game state functions running during the same loop. I would prefer an if else if structure, but...

Also, I believe this type of coding method is considered a finite state machine? Not sure.

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 22nd Feb 2014 06:35 Edited at: 22nd Feb 2014 07:07
To All,
My original question still remains unanswered:

In a code with multiple DO/LOOPS (i literally can't seem to stress this enough) how does one properly navigate between loops? Again, I am not dealing with functions or subroutines here, but loops. Neither am I suggesting to replace such routines with GOTO or Loops.

No one else has given an example with multiple loops. And here's the important part: Going from random loop to random loop without returns. Just like in my code. Who can say it's tangled and hard to read?



Is it not ^^^ reasonably possible to orchestrate entire program segments within separate loops? What is the specific hindrance? GOTO is the logical navigation tool in my example. The word spaghetti is not explaining it, just another way of saying tangle. Does my sample code look tangled? No matter how much I fill those loops with, how can they be tangled if well separated?


Quote: "If you feel goto is an answer to your coding problem, then your code is wrong."


That's like saying I picked the wrong notes on my own song.

I am aware that goto has its limitations, but that doesn't mean it doesn't work.
It's like calling a blind man dead!

zeroSlave
17
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 22nd Feb 2014 07:09 Edited at: 22nd Feb 2014 07:27
Quote: "The word spaghetti is not explaining it, just defining a tangle!"


That's exactly what your code will become if you are jumping around to a bunch of different loops! A huge tangled mess! Spaghetti code The first part of the first paragraph reads: "Spaghetti code is a pejorative term for source code that has a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, or other "unstructured" branching constructs. It is named such because program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled."


Quote: "@Derek, your code is less efficient than Phaelax's. You've turned one simple, easy to follow loop, into 3 loops."

What happens when instead of 3 loops, you are using 100 loops? or 500 loops? How many lines of code will that be. If loop 1 jumps to loop 5 on line 300 and then back to loop 3 on 250 then up to loop 8 on line 800 then back to loop 2 on line 20, it will be hard to follow. I suggest adopting a certain coding habit that does not use gotos. I believe that using many do:loops will require you to use gotos. And as I said above, if you're having to use gotos, it's because the solution to a problem hasn't been fully explored.








Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Feb 2014 08:00
I think I understand your confusion, and it is down to how you see the lifecycle of a program. I think you are looking at it as a progression from start to finish, a long road starting at one end, and finishing at the other, with a few minor detours (loops). So you do one thing (in a loop), then you do the next thing (in a loop) and so forth to the conclusion.

A program should work from "the outside in" and back again. It's like a maze. You start at the entrance and walk into the maze. Whenever you come to a dead-end (end of a loop's functionality), you retrace your footsteps back to the previous junction, and go in a different direction (another loop). Eventually you reach the centre. Even so, you still retrace all of your footsteps back (through all your loops) to the entrance to get back out.

You have lots of good examples up above. I can add one more which might help, it's a tutorial on writing Space Invaders for DBPro. But it is written from the perspective of creating a well-structured program - with lots of loops!

Part 1
Part 2
Part 3

Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 22nd Feb 2014 21:17 Edited at: 22nd Feb 2014 21:24
Quote: "I think I understand your confusion...

A program should work from "the outside in" and back again."


I wouldn't call it confusion. LoL
Matter of fact, I would call this a miscommunication.

I understand the accepted styles... my first sample uses gosub/return in the usual manner. Another way is #INCLUDE which I also use frequently, as well as using functions in both manner.

But let's say, for example, I want to put 10 games into 1 .exe file in some kind of super mini-game package. Separate loops would be the way to go in this case, because each game would have completely different media and engine mechanics.

That's all I'm saying - I'm not suggesting replacing the accepted style with a series of loops. I'm suggesting there are could be situations where completely isolated loops could be one practical solution to corral segments of a project in a neat fashion. In such a case, GOTO could be used to navigate the occasional need to switch loops.

Philosophically speaking, I feel there's no wrong way to make a program so long that it works exactly to the developer's vision. That said, I suppose I am thinking mainly along the lines of individual developers rather than large teams and companies.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Feb 2014 00:43 Edited at: 23rd Feb 2014 00:47
Quote: "But let's say, for example, I want to put 10 games into 1 .exe file in some kind of super mini-game package."


You would still use the same code structure in my example. Instead of relying on another loop structure inside each subroutine, use the main one. Controlling the value of mode will control which code is going to loop over and over. Pressing escape (scancode 27) will change the mode and take you back to the main menu where you can select another game to play instead. You'd probably want to call a switchGames subroutine or something before jumping straight back to the main menu, that way you can auto-save the current game or do whatever else that must be done.

I've taken your code example with all your multiple DO-LOOP's and reorganized it. This will do the same thing without using GOTO and it's much more manageable because it's easier to know where the program is at. As zeroSlave pointed out, jumping around like that without a return makes for some spaghetti code and debugging a nightmare.



zeroSlave
17
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 23rd Feb 2014 01:24 Edited at: 23rd Feb 2014 01:49
Phaelax beat me, but I'm not gonna waste the code I just wrote



I would probably have each game in it's own #included file, and an #include for specific things like user highscores, settings, saving, loading, etc.

And realistically, I would probably have a launcher that just launched the individual game executables themselves. Each game could have a very different approach to screen sizes, graphical needs, etc. And it'd be best if they just had their own respective process.

Quote: "Another way is #INCLUDE which I also use frequently"

#include is a very good thing to use. It helps code management even more. Functions are also nice because you can collapse the function in the IDE and even view the functions in the Project tree of the Code View.

Quote: "Philosophically speaking, I feel there's no wrong way to make a program so long that it works exactly to the developer's vision. That said, I suppose I am thinking mainly along the lines of individual developers rather than large teams and companies."


Quote: "That's like saying I picked the wrong notes on my own song."

Not necessarily. I'm sure there are notes that you would not put together in a song because the chord will sound horrible. You wouldn't want to change the key signature every measure, nor use halfstep or tritone chords all over the place. I understand musical interests are subjective and there are some pretty odd examples that will not adhere to this, but I think there is an objective line at some point. (And sorry, I don't mean "you" as in you, I mean it in the second-person form of speaking.)

If you play guitar, do you have it tuned to a variant of eadgbe? or f#fcadc? You could probably play the same songs with the latter tuning, but would it be very easy or necessary? Would you do it just because you could? It's true you can tune the guitar to all sorts of different tunings, but the majority of them follow a certain guideline for the sake of sanity. Open chords, 4ths, 5ths, etc.


Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 23rd Feb 2014 12:17
Quote: "But let's say, for example, I want to put 10 games into 1 .exe file in some kind of super mini-game package. Separate loops would be the way to go in this case, because each game would have completely different media and engine mechanics."


As Phaelax already said, it would probably be best to have just one loop handling all the different game update routines.

Just imagine you want to add a certain behaviour to the complete game (including all those 10 minigames), such as, for instance, some kind of debug output, or a piece of code that allows the user to take a screenshot when pressing a certain key, or something like that. When having 10 separate loops, you'd have to add this change to every single one of them. If, on the other hand, you have all your minigames handled within one main loop, you can just add your new code there once and are done.
Regarding the different media, you'd probably just have some kind of cleanup() and a loadminigame(ID) function and call them at appropriate times when loading a new game.

I believe I haven't used a single goto (or even gosub tbh, I always rely on functions) in any of my bigger projects throughout the last years - not because I have this strict rule of avoiding gotos by all means, but because it just never occured to me as a possible solution to any of my problems.
That being said, however, I did use it quite a bit in some of my 48 hour projects. Because then it's really just about getting quick results, instead of making sure your code is well structured.

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 23rd Feb 2014 15:02 Edited at: 23rd Feb 2014 15:04
The same "outside -> in" principle applies, as a very simple example:




In respect of GOTO's you could do exactly the same but you would GOTO your game 1, then GOTO your main menu, then GOTO game 2, then GOTO the main menu. The result is the same. As an experienced programmer you would never do this with GOTOs because it is just "uncomfortable". It's harder to follow visually, it's a lot harder to follow in a debugger (which we don't have yet in AGK), and it's extremely hard for another developer to pick up your code and follow it. BUT if coding is your hobby then it may not be so important.

It's a bit like golf. I, as a beginner - would take every shot to get as close to the hole as possible. A professional may drop the ball somewhere more strategic because they know that the next shots will be easier as a result.

Stab in the Dark software
Valued Member
23
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 23rd Feb 2014 18:15 Edited at: 23rd Feb 2014 18:25
Derek Darkly

Another way to handle this is with Game States.
One method for implementation of a Game State structure in Dark Basic Pro
is to construct a User Defined Data type for a Game State.
The data type for the Game State would store function pointers to functions for handling;
initialization of the Game State, cleanup of the Game State, and rendering.

An example project is attached.

[img][/img]


WindowsXP SP3,Vista,Windows 7 SP1, DBpro v7.7RC7
Stab In The Dark Editor
The coffee is lovely dark and deep,and I have code to write before I sleep.
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 23rd Feb 2014 21:07 Edited at: 23rd Feb 2014 21:27
As you can see kids, GOTO is not necessary, but like that creepy uncle, he's always around. I do see the point though; there's always another way. However, one man's organization will always be someone else's slop.

In fact, I'd say one's organizational skills could be well challenged in the use of GOTO, but no one wants to travel down that dark, rat-filled alleyway anymore.

I still feel that the end result is what matters most, especially for indie developers.
To paraphrase Crowley , "Do what thou wilt..."

666GO†O666
tiffer
20
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Scotland
Posted: 27th Feb 2014 12:21


Cwatson

Login to post a reply

Server time is: 2026-07-06 07:53:00
Your offset time is: 2026-07-06 07:53:00