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 / Variable Scope in DBPro - Is this how it works?

Author
Message
Jaze
20
Years of Service
User Offline
Joined: 20th Nov 2003
Location: Connecticut, USA
Posted: 25th Nov 2003 16:28
The only globals are arrays? So Each Function manages its own variables, and the main program can only share info its in an array (besides passing to a function of course)
???

Is this how it works?

Additionally - does declaring a structure make it global? Like:

type rtMYType
SomeVar as integer
endtype

rMyType as rtMyType

Would rMyType become global like this?

thanx - I'm doing tests but I wanted to hear the theory stated from a fellow coder please

-=/Jaze/=-
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 25th Nov 2003 16:55
There's a GLOBAL statement in DBpro. So
GLOBAL rMyType as rtMyType
would make the variable accessable inside functions.

Me, I'll sit and write this love song as I all too seldom do
build a little fire this midnight. It's good to be back home with you.
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 25th Nov 2003 16:58
I'll let someone else go into types with you, but as far as I know types are arrays so they are global.

You can also globalise a variable for use anywhere in your code, in fact it's a good idea to globalise your common variables whether you need to or not because DBPro knows where to get at it's global variables, saving it from having to go looking in memory for variables or making new variables inside functions.

GLOBAL n

This will make variable 'n' global. If you use the variable 'n' a lot like I do, making it global will speed up any function that uses it. Globals are also a god-send for passing more than one parameter back from a function, for example you might want to return destination coordinates from a collision check function, you can return a flag with the function and store anything you like in globalised variables that you might need.


Van-B


I laugh in the face of fate!
Jaze
20
Years of Service
User Offline
Joined: 20th Nov 2003
Location: Connecticut, USA
Posted: 25th Nov 2003 18:15
Thank You - I'm trying to learn the best Code Organizaion and stuff for making an API for my "tools" and functions I make etc. Need to think Scale, (global versus local..speed constraints.. Like suggestion that DBPRO handles GLOBALS better than dynamic self declared variables) Naming conventions etc.

This language has its own flavor if you know what I mean... each language/compiler has thier ins and outs.

I learned the constant# keyword from a code snip and wasn't able to find that syntax in the dbpro help (Using demo - the one I bought is in the mail from the UK to USA)

There is a lot to learn - and the syntax is flow is kind of strange to me because many functions are multi keyword .. like "object position x() and for most operations I end up with long lines of code to simply move an object from HERE to HERE + some value on the y plane or something.

Once I get a handle on the coding constructs etc... and organize a library of source - like an API, I should in theory start being able to make something respectable once the collection of "tools" gets hefty and tested and I start putting them together.

Thanx for the help I really appreciate it
Learning curves are tedious and cruel

-=/Jaze/=-
walaber
20
Years of Service
User Offline
Joined: 22nd Oct 2003
Location: Los Angeles, CA
Posted: 27th Nov 2003 07:18 Edited at: 27th Nov 2003 07:28
i have found for me using variables and TYPES to store all object locations makes for much cleaner code. for example:

TYPE ThreeDPoint
x as float
y as float
z as float
ENDTYPE

TYPE Object
ObjNum as integer
Pos as ThreeDPoint
(more stuff here depending on project)
ENDTYPE

then define all objects with this TYPE
Player as Object

Player.Pos.x = 100
Player.Pos.y = 100
etc...

then when you want to move the player you can just

inc Player.Pos.x,1

then each loop, update your objects to these locations
example:

position object Player.ObjNum,Player.Pos.x,Player.Pos.y,Player.Pos.z

just my way of doing things... if anyone else has an even cleaner way I'd love to see it... even with this organization my code always gets sloppy toward completion of a project...

Go Go Gadget DBPRO!
Jaze
20
Years of Service
User Offline
Joined: 20th Nov 2003
Location: Connecticut, USA
Posted: 27th Nov 2003 17:05
Thanx to you guys - Good Suggestions.

Walaber - I was messing with that approach as well because its the only way to organize in DBPro. I've used a lot of BASIC's from QBASIC right Through Microsoft's Professional BAsic 1.1 (Dos - After QBASIC before Virtual Basic) right up through VB6 and other dialects as well. DarkBasic seems more like its own scripting language with strange variable scope rules.

What you are trying to do by putting various types together reminds me of object oriented design and "classes" programming in object oriented languages.

Like in C++ and FreePascal .. You can do things like:

class t3DPoint
x as integer
y as integer
z as integer
endclass

class clsmybadguy as t3DPoint
Speed as float
endclass

MyClass = New clsmybadguy


Now you can do MyClass.x = 10 (As well as) MyClass.Speed (Without having to do MyType.threed.x) (which gets wordy...like you said - even when you try to stay "clean" the code gets muddy)

I'm finding myself getting frustrated with the loose type checking and dynamic variable declaration. EXAMPLE: How many times have you had a bug because your program was doing math on X# but a typo of "x" compiles with no problem and your program doesn't run as expected.

I wish you could make darkbasic not allow "dynamic" variables ... and only those you declare via global, dim, (declare? declare may not be even a db command)

Anyone else have a veiw or suggestion?

-=/Jaze/=-

Login to post a reply

Server time is: 2024-09-21 12:00:33
Your offset time is: 2024-09-21 12:00:33