構造体のメンバ(変数と関数)を定義するときは、修飾子 public
および private
を使用して、ユーザおよびデバッガに対するメンバの可視性を制御できます。
Public
メンバに対しては、外部コードからアクセスすることができます。修飾子を指定しない場合は、メンバは既定値で public であるとみなされます。3ds Max 2010 よりも前のリリースでは、すべてのメンバが常に public でした。
Private
メンバには、構造体インスタンス内からのみアクセスできます。private メンバにアクセスを試みると、ランタイム エラーが発生します。構造体を暗号化されたスクリプト内に定義すると、メンバ値はデバッガやエラー ダンプには表示されません。
サポートされる 2 つのイベント ハンドラである on create do
および on clone do
は、構造体定義の public セクションで定義された場合でも、必ず自動的に private になります。
例:
struct foo2 ( public A = 1, B = 3, fn error = throw "ZZZ", private C = 77, fn errorPrivate = throw "ZZZ", on create do format "in create: %\n" this, on clone do format "in clone: %\n" this ) f = foo2() --creating calls the 'on create do()' handler:
--> #Struct:foo2( --> error:<fn>; Public, --> c:<data>; Private, --> b:<data>; Public, --> errorPrivate:<fn>; Private, --> a:<data>; Public) --> undefined --> in create: (foo2 a:1 b:3) --> (foo2 a:1 b:3) --> OK
f = foo2 a:1 b:3 c:4 --passing three parameters fails:
-- Runtime error: Attempting to access private member: "c" in: #Struct:foo2( -- b:<data>; Public, -- errorPrivate:<fn>; Private, -- c:<data>; Private, -- error:<fn>; Public, -- a:<data>; Public)
f = foo2 a:10 b:30 --passing two parameters works:
in create: (foo2 a:10 b:30) (foo2 a:10 b:30)
f = foo2 10 30 --passing two arguments works:
in create: (foo2 a:10 b:30) (foo2 a:10 b:30)
f = foo2 1 3 4 --trying to pass three arguments fails:
-- Runtime error: Attempting to access private member: "c" in: #Struct:foo2( -- b:<data>; Public, -- errorPrivate:<fn>; Private, -- c:<data>; Private, -- error:<fn>; Public, -- a:<data>; Public)
f.errorPrivate() --calling the private error() function fails:
-- Runtime error: Attempting to access private member: "errorPrivate" in: (foo2 a:10 b:30)
f.error() --calling the public error() function works:
-- Frame: -- Runtime error: ZZZ
f2 = copy f --creating a copy of the existing struct
in clone: (foo2 a:10 b:30) (foo2 a:10 b:30)
showProperties f --the method prints the type of the members
a:<data>; Public b:<data>; Public error:<fn>; Public errorPrivate:<fn>; Private c:<data>; Private true