Variable declaration on source file

Previous topic - Next topic

erico

Hello,

I´m going through the trouble of adapting my code to multiple sources.
Now variable declaration is in one of this files and I call the sub from the main source code pretty much at the beginning before any variable use.

I get a set of errors like:
"BOOT.gbas"(206) warning : GPC1004 variable already exists : qsy

It all works, but do I need variable declaration to happen on the main source file?

spacefractal

#1
you can only do GLOBAL once anytime (even its might work). That GLOBAL variable can been used in all gbas files.

You should just remove that one in BOOT.gbas (or the other one).


Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

erico

But there is no declaration of globals on my main file, just on the BOOT.gbas, where I declare all variables.

My main file uses those variables though. So I take if they are there they are automatically declared and the ones on BOOT are re-declaring? even though first thing I do is gosub to the BOOT file to read the declarations?

spacefractal

did you uses GLOBAL in a function? You cant do that.

Then you need to move that GLOBAL out of the function to the top which is the correct method to do that.

If you only used that variable in the function, uses STATIC instead.

Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

erico

#4
All my variables are globals, the way I see they have to be declared on the main file?

EDIT: well...I will reach a point where my main file is only calls, I will see what happens then.

erico

...is it wise to break all things into split subs? I´m looking into having the cores into it, but it is tempting to have every single thing a part (a file for an enemy...20 source files :O) ;/
Any experience to share?

spacefractal

Try to remove the global and see if you got a another warning.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

mentalthink

HI guys I think if I don't remenber bad this it's a problem with the compiler, when you declare variables into a SUB or a Function for have all ordened appears this...
I have a lot of warnings in my code because I declare the Global variables into another source file and then I call whit functions or Subs...

But erico don't worry, I think doesn't matters too much, I think for the code exploits this don't have too much importance...

We wait to some Master better, what he will say....

TI-994A

Quote from: erico on 2014-May-22...if they are there they are automatically declared and the ones on BOOT are re-declaring?
Hi erico. It seems that way.

Quote from: GLBasic ManualIf you do not explicitly create a GLOBAL variable before assigning it a value you will get an "implicitly created GLOBAL" warning.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!

MrPlow

Make sure you GLOBAL is executed before assignment
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Slydog

#10
Quote from: erico on 2014-May-23
...is it wise to break all things into split subs? I´m looking into having the cores into it, but it is tempting to have every single thing a part (a file for an enemy...20 source files :O) ;/
Any experience to share?

I think it is one of the wisest things a programmer could do.  Each .bas file should have code relating to only one thing, or topic.  Therefore enemies have their own source file.  Input has one file.  Menus another.  etc.  This keeps code clean and uncluttered.  Plus you have the advantage that you can reuse each file in new projects easier.  "Oh, this project has a menu, input, but no enemies?  Great, I can steel my already separate menu and input code, and leave out the enemy code!"  Plus all global variables relating to a topic belong in that file.  For example, if you have a global array of enemies, that would be declared in your enemies module.

For your other problem, could you create a very small project to illustrate your issue?  Like one or two lines each file, and maybe only two files.  I remember globals sometimes being finicky and not behaving as expected.  Most times I just twiddled long enough it started working (ha, don't tell my boss, but that technique fixes a lot of my problems!  Bang it with a wrench!)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

erico

Yep, I will break everything into files. Working multiple source codes is new to me.
Good thing the objects and program parts are already modular, so it is just a matter of moving them to their own files and fix the main code to just have calls.

Having everything in a single code started getting too crowded lately and I would like to experiment with the incredibuild.

I will try a sample project when I finish it, I suspect it might work if I´m not using a variable on the main code.
As Mental said, it works fine, I just get a bunch of warnings.

Thanks everyone for the help.


kanonet

To summarize it and correct some of the wrongs that were posted before:

You need to declare a variable (assuming you use explicit declaration, what you should do), also a GLOBAL.
It does not matter when or where you declare GLOBAL, the GPC will detect your GLOBAL and automatically move it to the right place internally.
A GLOBAL declaration should not be inside a FUNCTION or SUB, it would work bit give you a warning and it is bad style.
You should declare every variable only once.

So you can place your declaration in any file you want/need it, just dont put it into a SUB or FUNCTION. Best practise is to put it where it belongs to E.g. if its part of a lib, put it into the gbas of this lib, so its automatically defined for every project that uses this lib.
Also GLOBALs can cause problems (name collisions) and you should always try anything to avoid them, use as many LOCALS or if needed STATICS as possible, pack the remaining GLOBAL into global types, so you have less individual GLOBALs etc...

Hope that helped you. :booze:
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

mentalthink

Then if I understand ok Kanonet, for declare global variables in another source code, the best way it's do a library... then in the main source make a call to #include or Import... sorry I never compile a library in GLbasic, really i don't know how works...

Thanks for the info, I always thinking this was a problem of GLBasic when calls to the GCC compiler...

kanonet

No need for an (externa) lib, it was just an example. This works with any files like gbas...

Examples that compile:
Code (glbasic) Select
// everything in main.gbas, note that it is used 'before' declaration
aa = 4
GLOBAL aa% = 4


Code (glbasic) Select
// in file main.gbas:
aa = 4

// in file two.gbas:
GLOBAL aa%
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64