PDA

View Full Version : String Methods


vasquez888
09-02-2006, 12:21 PM
Hi,

I'm wondering if there is a String class with predefined methods like charAt(), indexOf(), etc etc with Maxscript or do I have to write them myself?

Thanks

davestewart
09-02-2006, 11:53 PM
MaxScript isn't object-orientated in it's syntax like other languages. The functions are global in scope (like PHP), and in this case the equivilent functions you'd want would be:

str = "hello world"

str.charAt(4) // o
str[5] -- o

str.indexOf('o') // 4
findstring str "o" -- 3


If you wanted to write a class, the maxscript equivilent would be to write a struct, and contain the functions within.

str = "hello world"

struct StringFn
(
fn indexOf str char = (return findstring str char),
fn charAt str index = (return str[index])
)

StringFn.indexOf str "o"
StringFn.charAt str 5


Hopefully that illustrates some of the differences.
Cheers,
Dave

vasquez888
09-03-2006, 03:31 AM
Thanks so much Dave! That really helped clear a lot! =)

davestewart
09-03-2006, 11:07 AM
No problem! Out of interest, what's your background? What are you hoping to achieve in max?

vasquez888
09-03-2006, 12:52 PM
Hi Dave,

I studied Computer Science but I'm now working in the architectural visualisation field. I was hoping to look into Maxscripting and write some basic tools to speed up and automate some processes in our workflow. Hopefully it'll get to a stage where the scripts will be helpful to everyone in our studio. =)

davestewart
09-03-2006, 02:29 PM
MaxScript 100% rocks! You'll have bags of fun. Enjoy!

vasquez888
09-03-2006, 05:38 PM
Thanks Dave! It is rewarding when you see your script working. By the way, you have an impressive website and portfolio!

In the meantime I have a question about structs and evaluating an expression.

Say I have the following:

struct aCollection (name, count)
bottleCollection = aCollection "wineBottles" 10

property = count

print bottleCollection.property


How could I get it to evaluate the last line to read as bottleCollection.count instead of trying to find the member "property"?

Thanks

davestewart
09-03-2006, 05:50 PM
You could do using the following methods:

property = "count"

-- method 1
val = getProperty bottleCollection property

-- method 2
execute ("val = bottleCollection." +property)


Firstly, define your property count as a string, otherwise it will return the undefined value of an undefined variable count

Then, the first method is the best in this situation. getProperty will return a propety from most objects in max - spheres, controllers, structs, etc. For example:


getProperty $sphere01 "radius" -- the radius of $sphere01
getproperty $[1].modifiers[1] #angle -- the angle of the first modifier in the first object in a selection of 2 or more objects


The second method executes max code, similar to eval() in JavaScript. However, the function executes in, and returns any variables in, global scope. It's also a slow function, but sometimes the only way to get a job done.

Cheers the for the nice words!
Dave

vasquez888
09-03-2006, 05:59 PM
Thanks once again Dave! I'm surprised at how quick you replied and your answers have been of great help. Thanks once again! =)

davestewart
09-03-2006, 06:19 PM
I'm having a geeky weekend-in programming... shhh!!

CGTalk Moderation
09-03-2006, 06:19 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.