MXS Dictionary

The implementation below is outdated, if you need a pure-maxscript dictionary I would like to redirect you to my newer approach that is hosted on GitHub:


A very simple pure mxs implementation of a dictionary to store key-value pairs. The code is optimized to be as short as possible (so it can be included in a script file) , which is why it is not very readable 🙂

It is somewhat “fake” in that it is not using any hashing but an internal array of tuples. Which means the bigger it gets the slower it will be to work with. So its usage is probably quite limited when used with lots of items.

The big advantage to using a dotNet HashTable however is, that you can use anything as a value, not just strings and numbers. So you can create mappings of teapots, animation controllers, materials, whatever you need.

Example usage:

-- Either copy/paste into your script or bundle the file with your main script file and use
fileIn ""
-- or
include ""

-- Available methods are shown in these examples:

d = dict()
d.add "Peter" 34
d.add "Zacharias" 29
d.add "Christian" 12
d.add "Hank" 56

--#(#("Peter", 34), #("Zacharias", 29), #("Christian", 12), #("Hank", 56))
--#("Peter", "Zacharias", "Christian", "Hank")
--#(34, 29, 12, 56)

a = d.sortByKey()
--#(#("Christian", 12), #("Hank", 56), #("Peter", 34), #("Zacharias", 29))
b = d.sortByValue()
--#(#("Christian", 12), #("Zacharias", 29), #("Peter", 34), #("Hank", 56))

d.add "George" "McDolan"
b = d.sortByValue()
--"SortException: Dict values must be of same comparable type to sort"

d.hasKey "Hank"
d.get "Hank"

i = d.pop "Hank"
--#("Hank", 56)
--#(#("Peter", 34), #("Zacharias", 29), #("Christian", 12))

[download id=704]

If you use this script, be awesome and donate a few bucks!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.