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 Discussion / why cant you use arrays like variables?

Author
Message
kfoong
22
Years of Service
User Offline
Joined: 28th Jan 2003
Location: Australia
Posted: 21st Mar 2003 10:01
In Darkbasic classic you cant use arrays like variables. Why not? Every other language you can have the same freedom as of using arrays than variables. Maybe im wrong about that SO SOMEONE PROVE ME WRONG AND GIVE ME A REASON! :-s
--------
http://www.stellarblue.vze.com/
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 21st Mar 2003 12:06 Edited at: 21st Mar 2003 13:56
Think of a db array as a placeholder to a table of variables:


ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
kfoong
22
Years of Service
User Offline
Joined: 28th Jan 2003
Location: Australia
Posted: 21st Mar 2003 12:47
wha..? that was not my question...

--------
http://www.stellarblue.vze.com/
MrTAToad
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 21st Mar 2003 13:24
How do you mean you cant use arrays like variables ? Arrays are there to effectively group variable types together...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 21st Mar 2003 13:46 Edited at: 21st Mar 2003 14:01
well my code is there to show you how to extract the VARIABLES from an ARRAY, you're problem was that you can't use an array as a variable in some situations in DB, and that's because DB is written like a basic language, but all basic languages are essentialy scripts that activate functions written in lower level languages. In this case the language is set up to use something called an array to hold pointers to variables at run/compile time. Like mr TAToad said, an array is not a variable, but a placeholder to variables. Sorry if that doesn't tell you what you need to know, but it does answer your question, and that code I posted should tell you how to extract the variables from the array. Better?

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Richard Davey
Retired Moderator
23
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 21st Mar 2003 13:48
kfoong - post some example code of what you're trying to do (doesn't matter what language the code is in).

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 21st Mar 2003 14:00
Ok I edited that code above, it now has comments ( thanx rich )

Maybe i was a bit vague in my first post but the operative words wer "DB array"
I was trying to say that you should not think of a db array the same as you would say, a qbasic array the true lack of freedom in a db array is the inability to redim, but that's no biggie to workaround either

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Richard Davey
Retired Moderator
23
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 21st Mar 2003 14:06
I'm not really sure I understand what it is he's trying to do - in my mind "using an array like a variable" means doing something like:

inc X, arrayname[3]

or whatever - replacing a variable with an array element dynamically like that which you can do in DB or DBPro.

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 21st Mar 2003 14:15
Yeah but if he tried something like:

inc arrayname(1)

he'd get an error message something like

"cant increment an array"

my workaround is simple, arrayname(1)=arrayname(1)+1


but I put the code above in case he's doing a lot of operations on one array element, simpler to extract it and pop it back in after the manipulations

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
kfoong
22
Years of Service
User Offline
Joined: 28th Jan 2003
Location: Australia
Posted: 22nd Mar 2003 00:24
exactly what i mean rich. and danmatsuma which one is faster?

inc arrayname(1),1

or

arrayname(1)=arrayname(1)+1

can you tell the difference? you also cant use arrays for functions too.

--------
http://www.stellarblue.vze.com/
Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 22nd Mar 2003 00:30
arrayname(1)=arrayname(1)+1 is the only possible solution, as you cant INCrement arrays. I'm not sure bout DBPro, but arrays can be used in functions in DB Classic, i.e you declare an array at the beginning of the program, you assing values to it, and you can just use the values in the function, without having to load it in using function functionName(array(10)), making it a grouping of global variables.

Whatever I did I didn't do it!
kfoong
22
Years of Service
User Offline
Joined: 28th Jan 2003
Location: Australia
Posted: 22nd Mar 2003 01:27
kentaree it doesnt work. try it out for yourself.

it says "function declaration has unknown parameter"
and i have declared the array.

heres the snippet


--------
http://www.stellarblue.vze.com/
MrTAToad
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 22nd Mar 2003 02:03 Edited at: 22nd Mar 2003 02:04
Ah - you cant pass arrays to functions... Why, I dont know (probably due to pointer passing), but hopefully (for DBPro at least), it will get sorted at some point in the future...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 22nd Mar 2003 05:13
you actually don't need to pass an array to a function, ever.

Just declare the array global by dimensioning it at the top of your code and use the array inside your function.

like this:




ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 22nd Mar 2003 05:16 Edited at: 22nd Mar 2003 14:27
kfoong: "exactly what i mean rich. and danmatsuma which one is faster?

inc arrayname(1),1

or

arrayname(1)=arrayname(1)+1

can you tell the difference?"

well,

inc arrayname(1),1
will crash your program so it's only a faster way to end your game
I'm not sure I get your point...

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
kfoong
22
Years of Service
User Offline
Joined: 28th Jan 2003
Location: Australia
Posted: 22nd Mar 2003 07:45
still it doesnt mean the array has the same freedom as a variable. and danmatsuma i meant IF it worked. some people...

--------
http://www.stellarblue.vze.com/
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 22nd Mar 2003 08:52
lol *plonk*

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 22nd Mar 2003 12:21
Uhm, kfoong, what I said in my post is that you can use arrays in functions WITHOUT having to pass them to arrays like function functionName(array(10)).

Whatever I did I didn't do it!
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Apr 2003 16:05
In my short experience, you CAN'T declare an array globally, and use it in a function, the compiler fails to find the declaration.

However...I saw in passing a workaround, and that is to declare it again in the function, in code that will never execute...



Thanks in advance.
All the Best,
StevieVee
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 7th Apr 2003 18:20
Quote: "
you actually don't need to pass an array to a function, ever.
"

Well, there are circumstances where you would want to be able to pass arrays to functions.

Ogres have layers.
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 8th Apr 2003 06:46
Yes you might want to but you don't ever need to
Sure it'd make life easier in some cases, but if I had a real problem with it I'd be using a different language, simple.

StevieVee: declaring arrays globally and using them in functions is the very basis for the way I program in db, but I'm gonna see if that idea could be used to redim arrays on the fly, might make it possible to do a better linked list...

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Hell IVIonkey
22
Years of Service
User Offline
Joined: 1st Apr 2003
Location: Outer Limits
Posted: 8th Apr 2003 07:08
If you happen to have a dummy variable handy, such as "foo" or "temp," you can solve your problem with a very low-cost extra operation:

foo = array(x,y)
GenericFunction(foo)
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 8th Apr 2003 08:26
A [Typed Array] can be fed to a function in the way you want it to.

indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 8th Apr 2003 08:31
heres a sample for you to drool on allthough handing a Typed array to a function like this is better when you need to perform calcualtions
than a simple display update in this regard.

They are Global so they dont even need to be updated through the function paramter yet it can help as It did for me with a dynamic object problem.

anyway snippet.




Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 8th Apr 2003 09:44
Yes it would be wonderful to have types in classic

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 8th Apr 2003 10:07
Well golly gee here's the results of my test, a redim function:



ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 8th Apr 2003 10:11
yes psuedo types are part of darkedit for DBC

Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 8th Apr 2003 13:45 Edited at: 8th Apr 2003 13:59
I didn't even open that!! Checking out now.
[edit]
Thanks Indi, I always thought that was just a more windows based editor with a few templates for if,else,endif type stuff, so I just ignored it. Woulda cut my dev time by half damnit, might've finished my retro entry on time too
Anyhow, great to have types, I was just extending that redim function I posted to do this exact thing

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 8th Apr 2003 14:55
Quote: "
Yes you might want to but you don't ever need to
Sure it'd make life easier in some cases, but if I had a real problem with it I'd be using a different language, simple.
"

Yeah, but then you don't ever need functions, gosubs, arrays or conditional loop commands, too. There's always a work-around.

Ogres have layers.
trager
22
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 8th Apr 2003 16:54
I find the implicit global generation a bit iffy.

I mean at what point does the top of the code end?

If you declare loads of types before you start declare the arrays you may already be on line 200!

Personally I always get crap results if I try to treeat an array as a global array.

Was kinda hoping that you can pass arrays to functions in DBpro can anyone verify this?

There is no answer only more questions.
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 8th Apr 2003 17:58
Hehe, this thread became a waste of my time weeks ago, just a bunch of guys complaining, Shoulda realized it would stay that way, have fun with it

ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Red Giant 2183
22
Years of Service
User Offline
Joined: 15th Dec 2002
Location:
Posted: 18th Apr 2003 09:42
ok , the answer is quite simply "pointers". you cannont increment pointers the way you can simple variables., since db uses pointers for arrays, inc somearray simply wont work. If you are interested in a more detailed explanation of how pointers work, ect, i suggest you get c++ for dummies by Randy Davis, It has a nice section in it that deals with this.
cheers. x-d

RGteam~
Hell IVIonkey
22
Years of Service
User Offline
Joined: 1st Apr 2003
Location: Outer Limits
Posted: 18th Apr 2003 10:47
The goal was not to increment the array value, but to pass it to a function. Seemed that he wanted to pass a value from scancode() to a function, then interpret that as an array. Another problem is that variables (or arrays / many pointers) are not global, since the user is not given access to the * or & commands that exist in C/C++. Hope that made some sense...
Red Giant 2183
22
Years of Service
User Offline
Joined: 15th Dec 2002
Location:
Posted: 18th Apr 2003 14:58
this is what he said ,
"exactly what i mean rich. and danmatsuma which one is faster?
inc arrayname(1),1
or
arrayname(1)=arrayname(1)+1"

now, as for passing a value to a function , he could have simply called a function , and placed the scancode call within the function itself. also, if you declare you arrays at the very top of the program before anything , they are global. example
in main file :


in include file :




you will notice that the function showdummy1 used the array declared at the top of the program , without giving you a compiler, or run time error, why ? because , any array declared at the top of the program is automatically considered global








cheers~

RGteam~

Login to post a reply

Server time is: 2025-05-16 16:22:47
Your offset time is: 2025-05-16 16:22:47