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.

Author
Message
Plastico
20
Years of Service
User Offline
Joined: 3rd May 2004
Location:
Posted: 11th Aug 2004 16:45
Can someone explain functions to me a little more in detail, maybe an example or 2, or what there useful for...Im just not grasping whats in the manual.

Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 11th Aug 2004 21:25
A function, as far as programming languages usually go, is just a small routine to do a common calculation in a program. Say in a game, you want to know the distance to a certain object and you want to know this at many different locations in a program, then you could put it this small routine in a function and call the function instead at each point in the code. Then instead of having 3/4+ lines of code at each point therby repeating the same commands, you just have a single command calling the function.
Writing your programs this way is good practice. It adds structure to it, reduces the number of lines of code, and makes it clearer also to yourself (when debugging for instance) and other people what exactly is happening with your code.

I'm quite newish to DB, but use functions in other languages quite often. I think in DB, you can use them in place of subroutines also. The difference between a subroutine and a function, is that a function usually returns a number, whereas a subroutine can do anything you want, and is like a small (or fairly large) routine that may be repeated at several locations in your code.

Sorry I can't give you any examples, but I'm in work and anyway, I keep getting DB commands confused with other languages and would confuse you even more if I made a mistake. Unless someone else beats me to it, I'll try and post some examples in the near-future

Code Machine
Peter H
20
Years of Service
User Offline
Joined: 20th Feb 2004
Location: Witness Protection Program
Posted: 11th Aug 2004 22:48 Edited at: 11th Aug 2004 22:56
to supplement what he said...

the basis of a function is a piece of code the runs through once when you call it...the other thing is that it ignores all of the variables in your program that aren't global(I.E. you could have a variable named "health" inside your function and outside your function, but they wouldn't bump into each other...)

like this...


but there is more to a function then that, you can also "pass" variables to it...(Note: any none global variables that you mess around with in a function don't get changed when you exit the function.)

like this...


in this example the "player" object would get faster and faster...but the rate it moved would always be one less then the variable "speed"...

now say you want to know something about the results of what you did in your function(maybe it performs a calculation?) to do so you can either change global variables or use the "return vaiable"

like this...


now the "rate" variable's value at the end of the function is passed to the variable named "value"; and then they are both printed onto the screen...if you run this(DBPro but i haven't tested) the number on top(rate) should be one less then the number on bottom(speed)...


that's pretty much the basics of functions...i gotta go but feel free to ask more questions...

"We make the worst games in the universe."
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 11th Aug 2004 23:08
Thanks Peter H. That's probably a more useful explanation than I gave, with some examples even . The usefulness of functions doesn't really become apparent until you have much larger programs, but I guess start small first ....
Plastico
20
Years of Service
User Offline
Joined: 3rd May 2004
Location:
Posted: 12th Aug 2004 04:03
that clears so many things up for me, this help is greatly appreciated.

may I ask one other question. can you also explain Types a little more to me.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 12th Aug 2004 04:11
A User Defined Type is basically a lightweight object. Think of a Type as a variable, but this special variable carries empty buckets within it. So if you have

Type Person
Name as String
Age as Integer
EndType

Me as Person

now Me is your special variable, an instance of the type. Now in code you clould say:

Me.Name=CattleRustler
Me.Age=34

Also you can array a type:
Dim Us(9) as Person

now you have ten Us items(0-9) each with their own inner properties or buckets of Name and Age. You refer to the specific one just like with any array by specifying the index value

Us(4).Name=Joe
Us(4).Age=20
Us(9).Name=Ted
Us(9).Age=15

hope that helps


DBP_NETLIB_v1.4.3 - 65 FREE Functions * Click Logo
Plastico
20
Years of Service
User Offline
Joined: 3rd May 2004
Location:
Posted: 12th Aug 2004 04:16
well, im out of questions, to bad I can only text thank you all.
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 12th Aug 2004 04:24
UDT are great for storing the properties of monsters, enemy units, and other assorted objects. When combined with an array, it makes it easier to walk through your data and perform actions. For example, an array of bullet UDTs could be use to track each bullet you fire from a machine gun.

type bulletData
objectID as integer
isFired as integer
isExpended as integer
endType

dim myClip(20) as bulletData

In your game loop you can walk through the array to see if any bullets are in flight (fired) or have left the field of play (expended). So, instead of writing code for 20 bullet objectID numbers you write the code for one clip and get the objectID from that.
--
TAZ

"Do you think it is wise to provoke him?" "It's what I do." -- Stargate SG-1
Plastico
20
Years of Service
User Offline
Joined: 3rd May 2004
Location:
Posted: 12th Aug 2004 07:33
ahhh, that explains it even more.

so, how many functions can there be in a game, like lets say for a game like pacman, or another atari age game.
Peter H
20
Years of Service
User Offline
Joined: 20th Feb 2004
Location: Witness Protection Program
Posted: 12th Aug 2004 08:19 Edited at: 12th Aug 2004 08:19
you can use as many functions as you want

"We make the worst games in the universe."
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 12th Aug 2004 10:16
someone said there was a dbp limit of 255 total functions - that cant be true, can it?


DBP_NETLIB_v1.4.3 - 65 FREE Functions * Click Logo
Dodo
20
Years of Service
User Offline
Joined: 8th Aug 2004
Location: eating lunch
Posted: 12th Aug 2004 21:29 Edited at: 12th Aug 2004 21:31
If you used all of them it would have do be a truly huge game
Peter H
20
Years of Service
User Offline
Joined: 20th Feb 2004
Location: Witness Protection Program
Posted: 12th Aug 2004 22:56
a limit of 255 sounds like some internet restriction...

i don't think there is a limit...(but if that is the limit i would never use that many...)

"We make the worst games in the universe."
Plastico
20
Years of Service
User Offline
Joined: 3rd May 2004
Location:
Posted: 15th Aug 2004 12:45
srry, I was gone for a few days, thanks to all who replied to this thread!

Login to post a reply

Server time is: 2024-11-27 06:35:47
Your offset time is: 2024-11-27 06:35:47