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 / GLOBAL and Functions

Author
Message
Derek Darkly
12
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 12th Feb 2016 00:28
Here I am with my usual remedial questions... after 5 years I'm still learning DBP... LoL
So here goes:

True or False?
In order for variables to be recognized by functions they must be declared as GLOBAL, else they will be treated as new variables in the function itself.
Am I even close?

Also, is there a way to make all variables GLOBAL by default without having to declare them all individually?

That's all for now, Thanks!
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 12th Feb 2016 02:46 Edited at: 12th Feb 2016 02:49
True, although you can also pass them in as arguments without making them global.

You really wouldn't want to make every variable global by default, scope is a thing for a reason.

Also note, Arrays are always global in dbpro
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 12th Feb 2016 03:40
You could use subroutines in place of functions.
Derek Darkly
12
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 13th Feb 2016 16:07

Quote: "you can also pass them in as arguments without making them global. "


Now that you mention it, I guess this is sort of the whole point of functions, isn't it?
See, I know I'm slow, I just don't always know why.

Quote: "You could use subroutines in place of functions."


True, which is what I've done since I started, but as I plan bigger programs I'm starting to weigh the value of functions against just using subs with arrays to handle many variables. Being slow, I have yet to reach any conclusions. I suppose it's best to be taken on a case-by-case basis in one's code.

Hmm, I wonder if there are any good threads which debate this topic...
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 13th Feb 2016 18:35

One way of making the decision when to use a function or a sub routine is how you want call that block of code. Functions allow us wrap up block of common code into what become user defined (custom) commands. Some of the most obvious stuff initially will be using functions to bundle together code that performs some common calculation, or common task (like setting up an object/sprite/sounds etc etc). Such functions can then be used (called) in other expressions just like it was can internal command/functions.

Pseudo Basic Code


In the example code the function performs a common place integer addition math operation. Now normally we wouldn’t make functions this simple, but the calculations done inside the function can be anything, so the point here is it gives us a way to hide those calculations away from the higher level code that calls them.


There's a lot of this stuff hidden away in the boards, but finding it with the forum search isn't much fun, so prolly worth an external google.

TDKman wrote this tutorial set about a decade ago, the following link is the PlayBASIC version but there is a DB classic/ DBpro version of it on here some where. It includes a function primer
Beginners Guide To Basic Game Programming (Index)

smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 20th Feb 2016 00:36 Edited at: 20th Feb 2016 00:46
I use the crap out of functions, their good for organizing and you can reuse them sometimes for different parts of a program, it is true for a variable to be called outside a function it needs to be a global but as ortu said only the ones that have operate in different functions or called from outside need to be global-ed. From my own personal experience learning db ive found it easisest to break a program into many parts and each part goes into its own function and if its even remotely big I put it into its own source file that's included int he file browser. lots of comments and naming variables and functions with purpose and precision. Nothing worse then a few thousand lines of codes with functions subroutines and your trying to track down a problem where non of the variable names relate to what they do. comment comment comment. huge blocks of comments lol

most of my programs look like this
A child\'s dream never dies.
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 26th Feb 2016 00:21
Here's the link to the DarkBASIC classic/pro version of the TDK's tutorial series.. Pretty old now, but somebody might find it useful
Programming Tutorials For Beginners [DBC/P]


PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Derek Darkly
12
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 10th Mar 2016 11:23
Thanks for the responses!

After trying different ways of organizing my code, I have to agree... it's much easier to read and fix your code when it's already broken down into many functions. I'm finally seeing the light.

You should have seen some of my old walls of unorganized code...like finding needles in haystacks!

My current experiment is a random room generator which involves, among other things, selecting the room size, the entry and exit doorway style, which segment of the room the doorways are located in, where one room connects to another, whether or not there is a door, whether the door is open, closed or locked...eventually I'll add a random roll for stairs, enemies, decor & furnishings, possibly trap doors, etc.

Functions are making it much easier to track all of these small components. Yay functions.
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 11th Mar 2016 00:02
Nice

Next you will want to start organizing into modules across multiple smaller source files.

Like one for character stuff, one for ui stuff, one for level loading etc
Derek Darkly
12
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 19th Mar 2016 12:16

Quote: "Next you will want to start organizing into modules across multiple smaller source files."


If you're talking about using #INCLUDE and having my modules in separate files and opened in separate tabs, the only major problem I've had with that method is when I get an error it gives me a cumulative line number, and I have to try and figure where the error line is.

I suppose there may exist a solution to this, I just never thought to ask about it on the forums.
James H
16
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 19th Mar 2016 12:33
Quote: "If you're talking about using #INCLUDE and having my modules in separate files and opened in separate tabs, the only major problem I've had with that method is when I get an error it gives me a cumulative line number, and I have to try and figure where the error line is"


The solution is to find the temp folder which contains a single dba file that has all the code in the one file and the line number should then be the correct reference to best of my knowedge
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 19th Mar 2016 22:16
Yes, as James H said, the file name is fullsourcedump.dba, I think the temp folder is in the dbpro installation folder or the compiler folder. The line number will be correct in this file.
Derek Darkly
12
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 23rd Mar 2016 21:29
Quote: "Yes, as James H said, the file name is fullsourcedump.dba, I think the temp folder is in the dbpro installation folder or the compiler folder. The line number will be correct in this file."


Ah! Thanks James and Ortu! I may go back to the tab method now that I know this...

Without these forums I would have given up on DBP long ago.
You'd think after 15 years Lee and the boys would take a weekend or six and update the DBP help files, but since I haven't bought the license yet who am I to complain?

Login to post a reply

Server time is: 2024-04-20 02:33:09
Your offset time is: 2024-04-20 02:33:09