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 / Gosubs within gosubs?

Author
Message
diseased
22
Years of Service
User Offline
Joined: 16th Aug 2003
Location:
Posted: 30th Aug 2003 07:26
is it possible to have gosubs within gosubs? i get an error when i try to do that..

im working on a RTS or TBS(not sure yet) and i think im taking a very long way around controling unit stats and positions..
theres a relatively low # of units available at once(30 max) though, so i think this WILL work, but its going to take up alot of space..
in my main loop, i detect which unit is selected, and a gosub goes to that units stats, the problem is that if i cant puta gosub inside that gosub, then i have to repeat all of the possible commands inside the particular units gosub over and over..

heres an example:
loop
gosub positions
gosub selection
if unit = 1 then gosub unit1
if unit = 2 then gosub unit2
(repeated 30x)
sync : loop

gosub unit1:
controls positions firing etc
return
gosub unit2:
controls positions firing etc
return
(repeated 30x again)
MushroomHead
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 30th Aug 2003 12:14
Something like this? It works for me ... It's probebly good to have an END statement after wait key (for clarity's sake).
Mentor
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 30th Aug 2003 14:43
if you get an error then there must be some code error unless you are calling the gosub recursivly which will crash the runtime when the cpu runs out of stack, but you can nest em to any depth, not a problem, just don`t end up with code that effectivley does this.

gosub trouble
trouble:
gosub crashme
crashme:
gosub trouble

if you follow that a few loops roud you will see the problem, eventualy the pc will run out of memory trying to remember all the return points for all the gosubs, you and I know there are only two return points, but computers are dumb like that sometimes , it will insist on trying to keep a seperate record for every return point, these points are stored on the stack and the stack is normaly (IIRC) 64mb, so once it has looped 16000 times or so, it crashes

Mentor.
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 30th Aug 2003 15:09
To be honest though, it looks like you've got your structure all wrong.

Instead of:
if unit = 1 then gosub unit1
if unit = 2 then gosub unit2
(repeated 30x)

You should have something like:
for unit = 1 to 30
ProcessUnit(Unit)
next unit

Then write yourself a function that controls the unit based on whatever variable you pass it.

Function ProcessUnit(Unit)
Controls positions firing for "unit"
endfunction

Sorry if it seems like I'm being a picky bastard and not answering your question about gosubs, but a good structure should avoid all those problems.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
diseased
22
Years of Service
User Offline
Joined: 16th Aug 2003
Location:
Posted: 31st Aug 2003 05:12
fallout, the problem with that is that each unit has seperate attributes to keep track of, so i cant make them all the same..
and i dont know how to write my own functions..

also, so as not to post another topic, what is with sprites?
i loaded a single sprite as my crosshair, and it slows the whole program down to like a 1/3 of its non-sprited equivalent.. even if its not in the loop or active in the game.. and its only 64x64 with a transparent background..
can u set 3d models to be drawn to the front like sprites?
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 31st Aug 2003 05:23
Use the SET SPRITE command to switch the backsave option off and it'll speed back up again.
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 31st Aug 2003 16:07
Diseased. I'm sure there is a way, even if they've got different attributes. If you don't mind, add the source file to your next post and so long as its not gonna take me till the next millenium to figure something out, I'll see if I can make some suggestions. Otherwise, sorry I couldn't help and you'll just have to come up with another structure.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
diseased
22
Years of Service
User Offline
Joined: 16th Aug 2003
Location:
Posted: 31st Aug 2003 18:29 Edited at: 31st Aug 2003 18:43
alright, its a long one though.. if you want to run it all u have to replace is the image files.(i think just two) also, the thing is SUPPOSE to ask how many units of each type you want, but it doesnt.. it just shows a black screen.. u just have to type in 5[enter]10[enter]15[enter] theres also alot of other strange controls..
1 = next unit 2 = last unit (theres a error if u go to unit 0 tho)
upkey = move forward [enter] and then [y] = next turn -its going 2 be TBS which is why u stop moving after a short period..

IanM: thanks, that sped it up alot

are u working on a project, fallout?

DONT STEAL MY SOURCE AND STEAL MY GAME IDEA! THAT TOOK ME 3 DAYS TO MAKE!!<my immitation of a paranoid noob with code that isnt so great 2 begin with ^.^
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 31st Aug 2003 19:03
Looking into your code now. I probably wont try and compile it, I'll probably just see what I can suggest from looking at the stucture.

And yep, I'm working on a UFO Enemy Unknown type game at the moment, with a friend. Turn based, 3D, tiling system, destructable walls and floors, dynamic lighting, the works. And yes, it's bloody hard, and no we'll probably never finish it.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 31st Aug 2003 20:22
Ok, I had a good look through. I didn't check through everything, so I appologise in advance if I suggest something that conflicts with something I didn't look at.

Ok first up, look at your gosubs. Here's an example:
_tank05:
speed = 2
if tm05>0 then moveable =1
if upkey()=1 then dec tm05,1
if tm05<1 then moveable=0
return

Now all though I didn't really look into exactly what each variable does, you can still see that, in essense, the sub routines are the same. You just need to think of a way to represent the otherwise unique variables and numbers with something more general. So here's a possible structure:

function ProcessUnit(unit)
if tm(unit)>0 then moveable(unit) =1
if upkey()=1 then dec tm(unit),1
if tm(unit)<1 then moveable(unit)=0
endfunction

It looks like you're basically checking each unit to see if its movable or not, and then setting another variable accordingly. Same with the speed. It looks like depending on what unit it is, you're setting a speed variable. If this is the case, then you could set each units speed value at the beginning and not have to adjust it in this procedure.

Doing it this way means you'll only need this one function to control every single unit.

To therefore call the function properly, you need to change this:
If key = 1 then gosub _mech01
If key = 2 then gosub _mech02
If key = 3 then gosub _mech03
If key = 4 then gosub _mech04
If key = 5 then gosub _mech05
If key = 6 then gosub _tank01
If key = 7 then gosub _tank02
If key = 8 then gosub _tank03
If key = 9 then gosub _tank04
If key = 10 then gosub _tank05
If key = 11 then gosub _tank06
If key = 12 then gosub _tank07
If key = 13 then gosub _tank08
If key = 14 then gosub _tank09
If key = 15 then gosub _tank10
if key = 16 then gosub _infantry01
if key = 17 then gosub _infantry02
if key = 18 then gosub _infantry03
if key = 19 then gosub _infantry04
if key = 20 then gosub _infantry05
if key = 21 then gosub _infantry06
if key = 22 then gosub _infantry07
if key = 23 then gosub _infantry08
if key = 24 then gosub _infantry09
if key = 25 then gosub _infantry10
if key = 26 then gosub _infantry11
if key = 27 then gosub _infantry12
if key = 28 then gosub _infantry13
if key = 29 then gosub _infantry14
if key = 30 then gosub _infantry15

To this:

if key >= 1 and key <=30
ProcessUnit(key)
endif

So now you have the function being called and the right unit number being passed to the process, to tell it which unit its dealing with.

But now we have this in place, we need the arrays to handle the function above. So instead of:
mm01=100:m01=1
mm02=100:m02=1
mm03=100:m03=1
mm04=100:m04=1
mm05=100:m05=1
tm01=100:t01=1
tm02=100:t02=1
tm03=100:t03=1
tm04=100:t04=1
tm05=100:t05=1
tm06=100:t06=1
tm07=100:t07=1
tm08=100:t08=1
tm09=100:t09=1
tm10=100:t10=1
im01=100:i01=1
im02=100:i02=1
im03=100:i03=1
im04=100:i04=1
im05=100:i05=1
im06=100:i06=1
im07=100:i07=1
im08=100:i08=1
im09=100:i09=1
im10=100:i10=1
im11=100:i11=1
im12=100:i12=1
im13=100:i13=1
im14=100:i14=1
im15=100:i15=1

We can use:
dim tm(30) as integer
dim speed(30) as integer
dim Moveable(30) as integer

I've used TM as I don't know what the actual variables are storing, but obviously you could call the variable whatever is generic.

Notice how throughout your program you're using the variable "key" to tell the program which object is being controlled, and then you're using position object "key" commands etc. Well, the same principle applies to the array and the functions. Use the variable key to say which position the array you want to access, and to tell the function which unit you want to control.

So, in all your following subroutines which refer to moveable and speed, instead you just need to refer to Speed(key) and Moveable(key)

Anyway, hope that helps you out a bit. I must confess, I didnt look through your code quite as closely as I could've done, but to be fair I'm half asleep and I've had a few beers. You should be able to get something from that though - there is a really efficient and structured approach to everything, and for the proggy you're doing there, functions and arrays could definitely save you a lot of work.

Insiiiiiiiiiiiiiiiiiiiiiiiide!
diseased
22
Years of Service
User Offline
Joined: 16th Aug 2003
Location:
Posted: 31st Aug 2003 21:07 Edited at: 1st Sep 2003 03:48
alright,thanks for the help!

but.. how do i write to Arrays?
or would using lists be more effective?

Login to post a reply

Server time is: 2026-07-25 03:42:27
Your offset time is: 2026-07-25 03:42:27