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 / Arrays in types - they can be were useful...

Author
Message
n3t3r453r
21
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Russia
Posted: 26th Dec 2002 16:29
Why we can't use arrays in user defined types? 8( They can be very useful! If it is possible to do it, why not to add such function into next patch?

Example using arrays in types:

type weapon_t
name$
power
other
endtype

type people_t
dim weapon(10) as weapon_t
endtype

U see that it is very comfortable to use arrays in types, because you may create the people (for example) only with one command: p as people_t. And all properties will be avaible throught this one "p":

p.weapon(1).name$ = "weaponN1"
p.weapon(2).name$ = "weaponN2"
and ect.

I ask ones more: why don't include functions to create arrays in types to the next patch?
Kale
21
Years of Service
User Offline
Joined: 8th Sep 2002
Location: United Kingdom
Posted: 26th Dec 2002 18:11
arrays in types would be extremely useful! i can't understand why there not supported

What the flame does not consume, consumes the flame.
------------------------------------------------------------------------
AMD XP2100+, Geforce4Ti 4400, 512Mb DDR, Abit KX7, WinXP Home
Fluffy Paul
21
Years of Service
User Offline
Joined: 16th Dec 2002
Location: United Kingdom
Posted: 27th Dec 2002 13:06
The reason that there are no arrays in TYPEs is that the TYPE command simply creates a collection of primitive types. While I admit that it would be very, very useful to DIM private arrays within a TYPE, at the minute only primitive types can be thrown about DBPro. Like, have you tried returning an array from functions? I've not been able to so far. Nobody has been able to tell me for sure if such a thing is impossible so I keep trying different syntax.

Until an array is dealt with in the same way as a primitive type we'll not be able to include them in TYPE statements or return them in arrays or even have multi-type arrays (where you can put different types in different indeces).
Barliesque
21
Years of Service
User Offline
Joined: 10th Nov 2002
Location: Los Angeles
Posted: 27th Dec 2002 19:09
As for functions returning arrays, I think a reasonable way to develop this area of functionality would be to use pointer types. In every language I've used, functions only ever pass a single value. If you want to return an array, then your function creates/modifies a static array and then returns a pointer to its location in memory. Using memblocks you can achieve this kind of thing, though it's a bit more troublesome.

I don't see the connection, though, between passing arrays, and being able to create an array of user defined type. I'd certainly like to see that in future versions of DBpro.
Barliesque
21
Years of Service
User Offline
Joined: 10th Nov 2002
Location: Los Angeles
Posted: 27th Dec 2002 19:27
Wait a second!

You CAN do arrays of user defined type. I've just been looking at an entry in the Code Snippets section of the forum that includes this code:

type cubic X as float Y as float endtype
dim spline(linenumber) as cubic

Works A-O-K for me. I believe Patch 3.1 included a fix in this area that wasn't included in Patch 3.0

...Well, that's a relief. I'd thought you could do that, then saw this topic and thought I must have been wrong.
haggisman
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 27th Dec 2002 20:19
But you cant put the array into the type like in the first post

Specs:- 1GHZ athlon, Radeon8500, 192mb ram, winxp
Barliesque
21
Years of Service
User Offline
Joined: 10th Nov 2002
Location: Los Angeles
Posted: 27th Dec 2002 23:20
Ahh, yes... (Doh!)

Strings are essentially arrays of single characters. So basically, what that example is trying to do is create an array of character arrays mixed with other types in a user-defined structure. Very complex, particularly considering strings don't have a fixed size.

If you make a separate array of strings, then there's no problem, and you can use your own variable type for the other items.

Back to functions returning arrays... I'd forgotten, DBpro DOES offer limited functionality with pointers. Unfortunately, you can't use pointers with arrays or user defined types--So this won't work as a solution. Back to memblocks.
Barliesque
21
Years of Service
User Offline
Joined: 10th Nov 2002
Location: Los Angeles
Posted: 28th Dec 2002 00:17
...Just realized my error. Not reading things properly. Of course, there's no problem using strings within user-defined types. Ignore my previous post, please.

What you COULD do, is make use of the fact that a string is essentially an array. Presumably in the example in the first posting, your game involves a pre-defined set of weapons; so you don't really need to store the full name of each weapon for each player. Use each character to signify a different weapon: "A is for Axe" "B is for Blunt Instrument" "C is for..."

Another option, use the character position in your string to refer to each weapon. "NNYNNY" would mean this player is carrying weapons 3 and 6.

I do hope this has been helpful (eventually).
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Dec 2002 02:19
Strings are actually pointers to arrays of characters. When you put a string into a type, you in fact only put the pointer into the type. The actual characters of the string are not stored within the type.

If you use my utility dll, or MrTAToads , you can poke any byte value into your string (as long as it is not zero, and is not outside the string length). You can then peek or mid$ it back out when you need the results.

It would probably be safer to allocate memory instead though instead of stretching strings to do something they were not designed to do.
n3t3r453r
21
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Russia
Posted: 28th Dec 2002 07:52
People, it is posible to make function that lets you to use arrays in types!

For example command in type dim array_n(10) as type_n
Can be moved to other place by translator, then type will contain only pointers: *array_n1Ptr, *array_n2Ptr and ect.
And then you call type_n.array_n(1) it means that you call type_n.array_n1Ptr.

Simple, but useful. Lee Bamper, think of this question, please...
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 28th Dec 2002 16:32
if doing say



would be useful so you can have model formats fully contained, however as this would be memory in efficient having to hold that much memory its best to do it in a function
if you global the data within those array's it give you alot of leway (^_^)

Anata aru kowagaru no watashi!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 31st Dec 2002 06:44
Why we can't use arrays in user defined types?


umm please tell me the difference between what u mean and this code ok as I think its neat and possible using this method.





IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 31st Dec 2002 13:31
That's an array of types - perfectly OK. What they mean here is an array in a type

n3t3r453r
21
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Russia
Posted: 31st Dec 2002 15:16
@Indi:
In your code your create 1 monster as dim, but I mean that I must create for example 10 peoples and all of them must have 10 weapons, so 100 weapons at all. It's much more comfortable, then creating weapons outside the type.
Once more for example:
dim man(10) as man_t
man(1).weapon(1) = "minigun"
man(1).weapon(2) = "tompson"
man(2).weapon(1) = "machinegun"
man(2).weapon(2) = "colt"
Understand, i hope?
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 31st Dec 2002 17:27
well its more for a preset of something...
so say every instance of your monster you need the value to actually provide a SET array within it,

so say if you use the above then you access it like



now for each model(index) you will be able to store the ENTIRE data possible within that array point.

so each vertex would be



and that would completely write the vertex, and you can attribe it all to the same model rather than having several arrays externally needing to be named unique names each time.

if you need to then release the memory you should be able to go



and it would run through that array entry and undim all the arrays associated with the type
i mean there is really no point in it other than being able to make every self contained, which would put it that bit closer to actual structuring - this can actually help exporting means as well.
But the point here is really adding things which is only particularly useful to keeping code smaller and simpler, focusing more towards application development.

Anata aru kowagaru no watashi!

Login to post a reply

Server time is: 2024-05-18 08:36:43
Your offset time is: 2024-05-18 08:36:43