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 / possible to have an array within a type?

Author
Message
Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 19th Jul 2010 11:12
This is what im after:


I need it so i can store pathfinding data for individual players.

Thanks, Aralox

bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 19th Jul 2010 11:16
I wish

Unfortunately not.

You'll have to use a multidimensional array to do this.

DIM array(2,3)

Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 19th Jul 2010 11:18
Hey thanks for the quick reply. Oh well, i guess ill try and find a workaround. Multidimensional arrays wouldnt work in this case, maybe theres a way with vectors or something. hmmmmmm

Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 19th Jul 2010 11:28
Actually, multidimensional arrays do work
Quick question: is there a fast way of sorting arrays by their value? eg if thing(7) = 19 and thing(4) = 55 is there a way of making thing(4) thing (1) and thing(7) thing (2)?

Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 19th Jul 2010 11:30
Vectors? - not sure how they would help, or why multi-dims wouldnt.

Quote: "type mytype
thing(3) as array?
endtype

dim hello(2) as mytype

so i can do stuff like hello(1).thing(2) = 4"


Why can't you do this?




Health, Ammo, and bacon and eggs!
Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 19th Jul 2010 11:33
hehe yeah youre right - just took a little clear thinking on my part to realise that

Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 19th Jul 2010 11:41
As for sorting, well maybe not ideal to sort something that is tied in with an object. So if thing(3) for instance was also object 3, then really your stuck with it as it is, or you could make an index - an index might be a simple dimmed array with the index (3 for example) and the sort value, maybe the world position or something. That way each index would be sorted by it's sort value.

Dim thingdex(3,1)
So 3 things, with 2 indices in the index, indice #,0 is the index, indice #,1 is the sort value.

thingdex(0,0)=0 : thingdex(0,1)=10
thingdex(1,0)=1 : thingdex(1,1)=80
thingdex(2,0)=2 : thingdex(2,1)=20
thingdex(3,0)=3 : thingdex(3,1)=95

So that's the ID in slot 0, and the sort value, Y value, height, whatever in slot 1. Then it's easy to sort those with a boolean sort.


For a=0 to 3
For b=a+1 to 3
if thingdex(a,1)<thingdex(b,1)
t1=thingdex(a,0)
t2=thingdex(a,1)
thingdex(a,0)=thingdex(b,0)
thingdex(a,1)=thingdex(b,1)
thingdex(b,0)=t1
thingdex(b,1)=t2
endif
next b
Next a

So you'd have a index of ID's, sorted by whatever you need - if you step through thingdex then draw the items in that order for instance, then that might be the sort of thing you need. By using an index, you can reduce the amount of data that has to be moved, so rather than sorting a big typed array with all your 'thing' properties, you make an index and sort that, it does the same job except with minimal data.


Health, Ammo, and bacon and eggs!
Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 19th Jul 2010 12:44 Edited at: 19th Jul 2010 12:46
I see, thank you. Another question: is there a way of shuffling down values on multidimensional arrays? I tried IanM's rotate array command, but it shuffles down the first dimension (i need to shuffle the second dimension, the first is just the player ID). Either this or a method of inserting elements into the second dimension, but im pretty sure this is impossible as we cant have jagged arrays.

EDIT: sorry i realised you posted a method, but i was wondering if there was an inbuilt command, or one somewhere in a plugin before i resorted to just writing up the code that will do it

bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 19th Jul 2010 13:16 Edited at: 19th Jul 2010 13:26
Van B showed a sorting method, rather than a shuffling method.

There is no built-in command.

You could put in a feature request on the matrix1utils thread in dll talk I think.

If you're looking to sort, a bubble sort is pretty easy to implement though, if you want to build your own.

If you're looking to shuffle, that's pretty simple.. er wait, my example is flawed...

I'll come up with one later if nobody else hops on it first

Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 19th Jul 2010 13:29
Im sorry, i just cant follow the code =P thank you though, i think conceptually what you are doing is: getting the value of an index about to be overwritten, overwrite the index (eg 5th to 6th), then get the value of the index after (if it exists), and overwriting it with the previously stored data, and so on and so forth. Is this what you did? im really not sure

bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 19th Jul 2010 14:00
Yeah, it wasn't quite right though. I'm at work, so I'll post a commented, correct version, when I get home

Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 19th Jul 2010 14:59
cool =)

bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 19th Jul 2010 15:07 Edited at: 19th Jul 2010 15:09
ok, here goes.



Aralox
18
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 20th Jul 2010 08:43
nice and simple. Thank you, i appreciate it

edta yin
14
Years of Service
User Offline
Joined: 20th Jul 2010
Location:
Posted: 20th Jul 2010 09:41
it wasn't quite right

edta yin
14
Years of Service
User Offline
Joined: 20th Jul 2010
Location:
Posted: 20th Jul 2010 09:42
That's right

Login to post a reply

Server time is: 2025-06-05 20:21:37
Your offset time is: 2025-06-05 20:21:37