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

Author
Message
Black Mesa
18
Years of Service
User Offline
Joined: 25th Mar 2006
Location:
Posted: 13th Apr 2006 01:37
Im re-writing my game code into a number of functions because I want to implement a menu system and I find it will be easier than copying the entire game loop. The problem is Im confused on exactly what I can and cannot do with functions. For instance this code:



I have a variable "isDeleted" that occurs when and object is deleted. The first part checks to make sure an object is there and if so it makes checks if it is within a box. If it is it sets the state to deleted, deletes the object and increase the score. The problem is I get a compiler error if I try and return more than one value, and I need to return both the state of the object and the object variable or I get an error in game. Whats the work around?
musty
18
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Istanbul
Posted: 13th Apr 2006 01:43
Have you considered declaring one or more of your return variables as global?
As far as I know you can not return more than one value from the functions.

"Ignorance is bliss." Cypher from the movie The Matrix
Black Mesa
18
Years of Service
User Offline
Joined: 25th Mar 2006
Location:
Posted: 13th Apr 2006 02:31
Whats the syntax for global variables?
MadBovine
19
Years of Service
User Offline
Joined: 17th Oct 2005
Location:
Posted: 13th Apr 2006 05:27
Before your main loop you declare them, for example:
ZEDi Knight
18
Years of Service
User Offline
Joined: 12th Apr 2006
Location:
Posted: 13th Apr 2006 05:28
A variable is global if it is defined in the main body of code. And accessible by everything. Globals are declared and intialized before your main body of code.

Rem initialize globals
isDeleted = 0


However, I don't know how DarkBasic handles globals at FUNCTION level. FUNCTIONs have their own stack and memory space. So isDeleted is a different variable inside the function and not the isDeleted global. Make sense?

Rem R = Rx Ry Rz
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 13th Apr 2006 08:04
global camera=0
musty
18
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Istanbul
Posted: 13th Apr 2006 08:39
You don't have to assign the value when you declare the variable as global. You can declare global variables first and then assign their values later in the program.
example
-------
global ballx#,bally#,score
do
if something good happens then score=score+1
loop

"Ignorance is bliss." Cypher from the movie The Matrix
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 13th Apr 2006 11:34 Edited at: 13th Apr 2006 11:36
You can do away with the isDeleted variable by using the command object exist().
Below is your code turn into a function. Just call CheckBallInBox(objnum) from in your main loop. This way you don't need any return values from your function but score will need to declared as a global variable or you will not see any change to it.



However this is better still:

Using this function you can check any object in any box.


musty
18
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Istanbul
Posted: 13th Apr 2006 12:51 Edited at: 24th Apr 2006 18:38
I believe it is the best way to declare variables such as score,number of bullets etc. which you want to modify and access everytime as global and keep the local loop and logic variables confined to the functions. This way you can prevent many bugs which will otherwise be very hard to trace . For example if you use a variable named bull for number of bullets and declare it as a global variable and decide to use a function from one of your other programs which happens to have a variable with the same name then you would definitely cause a chaos.
Of course another approach is to use resident functions such as object exist(x) , delete object x etc. This enables you to effect the game world without messing up the variables.

"Ignorance is bliss." Cypher from the movie The Matrix
musty
18
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Istanbul
Posted: 14th Apr 2006 10:00 Edited at: 14th Apr 2006 13:57
I will try to clarify an aspect of functions that is very similar to its c++ counterpart. The functions use local variables and when you calculate some variable's value in a function you can not reach it through that variable even if you set it as a return value by writing something like 'endfunction x' .
If you want to reach that calculated value of x you should use the function call statement as an argument for the parameter you are trying to get.
Below I've written 2 examples.In the first one you get 0 for x because the main program isn't still aware of x. In the second one the value of x is correctly printed as 25.

example 1:
calcx(5)
print "x= ";x
wait key
function calcx(a)
x=a*5
endfunction x

example 2:
x=calcx(5)
print "x= ";x
wait key
function calcx(a)
x=a*5
endfunction x

"Ignorance is bliss." Cypher from the movie The Matrix
Me!
19
Years of Service
User Offline
Joined: 26th Jul 2005
Location:
Posted: 14th Apr 2006 11:48
just to mention, as always functions should be designed, not just hacked into existance, a well designed function (and it`s supporting code) will hardly ever need more than the return variable, personaly, if I need to alter some large amount of data (more than two vars) globaly I would use a subroutine and preserve the use of functions for what they are intended.



Dr Frankenstiens mum told him to make some new friends, not knowing where this was going to lead.

Login to post a reply

Server time is: 2024-11-26 23:40:37
Your offset time is: 2024-11-26 23:40:37