I can get items by name, index, update items, etc., and do things like...

...printing:
arr.print() -- unsorted

five: 5
four: 4
three: 3
two: 2
one: 1

...sorting:
arr.sort() -- by name

five: 5
four: 4
one: 1
three: 3
two: 2

arr.sort field:#value -- by value

one: 1
two: 2
three: 3
four: 4
five: 5

However, it then occurred to me that I might be able to just use a dotNet class for this, and save having to load in a script each time.

I don't really know anything about dotNet though. Is this possible?
How would I go about accessing and creating a Dictionary / Collection, then add items to it, sort it, etc?

Thanks for the input,
Dave

magicm

06-06-2008, 01:04 AM

Hi Dave,

AFAIK, there's no way to use .net for this, at least not with maxscript. There is System.Collections.Hashtable which allows you to store key/value pairs, but as it is a hashtable, it's not sortable. Same goes for System.Collections.Specialized.StringDictionary, which also uses a hash table to store its data.

Martijn

davestewart

06-06-2008, 01:13 AM

Hi Martijn,

Thanks fpr that. Sorting isn't a massive prioroty, it's just a "nice to have", and will probably come in useful at some point.

I did some tinkering with HashTable earlier, after a bit of Googling, but it didn't seem to reveal any useful-looking methods in max.

Do you know how would I go about using the HashTable dotNet class?

Cheers,
Dave

magicm

06-06-2008, 01:22 AM

Well, the annoying thing about using this class in mxs, is that it only accepts dotNetObjects as keys/values. I suppose you could work around this by writing a wrapper function which automatically converts the value. Not sure if this is worth the trouble though.

The other class I mentioned does accept mxs values, but only strings. So that's probably not an option.

I could happily use a 2-part struct to store items, but I'm going with a couple of internal arrays at the mo. I guess it's slightly cleaner (definition-wise), and still provides the functionality I need.

I'll post when done and you can pick it apart anyway.

Cheers for the input,
Dave

davestewart

06-06-2008, 08:17 PM

Whadya know, I went with an array of structs in the end.

Even though it's not as clean, it's definitely more intuitive to think of name value pairs than an array of names and an array of values. Thanks Paul.

"List" makes easy to store and retrieve arbitrary values.

Properties

<array> items <name> <value> - the key/value pairs.

Names can be a #name, "string", or even an index
Values can be any MaxWrapper value (i.e. anything)

Setters

<ListItem> addItem <name> <value> - adds an item to the List, and if it already exists
<ListItem> setItem <name> <value> - synonym for the above
Getters

<value> getValue <name> - returns the value of the named item
<index> getIndex <name> - returns the index of the named item
<name> getName <value> - returns the name of the first item that matches the supplied value
<ListItem> getItem <name> - returns the List item corresponding to the supplied name (typically, you wouldn't use this, as you know the name component already, it's just included for completeness)
<array> getItems() - returns all items as an array of ListItem structs
<array> getNames() - returns all names as an array
<array> getValues() - returns all values as an array
Clear or delete

<array> clear() - clears the lit of all items, and returns the empty ListItems array
<boolean> deleteItem <name> - deletes the named item from the list, and returns it
<boolean> deleteIndex <index> - deletes the item at the index, and returns it
Utilities

<array> sort field:<name> order:<name> func:<function> - sorts the list in a variety of ways, even supply a comparison function (see the max help on qsort)
<string> print() - prints the List items to the Listener