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 / Function...

Author
Message
Xenocythe
19
Years of Service
User Offline
Joined: 26th May 2005
Location: You Essay.
Posted: 16th Jul 2005 00:09
function draw_to_screen(object position x(1),object position y(1),angle#,hit,t,laser)


That is the function declaration I have. The problem is, when I run it with the program and Function I have (function is below the loop) it highlights that function declaration and says :
SYNTAX ERROR. FUNCTION DECLARATION HAS UNKNOWN PARAMAETERS.

What in the world is wrong with the parameters?

Enjoy Life while your still alive.
Enjoy a recees anytime!
Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 16th Jul 2005 00:17
yes, you cannot use those parameters.

Or you have to do this:


Or this



Immunity and Annihalation makes Immunihalation...
Xenocythe
19
Years of Service
User Offline
Joined: 26th May 2005
Location: You Essay.
Posted: 16th Jul 2005 00:45
Thanks.

Enjoy Life while your still alive.
Enjoy a recees anytime!
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 17th Jul 2005 11:36 Edited at: 17th Jul 2005 11:37
Hey GreenFox, I,ve been reading your posts lately and I can't wait for the game you come out with...


(sounds like it'll be good)


By the way, what is a function, and why do you use one? I've heard about their signifigance in organizational issues but I havn't tryed using one.

Thanks

Weird = Unique. Unique = Human.
Im human.
NanoBrain
20
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 17th Jul 2005 12:29
Ooska,

Organization is the main reason for functions, and for much smaller code. Let us say that you have to write three lines of code to make a specific calculation. Let us also say that the same lines of code are elsewhere in your source code, making a total of two locations in your source holding the same three lines of code.

Would it make more sense to keep the above as so, with two locations of the exact same lines of code? This is where functions come to play. That, you can place the three lines of code in a function and call them when needed within the source code.

In the long run, this method saves on you having to type so much. Lastly, they can help when needing to update or edit parts of your code. They are more useful in larger programs and games. In smaller programs, let us say, that only have about fifty lines of code, they are not so useful(may I be corrected if wrong).


+NanoBrain+
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 17th Jul 2005 14:51
Woah! Those things are helpful! I have had a need for functions for a long time! This is great news, that this is possible, but I can't use functions since I still don't know how to impliment them.

Weird = Unique. Unique = Human.
Im human.
Xenocythe
19
Years of Service
User Offline
Joined: 26th May 2005
Location: You Essay.
Posted: 17th Jul 2005 15:21
@Oaska
Thanks, i'm trying my best to make it great. If you want the plot, e-mail me at [email protected] ... I don't want to spoil it for everyone

@NanoBrain
Once again, thanks and great explanation. You should make a tutorial for beginners! That would be great!

Enjoy Life while your still alive.
Enjoy a recees anytime!
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 17th Jul 2005 21:15
Quote: "
Woah! Those things are helpful! I have had a need for functions for a long time! This is great news, that this is possible, but I can't use functions since I still don't know how to impliment them.
"


Functions can do a lot of things. Here's some examples:



As you can see functions are easy.
Zotoaster
20
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 17th Jul 2005 21:52
a function is basically just a custom command

Your signature has been erased by a mod because it's larger than 600x120...
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 18th Jul 2005 13:30
OK, I get it! Thanks.
(Greenfox, I dont want the plot, (t'would ruin it for me too!) but, I would like to know if it's sci-fi,fantasy,strategy or whatever.....)
( [email protected] m )

Weird = Unique. Unique = Human.
Im human.
Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 18th Jul 2005 13:50
Uh oh! I just ran into a function problem......

Im trying to make a function called "movement()"
I know what I want it to do but I don't know how to tell the program what I want it to do.
Heres what DOESNT work


function movement()
if upkey() then playery=playery-3 : sprite 1,playerx,playery,1 : sync
if downkey() then playery=playery+3 : sprite 1,playerx,playery,1 : sync
if leftkey() then playerx=playerx-3 : sprite 1,playerx,playery,1 : sync
if rightkey() then playerx=playerx+3 : sprite 1,playerx,playery,1 : sync

end? endfunction? *shrug*



I know I need to define what the function starts and where it ends...

Weird = Unique. Unique = Human.
Im human.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 18th Jul 2005 16:57 Edited at: 18th Jul 2005 17:01
It's probably because playerx and playery aren't defined outside the function. Any variable that you have outside the function and you want the function to change the variable it either needs to be dimensionalized "dim playerx(0)" or set to global with "global whatever".

It's also better to just use one sprite command and sync at the end of your function.

What's weird is I tested this code in both Classic and Pro... Pro needs the global command and Classic doesn't. Classic doesn't even know the word global and has to be removed.

Do you use Classic or Pro?

Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 18th Jul 2005 19:37
Again, THIS A DBC FORUM!

The function you created should be this way:


just like grog said. The function will work.
The variables used in the function cannot be used in your main loop. (well, you can use them, but they will lose the values of in the function). Though, within the function, those values will not be lost. The only problem with your code is that, when you start your program, playerx and playery will always be 0. (left-top of the screen)

Immunity and Annihalation makes Immunihalation...
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 18th Jul 2005 20:41
Quote: "You should make a tutorial for beginners! That would be great!
"


http://www.thegamecreators.com/data/newsletter/newsletter_issue_27.html#9

Although it states that it's for DB Professional, I don't see any reason why it won't work for Classic.
If you find any problems, let me know and I'll see if I can update it to be compatible with both.

BatVink
Keaz
21
Years of Service
User Offline
Joined: 22nd Sep 2003
Location: Somewhere in south Texas
Posted: 19th Jul 2005 02:24
@Sven B
I don't see the difference in your function?

Breaking Stuff=Fun!,Bug Testing<>Fun!, Bug Testing=Breaking Stuff, so...
Bug Testing=Fun! Hmmmm....
DOES NOT COMPUTE! SYSTEM MALFUNTION!
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Jul 2005 03:25
Quote: "
@Sven B
I don't see the difference in your function?
"


That's because it's done right.
NanoBrain
20
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 19th Jul 2005 13:31 Edited at: 19th Jul 2005 13:41
Ooska,

Though Sven B's snippet function is correct, the function from it's beginning is useless the way it is set up. This is, unless the code to move the object with the implemented variables' values is going to be within the function itself, also. This is because the variables inside the function are local to that function, and will not effect any values outside of the function. This is true even if there are variables outside of the function that have the same exact names as the variables inside the function.

To fix this, the variables can be passed to the "outside world", by the use of the return value. The return value, which can only be a single value(variable), is placed after endfunction, with space between the two words. For example, let us say that you have a variable named playerxpos which is withinside the function, and edited within the function. To pass playerxpos to the outside world, the end of the function would be setup like so:



Remember, that only one value can be returned to the outside world. So, if you have two variables within the function, and you need return both of their values, you cannot. Only one variable can be returned with it's newly edited value.

We recieve this value from the function call by adding the function call to a variable. For example, let us say that we need to recieve the value within playerxpos(the variable from within the function) into a variable named newvalue. We do this just like when we pass a value from a variable to a variable, or when we fill a variable with a specific value, whatever it may be.



These two examples, their methods together, will pass a variable that is within a function to a variable outside of a function.

However, what is the point of passing the same value, which can't be edited within runttime, to another variable, when you could simply do so at the start of a program, in your setup section? A value can also be passed to a function, to be used within a function, and then it's new value passed back to the outside world. This is mainly the whole purpose of a function. Which is, to pass values to a function, and the function to create a standard calulation on the value, and then pass the new value back to be used in the outside world.

To pass a value to a function, beit a variable or a pure value, you make use of arguments, also called perimeters. These arguments are placed within the brackets () of the function call and the function itself. Arguments are simply variables or values, which are within the brackets. For example:



Pay close attention to how the perimeters are setup. The above is a runnable example. But how, seeing that the names within the function call's perimeter list differ from the names within the function's perimeter list? This is because names do not matter, not even the name of the return value, in relation to the variable in the outside world that has the return value passed to it from the function. Only the types matter.

What do I mean by types? The types of values within the perimeter lists. There are three types of variables, real(with a decimal point), integer(whole numbers), and string(characters). These types must match from perimeter to perimeter. Also, make sure that there is a comma seperating each value or variable with the argument lists, and that the argument lists are both setup the same. To clarify, if the function call's list includes an integer, then a real number, then an integer, then the function's list must be setup as so.

To use a value in a function's argument list within the function, use it by it's name that is within the argument list. If there is a variable within the argument list named newtotal#, and you wish to add a value of 5 to it, take this example:



To conclude, remember that all variables within a function are local to that function, and do not effect variables within the outside world, unless the values are passed to a variable within the outside world. Also, in liking, any variables within the outside world are local to the outside world and do not effect any variables within the function, unless passed to the function. This is why they are called local.

However, arrays are naturally global, and can be edited within a function without having to pass their values to the function. Also, if a global variable is edited within a function, it will also be edited outside of the function as well, even without returning the value.


+NanoBrain+
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Jul 2005 16:30 Edited at: 19th Jul 2005 17:28
Of course in this case since it is the x and y of a sprite it can easily be found out and changed outside of the function.



How about this?

Eddie B
20
Years of Service
User Offline
Joined: 10th Apr 2005
Location:
Posted: 20th Jul 2005 07:47
No, Becuase you have to have a differant number for each sprite.

Ooska
20
Years of Service
User Offline
Joined: 29th Aug 2004
Location:
Posted: 20th Jul 2005 11:59
OK! Thanks guys! I pretty much have it down.
I read through your post, Nanobrain and (as usual) I lost ya by about the third or fourth snippet. But I looked at both of Grog Grueslayers posts and I figured out most of it.

Thanks again for your help guys!!

Weird = Unique. Unique = Human.
Im human.
Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 20th Jul 2005 19:08 Edited at: 20th Jul 2005 19:08


Grogs code, but it works for all sprites

Immunity and Annihalation makes Immunihalation...

Login to post a reply

Server time is: 2025-05-22 19:09:22
Your offset time is: 2025-05-22 19:09:22