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 / gosub command?

Author
Message
Forest
18
Years of Service
User Offline
Joined: 29th May 2006
Location:
Posted: 22nd Jun 2006 23:52
Can anyone please help me with this?
I am experimenting using the gosub command to direct the computer to different parts of the code or more specifically to different loops.
eg This is what I've tried:

If upkey()=1 then gosub _a
If downkey()=1 then gosub _b

_a:
Do
Stuff
loop

_b:
Do
Stuff
loop


But what happens is the computer goes straight to the loop anyway even if the key has not been pressed. How do you correct this?
Crit
18
Years of Service
User Offline
Joined: 24th May 2006
Location:
Posted: 23rd Jun 2006 00:22 Edited at: 23rd Jun 2006 00:23
I personally don't use gosubs that much, but this should work for you:



flock
18
Years of Service
User Offline
Joined: 10th Mar 2006
Location: Earth
Posted: 23rd Jun 2006 00:22
You need the upkey() and downkey() commands inside a loop. What happens is the computer states IF the user presses up or down then go to the loop, if they don't then the computer will run into the first gosub and remain in the loop. As an experiment you can try holding down upkey as you start your program.
flock
18
Years of Service
User Offline
Joined: 10th Mar 2006
Location: Earth
Posted: 23rd Jun 2006 00:23
Note you'll have to break out of the loops to utilize the return command...
D Ogre
21
Years of Service
User Offline
Joined: 19th Nov 2003
Location:
Posted: 23rd Jun 2006 00:49
If you need to exit a DO/LOOP, you can setup a condition for it to happen
using an IF/THEN statement and the EXIT command. You can also use other loop
types that already have a conditional check for and exit point.
(ie. WHILE/ENDWHILE and REPEAT/UNTIL)
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 23rd Jun 2006 02:10
and put END above a:

Science, Mathematics, and Physics do not lie - only people do.
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 23rd Jun 2006 14:32
To distill everyone's guidance, here is some revised psuedocode based on Crit's {thanks!}

Hope that helps!
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 23rd Jun 2006 14:50
masterpiece DAD

Science, Mathematics, and Physics do not lie - only people do.
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 23rd Jun 2006 15:10
Thanks! {Takes a bow}

I'd never really noticed all the uses of my screen name until this forum, but they all work.

Dream and Death: My two fav Neil Gaiman characters from Sandman.
D&D: I've been playing for 18 years.
DAD: Yep, father of two.

Anyone for any more?
Forest
18
Years of Service
User Offline
Joined: 29th May 2006
Location:
Posted: 23rd Jun 2006 17:24
Thanks everyone, great help!!
Forest
18
Years of Service
User Offline
Joined: 29th May 2006
Location:
Posted: 23rd Jun 2006 17:46 Edited at: 23rd Jun 2006 18:01
Just couple of questions:

why do you need the END command? Program seems to work ok without it?

Also, how do I get two or more processes to happen simultaneously eg

Say, gosub _a makes object one move left
and, gosub _b makes object two move right

How do you get _a to happen while _b is happening or visa versa?
At the moment, if I press upkey _a happens but pressing downkey will not make _b happen until _a has ended.
Daemon
18
Years of Service
User Offline
Joined: 16th Dec 2005
Location: Everywhere
Posted: 23rd Jun 2006 18:12
You can't make things happen at the same time. They will always happen one after the other. With they sync commmand you can wait to update the screen until both have happened so it seems to have the same effect.

Forest
18
Years of Service
User Offline
Joined: 29th May 2006
Location:
Posted: 23rd Jun 2006 18:25
Ok, well I dont think gosub is going to work for me in this program.
How would I go about doing this then?

I am going to have many objects on screen

The user will be prompted to press a certain key for each object.
Depending what they press makes the object do certain movements.
The movements are different for each object and different depending on the key pressed.

But I want all this to happen simultaneously, so object 1 may just start moving as object 2 is just stopping etc.
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 23rd Jun 2006 18:55
Sync makes things happen at the same time as posted above. You can carry on using Gosub.

Forest
18
Years of Service
User Offline
Joined: 29th May 2006
Location:
Posted: 23rd Jun 2006 19:10
Sorry, but I must not be understanding this correctly, Here is an example code:

Do
If upkey()=1 then gosub _a
If downkey()=1 then gosub _b
sync
loop
end

_a:
For x=0 to 1/0.01
move object 1,0.01
turn object left 1,0.1
sync
next x
return

_b:
For x=0 to -1/-0.01
move object 2,-0.01
sync
next x
return

So where do I leave out sync???
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 23rd Jun 2006 20:06 Edited at: 23rd Jun 2006 20:07
Ah, I see your problem - don't use for..loops in gosubs or functions, as they will stop the rest of the programme running 'til they end.

Try this: (I'm gonna code in quick psuedocode, but it will give you good practice to work it out in real code)


This way, the subroutines (I would personally code them as functions, but I'll work with what you seem familiar with) start running if they are not already running. They will get updated once per main loop cycle, until they are finished, in which case their flag gets reset and they can be started again.

Hope this helps! [Edit] Just realised that it might not be psuedocode, it might actually work!
Forest
18
Years of Service
User Offline
Joined: 29th May 2006
Location:
Posted: 23rd Jun 2006 20:41
Great, Thanks!
I am keen to use functions, but not quite figured them out yet, how would you change that code to use functions instead?
(If you have time, thanks!)
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 23rd Jun 2006 22:19
I would write it similar to this:


Then I have only one multi-use function, rather than two (or more) gosubs. The function can be passed any object number, with a move rate and a limit.

If the square of the objectpos is greater than the square of the limit, the function stops, returning a value of 0. I check for the square so I don't have to check for xpos>limit if move +ve or xpos<limit if moving -ve.

If the xpos squared is greater than the limit squared the function returns 0, this gets assigned to either flag (A_func_running or B_func_running), resetting them so they can run again later.

If not, the xpos gets updated, and the object moved, and then the function returns 1, which keeps the flag going.

Hope this shows yu the power of the function! It is your friend, and will become the centre of your coding!
Forest
18
Years of Service
User Offline
Joined: 29th May 2006
Location:
Posted: 23rd Jun 2006 22:48
Huge thanks!!!
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 23rd Jun 2006 23:37
any other loop you branch off to from a main loop, whether its in a sub or func, will cease the main loop - there are ways around it however

Science, Mathematics, and Physics do not lie - only people do.
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 24th Jun 2006 00:43
CattleRustler: I was just trying to proving that very point with an ongoing function.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 24th Jun 2006 04:15
yep

Science, Mathematics, and Physics do not lie - only people do.
harggood
21
Years of Service
User Offline
Joined: 9th Apr 2003
Location: the forest
Posted: 24th Jun 2006 20:34
Quote: "any other loop you branch off to from a main loop, whether its in a sub or func, will cease the main loop - there are ways around it however"


I just want to add - that you can, if you like, put everything inside subs or funcs - no main loop.

Awfuldark Forest
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 24th Jun 2006 20:59
harggood: If you can do it that way, good luck to you. I have never heard of a program/game organised that way in any language, and especially not DBP with the requirement to SYNC every cycle.
harggood
21
Years of Service
User Offline
Joined: 9th Apr 2003
Location: the forest
Posted: 25th Jun 2006 00:46
Quote: "harggood: If you can do it that way, good luck to you. I have never heard of a program/game organised that way in any language, and especially not DBP with the requirement to SYNC every cycle."


You can put the do loop in a subroutine and sync it. In that case, you don't use the return.

Awfuldark Forest
Dream And Death
18
Years of Service
User Offline
Joined: 21st Feb 2006
Location: The circus! Juggling job, kids and DBPro
Posted: 25th Jun 2006 10:52
Which then counts as your main loop(?!)
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th Jun 2006 16:20
Quote: "Which then counts as your main loop(?!)"


It does really yes. You could have two main loops or more though, so it's not quite a main loop.

harggood
21
Years of Service
User Offline
Joined: 9th Apr 2003
Location: the forest
Posted: 25th Jun 2006 19:56
Quote: "It does really yes. You could have two main loops or more though, so it's not quite a main loop."


For example, in the driving game I'm making, the driving part is in a loop in a subroutine. If you stop at the garage, the program leaves that sub to the garage sub, which has a loop. When you're done in the garage, back to the driving sub.

Awfuldark Forest
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th Jun 2006 21:07
It is a strange way to program though. It sounds like you have wasted gosubs that are doing nothing. If you never hit a return, then the Gosub is pointless. It is also at the risk of filling up the return allocation cache.

harggood
21
Years of Service
User Offline
Joined: 9th Apr 2003
Location: the forest
Posted: 25th Jun 2006 21:39
Quote: "It is also at the risk of filling up the return allocation cache"

I never had that problem. There's no waste of gosubs. I guess that I didn't explain it well. The only thing I was trying to say is that you can put your do loop in a gosub, if you like.
Quote: "If you never hit a return, then the Gosub is pointless."

The point is to make use of this:
Quote: "any other loop you branch off to from a main loop, whether its in a sub or func, will cease the main loop"


Awfuldark Forest
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th Jun 2006 22:07
I just don't see many practical uses for this. I can imagine using it for switching system specs, where you have a low end, and high end loop. Then you are avoiding some tests which might slow down the program slightly. So I suppose the practical use is to avoid some IF's.

harggood
21
Years of Service
User Offline
Joined: 9th Apr 2003
Location: the forest
Posted: 25th Jun 2006 23:10
Quote: "So I suppose the practical use is to avoid some IF's."


Yes. I think thats possible. Sometimes it's handy having loops in subroutines, so that you can return from the game to the options menu, for example, and then go back to the game. Keep in mind that you can put a return command anywhere you want in a subroutine.

Awfuldark Forest
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th Jun 2006 23:50
.
Quote: ". Sometimes it's handy having loops in subroutines, so that you can return from the game to the options menu, for example, and then go back to the game. Keep in mind that you can put a return command anywhere you want in a subroutine."


You can do all that anyway. Everybody does that. All games do that, nobody puts loops in Gosubs to do that.

harggood
21
Years of Service
User Offline
Joined: 9th Apr 2003
Location: the forest
Posted: 26th Jun 2006 00:04
Quote: "You can do all that anyway. Everybody does that. All games do that, nobody puts loops in Gosubs to do that.You can do all that anyway."


Okay. I understand. Just sharing ideas. If nobody is doing it, it's still possible. Why is it a bad idea? Have you tried it?

Awfuldark Forest
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 26th Jun 2006 17:32
Well Repeat Until is what I use. It's not a Do Loop, but it's a loop. Do Loop should be the main loop, it is easier to read the program by looking inside the Do Loop and seeing where it branches off. That is like the tree Trunk. To have two Tree trunks on a tree is overkill. However there might be some reasons for it. It's just that your reasons are already easy to do without another Do Loop. In fact, the do loop would probably require an extra command.

Login to post a reply

Server time is: 2024-11-26 17:37:44
Your offset time is: 2024-11-26 17:37:44