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 Discussion / Subs -> functions?

Author
Message
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 27th Nov 2003 01:20 Edited at: 27th Nov 2003 01:21
i read somewhere about functions that they could and should be used to replace sub. how do i go about doing that?

also, do you guys suggest that i go through my code and change all my subs(about 11) to functions

=<=Pudgie Wars=>=
Game engine: 20% = Models: 25% = Textures: 1% = Sounds/Music: 0%
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 27th Nov 2003 03:57
Yes, use functions wherever you can instead of any other type of code...

Atm, im making a BreakOut game for the 20liner challenge, and this is all that is in my main loop (due to function useage):


That is all, out of 380 lines that gets called in the loop.

That is what you should aim for. It is the best kind of codeing you can do.

Also, functions a re reusable, and can be used for all sorts of different projects. They are very universal.


To answer your first question, im going to convert your bulletlife code from the other thread into a function for you:




It's not too hard, but requires a bit of thought. This is the function version:


To call that, put in your main loop, something like:


What that does, is it passes all teh information needed by the function into it, and can be changed by you each time you call teh function, say you had 5 bullets, you could put in a different number for objnum each function call.

The part where it has
bulletlifemain = ...
When the function returns the variable stored in bulletlife from inside the function, it needs to be stored in a variable in the main loop (since variables inside functions are not global).
That just puts the value of bulletlife into the variable bulletlifemain which is then used for determining the bulletlife in the function the next loop (you can see, it is one of the parameters)

Hope I Helped...

PS sorry bout the long post...


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 27th Nov 2003 04:39
ok thats awsome. when i tried to change it, i didnt take into account that Functions arnt global. i knew it was but i didnt think
i just tried function_name() w/o passing

thanks once again JessT

=<=Pudgie Wars=>=
Game engine: 20% = Models: 25% = Textures: 1% = Sounds/Music: 0%
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 27th Nov 2003 05:15
That's cool.

Yeah, the main thing is you have to think about the variables you use and what you need to pass the function.

Also, functions can be good for hiding away stuff you'll never need to edit, like setting up your level or creatinga whole bunch of objects. Just put a function command infront of it, and an EndFunction command after it all then stick the function into another file and just #Include it... can help to keep your code alot neater.

Glad To Help...


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 27th Nov 2003 19:06 Edited at: 27th Nov 2003 19:57
yeah thats what i've been doing, #include, and that's why i wanted functions so i could put all my player keys in one, weapons in another, blah, blah, blah. so im going to go through all my code today and change it. thanks again

=<=Pudgie Wars=>=
Game engine: 20% = Models: 25% = Textures: 1% = Sounds/Music: 0% = www.geocities.com/bigapple_entertainment
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 27th Nov 2003 21:22
ok, do i have to pass the camera angle x(), etc to the function?

=<=Pudgie Wars=>=
Game engine: 20% = Models: 25% = Textures: 1% = Sounds/Music: 0% = www.geocities.com/bigapple_entertainment
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 28th Nov 2003 09:03
not necesaraly.

If you don't have the value of Camera Angle X() stored in a variable for each loop, then you don't need to, all you'd do is:


But, if you are using a variable ni your main loop to store the value of Camera Angle X(), then you need to pass it to the function.



Hope I Helped...


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 29th Nov 2003 22:19
okey. thanks

=<=Pudgie Wars=>=
Game engine: 20% = Models: 25% = Textures: 1% = Sounds/Music: 0% = www.geocities.com/bigapple_entertainment
CPU
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Carlsbad, CA
Posted: 30th Nov 2003 05:54
you don't have to pass certen kinds of data like if you have an object you don't have to send the function the xyz coordinets, just give it the object number and you can find that out in the function. Hope that little tidbit helped.
CPU

Sparring - Loved by many, Pefected by few.

I'm going to live forever, or DIE IN THE ATTEMPT!!!"
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 30th Nov 2003 06:47
Yeah, that's what i was tryign to get at in my last post.

Thanks for clearing that up CPU.


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy
BarZoule
22
Years of Service
User Offline
Joined: 26th Nov 2002
Location:
Posted: 1st Dec 2003 02:42
Also if there is a variable that is accessed and changed by several functions it could be a waste of cpu time and stack memory to pass it as a parameter.
You can instead create it as an array at the start of the program and that will make the variable global.
example of such variables could be
player_health(0)
monster_type(0)
game_mode(0)

my current game start with one page full of DIM and four commands:


-BarZoule

'Klaatu barada nikto'
Xander
22
Years of Service
User Offline
Joined: 3rd Mar 2003
Location: In college...yeah!
Posted: 2nd Dec 2003 06:27
Ooooh

Arrays are global? Maybe I will reconsider and convert some of my gosubs to functions. My program is over 6000 lines long with tons and tons of gosubs, and my game goes kind of slow on my friends computers who have no video card whatsoever (Just the built in stuff.)

It runs around 30-50 fps on my computer (1 ghz with 64MB Geforce4 and 256MB RAM). I want to speed it up, and have been looking for some big ways to do so. I will edit out the matrix for a couple carefully textured plains in the next few days, but an looking for more ways.

Do functions speed up programs a lot, or are they just for convenience?

Thanks

Xander - Bolt Software

Why do the antagonists always have to look so cool?
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 2nd Dec 2003 12:22
Convenience mainly... But if you use them wisely, you might be able to gain some FPS.

The good thing with Functions is you can make them universal, whereas you may need two or three different GoSub's to do the same thing three slightly different ways, you can just use the one Function to do it all, by just passing it some info.

Hope I Helped...


Team EOD :: Programmer/Logical Engineer/All-Round Nice Guy

Login to post a reply

Server time is: 2025-05-22 06:27:31
Your offset time is: 2025-05-22 06:27:31