许铮

学习 / 生活 / 工作 / 娱乐

How to create a class in MaxScript

转自:https://neiltech.wordpress.com/2012/11/16/how-to-create-a-class-in-maxscript/

November 16, 2012

For anyone who is masochistic enough to have used MaxScript you probably know that it doesn’t have classes. The following code snippits are the closest I have come to being able to create classes in external files. For some reason I’ve never been able to find examples like this when searching google so I figured I would put it here so that my future self will be able to find it in a pinch and hopefully other Technical Artists out there will find it useful as well.

Place the following code in a file called MyClass.ms:

struct MyClass
(
    public

    -- The constructor function that gets everything started.
    fn Constructor =
    (
        print ("The Constructor has been run")
        return true
    ),

    -- An example public function
    fn MyFunction =
    (
    ),

    initalized = Constructor(), -- This will be executed when this strut is created the first time.

    private

    -- An example private function
    fn MyPrivateFunction =
    (
    )

)

Calling it is also a little tricky to get correct. Put this code in a second file, save it to the same folder as MyClass.ms, and run it:

(
    clearListener()
    Global MyClass;     -- Inform MaxScript that this variable will eventually exist.
    fileIn "MyClass.ms" -- Read in the external class file
    data = MyClass()    -- Create a local instance of the class.

    print ("The class: " + data as string) -- Display the contents of the local instance of the class.
    ""
)

If you did everything correctly, when you run the second script you should see:

"The Constructor has been run"
"The class: (MyClass initalized:true)"
""

Happy Programming!

最近的文章

希腊字母的英语发音

维基百科上的希腊字符及发音.读数学符号用的上.虽然用希腊发音更好,但是为了方便交流,目前用英语发音比较流行.(这算是一种文化覆盖吧…)原文是繁体中文.以下是用KK音标?表示的希腊字母的英语发音,即希腊字母的古希腊语发音。在科学里,元音的发音会比较接近希腊语发音,如psi.的发音为/ˈpsiː/而并非/ˈsaɪ/。希腊语古希腊语名字[1]英语名字英语发音英式美式Α αἅλφαAlpha/ˈælfə/Β ββῆταBeta/ˈbiːtə//ˈbeɪtə/Γ γγάμμαGamma/ˈɡæmə/...…

笔记继续阅读
更早的文章

Godot日常QA

文章内容记录一些看到的知识点,免得以后用到了却找不到. Q:为啥别人的工程运行时会加载资源,我自己的工程不会加载,脚本里也没看到加载资源的代码? A:打开这个选项可以在运行时自动输出详细信息如资源的加载 来自:QQ群…

笔记继续阅读