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 / Passing an array to a function

Author
Message
code master
21
Years of Service
User Offline
Joined: 4th Dec 2003
Location: Illinois
Posted: 21st Mar 2006 01:28
Is it possible to pass an entire array to a function, so that the function could extract/manipulate the data contained within? I.E:


later

RUCCUS
20
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 21st Mar 2006 01:42
All arrays have a global scope, from what I can see - unless you're making them local - there's no reason to pass them to a function then as they can be manipulated anywhere.

But there's two ways I can think of off the top of my head,

1. Dim an array as a type and pass the type into the function, like so:



And then you'd manipulate the arrays type fields in the function, if that makes sense... If not, look up Types and search for BatVinks tutorial on them in one of the newsletters, it explains them really well.

The second way would be to use a For/Next loop, it has it's pro's and cons, for one thing loops (if too big) will slow the computer down, but they have to be REALLY big. Anyways, like so:



But like I said, arrays are global so there really is no need to pass them into a function.

Hope it helps,
- RUC'

code master
21
Years of Service
User Offline
Joined: 4th Dec 2003
Location: Illinois
Posted: 21st Mar 2006 02:09
Thanks, but i'm using DBC, so i can't use types. I know that arrays are Global, but i want to treat an array as a type, so i can pass any array to the function, and it would manipulate the specified array. Thanks for your help!

So, any one know how i would do that? is it possible?

later

RUCCUS
20
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 21st Mar 2006 02:33
Ah sorry I didnt realize DBC didnt have types. Well the For/Next loop route is still open. But still, I dont see the need to pass it into an array, I must be missing something. You want to manipulate an array from a function, why wouldnt something like this work:



It sounds like you're trying to reinvent the wheel here.

code master
21
Years of Service
User Offline
Joined: 4th Dec 2003
Location: Illinois
Posted: 21st Mar 2006 03:32 Edited at: 21st Mar 2006 03:35
Basicly what i want to do is:

Create an array for each enemy
Have a function that an array can be passed to

So, i could do this:


and, the function implementation:



Of course, that's a rather pointless example, but in essence, i want to create phesudo types.

later

RUCCUS
20
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 21st Mar 2006 04:24
Ah Ok. Then I would suggest using a multi-dimensional array. The first level containing all of the enemies, the second level containing information about each enemy. Its very much similar to types except you'll be using numbers instead of words, which in some cases is more efficient as you can use them in loops.

So:



Now you've got 3 enemies, each with 100 health and unique positions (the 1st variable is their health, the next 3 are their x, y and z positions).

There still isnt any need to pass the arrays into the function since they're global, but you essentially have a poor-mans version of types. Instead of something like Enemy(1).Health# you have Enemy(1,1). Like I said before this can be more efficient since the numbers can be manipulated easier in a loop rather than the words used in Types.

So, using a basic function you could do something like position the enemies at their coordinates and display their health over their heads:



Adding it all up into a basic program:



Ofcourse this is a basic example but you see what I mean. You can add more dimensions to the array for more classification. For example, going up once more you could have Players>Enemies>Information, and then another category for Allies (Players>Allies>Information). So if there were 2 Player Types, Enemies and Allies, and there were 5 enemies and 6 allies, and each enemy/ally had 10 pieces of information, you'd do something like this:



You could make it easier to understand by storing the default values in variables, like so:



Though that'd cause for more lines and data storage. The route is up to you, I think thats what you meant though.

Goodluck,
- RUC'

code master
21
Years of Service
User Offline
Joined: 4th Dec 2003
Location: Illinois
Posted: 21st Mar 2006 04:37
Thanks Man! Perfect

later

RUCCUS
20
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 21st Mar 2006 04:40
Not a problem glad it worked out.

Login to post a reply

Server time is: 2025-05-23 19:33:52
Your offset time is: 2025-05-23 19:33:52