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.

Newcomers DBPro Corner / Goto "Variable"

Author
Message
Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 7th Jul 2010 02:19
Ive been trying to find a way to make a goto command that will go to the name of the variable of its destination. But i just found that the goto doesnt understand the variable as a variable but as the name of its destination. So i wondered if theres any way to make my goto command to understand that the variable is naming its destination, and not that the name of the variable is its destination.

Like this:




So, does anyone have had this problem before? What can i do?
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 7th Jul 2010 03:21
What exactly is it your trying to do? I'll try and give you some better advice on how to achieve it.

I will warn you now however that GOTO is generally considered around here to be evil, and that you should probably never use it. (I certainly haven't found a need for it); but theres plenty of forum posts about this subject, no need for me to go on about it.

Having said that, have you considered something like



It's not very elegant, but it might help you a little.

Better help will come after you share a few more details on what you're trying to do.

Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 7th Jul 2010 05:23
I know "Goto" its like a kind of taboo in this forum XD But i have found several uses for it, most in functions. And yes, i have already though of that code u just wrote. Ill explain what i achieve to do.

The goto command im talking about in this post, its to go to another area of my game, like, 2nd lvl. So, for example, if im in 2nd lvl, ill be able to choose to go from 2nd to 1st, or from 2nd to 3rd. This is something that will happen all over the game, and thats why i cant use the code u just wrote, it would be just, gigantic. I need a little code, that will work in all my game, by just changing a variable. Summary: The goto its in a function.

Idk if i explained myself... ill show u that part of the code so u guys see what i mean:



Ok, lemme explain. (lol, i constantly write my codes in spanish, so ill translate)

"Puerta" means door
"PuertaN" means the number of the door
"Puerta primera" First door, to "Puerta Ultima" Last door
Pixelcol is a collision fucntion made by CLOGGY
WDSprites are the sprites that are colliding with the door
"Puerta Visible" means that the door is not invisible XD

"Jungla1" which means jungle, is the destination of goto... So here is the problem. I want instead of putting "Jungla1" to put a variable, so i can constantly change the destination of the goto. But the goto command just doesnt understands variables, what can i do?
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 7th Jul 2010 11:31 Edited at: 7th Jul 2010 11:36


With this function at the end of your code, you can simply call "changeLevel()" to do what you need.

Although, I think having a seperate bit of code for each level is a silly idea and will make your game ENORMOUS;

You'd be better off writing a universal game loop, which can load in the level data from a file; or from an array; so you can switch levels just by changing a variable or 2.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 7th Jul 2010 16:26
To me, it does not make sense to use GOTO like this. Are you going to have separate routines at Jungla1 to control the player, move the enemies, bullets, etc.? Unless it is a special level, I would think not. Even if you had separate routines for all of the main loop functions/subroutines, you would have to write another main loop to process everything properly at Jungla1. Sure, it could be done, but I think it would make your code messier, longer and harder to debug.

Typically, you will have a main game loop and will call a function/subroutine to load in a level as needed and then continue processing the main game loop.

If you jump around to different levels like this, you are asking for headaches IMO.

LB


Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Jul 2010 18:17
It seems what he's looking for is SELECT/CASE statements. SELECT/CASE statements allow you to have multiple choices in one area so it's a lot easier to work with and debug rather than having a whole bunch of IF/THEN statements.



It's ok to learn and use GOTO... just stop using it when you learn about GOSUB and/or functions.

Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 7th Jul 2010 18:47
Quote: "
SELECT/CASE statements allow you to have multiple choices in one area so it's a lot easier to work with and debug rather than having a whole bunch of IF/THEN statements.
"



is it ?






Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Jul 2010 18:59


( Grog pretends to be an insulted little kid )

Oh yeah? What about CASE DEFAULT that's more betterer than IF/THEN/ELSE/ELSE/ELSE?



Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 7th Jul 2010 20:12
Why would you need ELSE ?



Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 7th Jul 2010 21:21
lol... The main loop of my program, is the function, so this is basically how my program works.

I make 1 or 2 functions only, that is all my main loop, of all my program, all i got to do for the rest of the program, is to change the variables outside the function. The first time i started to write it, i wanted it to be easy to work with, so thats kinda what i made. Its easier than u guys think rly, its just one main function acting as the one that moves the character, the scenario, the enemies, and everything else, and the variables to change the content of the function, look, this is my code:



That is the code that contain all the main functions, AND the first lvl.And thats why its soooooooo big.


And, this is the code that contains the second lvl, which only uses the main function, as simply as that. Look at it:




I hope i explained myself well, its not complicated, in fact, i found the goto commands rly usefull for all my game, and the "spaghetti" problem does not happen if ur careful with it.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 8th Jul 2010 02:02
Quote: "Why would you need ELSE ?"


Ok, you got me on that one Kevin.


Yitzu, you really should look into saving all of the levels into files so you only need one routine to load any level you want.

Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 8th Jul 2010 05:17
Could u explain how the file thing works, seems pretty useful =P
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 8th Jul 2010 10:51
Personally, I really like using Select/Case when programming. They're simple and clear to use, remembering also that you can have a "default" case too. Another trick that you can do with Cases, is have multiple prerequisites per case. All you have to do is separate them with a comer ",".

Here's a simple example I made when first playing with Select & Case.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 8th Jul 2010 18:27
Quote: "Could u explain how the file thing works, seems pretty useful =P"


Instead of having all those separate variables you load them into an array via a file. The attached file is just a standard text file that can be edited with Notepad. Each line represents the numbers you want to store in the array.

Ideally you change your code to use the array rather than each individual variable. Some variables should be separate like the ones involving the screen width.



Once you get the data in the array you can use SAVE ARRAY and LOAD ARRAY to quickly save/load it all with one command. Look up those commands and experiment a bit.

Attachments

Login to view attachments
Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 9th Jul 2010 05:14
mmm... may i ask something, saving ur data in a file is just for making the code look pretty or does it makes any plus in ur program?
Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 9th Jul 2010 05:31
Is SELECT/CASE just useful with inputs commands? if it isnt, how can i use it, it seems rly cool.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 9th Jul 2010 07:50
Haha, we can't read your mind of what it is you're trying to do. First of all, test if you can actually get your own simple Select/Case program working. I do apologise for my above example, for it included functions which may have confused you.

Maybe, you could have a variable called "Level". Level can equal say, 1 to 10. Then you could have 10 cases in a Select, which identify which function to run depending on what "Level" equals.



So, if Level = 1, Case 1 will be carried out. If Level = 2, Case 2 will be carried out, and so on. A default case can be employed, just in case, for example, Level = 11, and you didn't have a case for 11. The default case would then run.
Yitzu
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location:
Posted: 9th Jul 2010 08:52
Wow. I think ill use Select/Case, alot. Seems pretty nice, hey, thx for the info, i rly appreciate it. Every day a i learn more in this forum =P

TY
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 9th Jul 2010 21:47
Quote: "Every day a i learn more in this forum"


I'm sure you do, but you still insist on using GOTO in your coding and have not learned that it leads to problems/headaches/unstructured code. If you click on Grog Grueslayer's sig, it will take you to the WikiPedia definition of spaghetti code.It is not pretty. You may remember a similar GOTO discussion we had back in February.

In that thread, your code was so disorganized that you couldn't find the bug in it (imagine that). I took the time then to re-write your code for you in a structured manner and you said
Quote: "Ty very much, I rly can learn a lot from this code, ok, ill use all the info u guys gave me, thx everybody =)"


"A man convinced against his will is of the same opinion still."

Good luck with your coding, you will need it.


Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 10th Jul 2010 19:07 Edited at: 10th Jul 2010 19:08
Quote: "mmm... may i ask something, saving ur data in a file is just for making the code look pretty or does it makes any plus in ur program?"


Yes, it does make your code "pretty" and gives you the benefit to edit the levels without hunting down the level data in your code. You don't even have to compile the .exe again because you can simply open the .txt file and edit the data there. Of course using an easily editable .txt file is mainly used while programming and is switched to something not so easily editable before you share your program with the world. Or if you want to give the users the ability to edit the levels you leave it a .txt file.

The good thing about loading all that data into an array is all your level information is in one place rather than 50 to 100 different named variables. If you want to add a new variable all you do is increase the size of the array and use the newest element rather than making up yet another variable name (that if your not careful you may repeat).

LBFN is 100% right... you really should stop using GOTO. When I first told you about SELECT/CASE it was to steer you away from GOTO. If you have to GOTO a label at least make it GOSUB instead but it is possible to write code without using a label at all using only functions.

You've learned GOTO... now learn about GOSUB and functions.

Your way with GOTO:


With GOSUB:


With a function:


Login to post a reply

Server time is: 2024-11-24 14:36:55
Your offset time is: 2024-11-24 14:36:55