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 / #Includes and functions?

Author
Message
gbison
15
Years of Service
User Offline
Joined: 15th Dec 2010
Location: USA
Posted: 3rd Oct 2012 04:43
Hello all,

Im actually just getting a break from some other code to tinker with Dark Basic a bit. So I have setup an architecture for a casual game concept and I go to outline that in code only to find that I'm having an unbelievable amount of trouble with includes and functions. I mean I must really be having and off night and Im sure Im about to feel real stupid here but....can you not #include functions in DBP, if so why if I #include "myfunctions.dba" at the beginning of my file does it scream that all the functions are duplicated and will not run??

So for this, I search and to my amazement found nothing. Thanks in advance.

"The greatest trick the devil ever pulled, was to convince the world he didn't exist."
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 3rd Oct 2012 05:00 Edited at: 3rd Oct 2012 05:01
This is probably due to the IDE automatically including the .dba as a source file. Depending on what IDE your using, you can usually see in the project manager or what ever it's called the files attached to the project.



Basically you can just include/add files through the IDE instead of using #include (which I swear is buggy, can't remember). I think when you create a source file within a project it just adds it to the project, so remove the #include and everything should be up and running.
gbison
15
Years of Service
User Offline
Joined: 15th Dec 2010
Location: USA
Posted: 3rd Oct 2012 06:59
Quote: "IDE automatically including the .dba as a source file"


hahahah yea that would explain it and you are exactly right. Much appreciated. Should be a big "Gotcha" sign somewhere.....

So in knowing that(buggy)....can you turn it off? or does it just simply have to be that way with DBP?

Sorry for the seemly stupid questions, really just trying to get to know the DBP and AppGameKit environments.

"The greatest trick the devil ever pulled, was to convince the world he didn't exist."
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 3rd Oct 2012 12:46
Quote: "So in knowing that(buggy)....can you turn it off? or does it just simply have to be that way with DBP?"


I mean't the other way round. It's more buggy to use the #include command from my experience. Though I haven't used it in a long time so it might be fine. But if you'd rather use it then simply remove the source files from the project. If you right click on the source file through the IDE, you will see the option Remove From Project.

Another thing is if you have multiple separate files it's best to create subroutines (gosubs) to declare or initialise the variables needed in those files, because if you use variables from another function before it's variables have been declared then it won't work. So we do something like this:

Main.dba


Math.dba


You can do this with functions, just note that gosub's are quicker than functions.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 3rd Oct 2012 14:02
It's not necessary to use a "gosub" to declare a variable, you can declare variables anywhere in the source code. The only time you need to use a subroutine or function is if you need to execute code. For example, assigning a value to a variable, or "dim"ing and array.

[b]
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 3rd Oct 2012 14:39
Quote: "It's not necessary to use a "gosub" to declare a variable, you can declare variables anywhere in the source code."


Ture, I would say I only use it for declaring core variable all the functions use like maths for instance. Everything else is usually self-contained. Since some variables don't need to sit in memory doing nothing for ages, there's no point declaring them at the start. And most variables can be local instead of global if you refine your code.

Though I've always wondered about big array's, creating a massive array in the middle of runtime will surely give a performance hit since it has to suddenly allocate a large amount of memory, similar to deleting/adding element to a large array is slow. So is it best to declare all big arrays at start up even if there not used right away?
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 3rd Oct 2012 17:49
Non function-local variables will sit in memory the whole duration of the program wherever you declare them. They are compiled as an empty space into the program executable at compile time. You could declare hundreds of variables and no actual code would run at runtime, so it's not necessary for the execution of the program to pass through the lines of code where they are declared.

Arrays are different because they are made up of two parts. Internally they are just like any other 4 byte variable, which will initially be zero. However, the dim command is used to declare them and the dim command does run code at runtime. This code allocates the memory for the array and stores the address in the variable for that array. If this code fails to run (for example its in a different source file, and you haven't used a function or subroutine) the array will still exist as a variable, but trying to use it will cause problems as no memory has been allocated for it.

[b]
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 3rd Oct 2012 18:44
Oooh! Very informative. Thanks

Login to post a reply

Server time is: 2026-06-26 03:08:41
Your offset time is: 2026-06-26 03:08:41