Notifications
Clear all

[Closed] Using THIS keyword in MAXScript

In most of the languages I use, you can always use the this keyword to refer to the current object within its methods. Overall in MAXScript you never use this kind of writing. But there are times that you need to use it–like if you are calling a non-method function that expects to get an object (and you are wanting to pass THIS current object):


function myfunction expectedObject = (
 format "Do something with my confusion? %
" (expectedObject.confused)
)
 
struct myObject  (
 
confused = true,
 compat = "unknown",
 
  function passMe = (
 
	 myfunction this
 
 )
 
)
 
question = myObject()
question.passMe()

My question resolves around the fact that the last time I used any code like this, users of older versions of Max could not execute the code. It was failing when the “this” keyword was used. I am also confused because the MAXScript docs do not mention this as a reserved keyword.

I cannot test these as my license for Max no longer goes beyond Max 2010. I need to know:

  1. Does it in fact work for older version of Max?
  2. If not, what is the most efficient way to do this without having to recreate a new object and fill in all its properties with those in the current object?
4 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

1.0) i might be wrong but this using in structures was added in version max8 (or max9).
2.0) if you know the global name of the structure instance you can pass it as a function argument.
2.5) you can make internal property of the structure, set it to the structure itself, and pass it to a function :


  (
  	struct asThis 
  	(
  		_this,
  		title = "Hey!",
  		fn whatIsThis = format "%
" _this.title 
  	)
  	test = asThis()
  	test._this = test
  	test.whatIsThis()
  )
  

or you can pass the structure definition:


  (
  	struct asThis 
  	(
  		_this,
  		title = "Hey!",
  		fn makeThis = _this() 
  	)
  	test = asThis()
  	test._this = asThis
  	test.makeThis()
  )
  

Thanks Denis. That seems like a good simple solution. By brain has been on vacation for a couple weeks and doesn’t want to solve any problems on its own

i envy you… my brain doesn’t want to work because I haven’t been on vacation for a long time.

Well that’s no good for anyone!

Incidentally, I’m taking my family on our first family vacation in over half a decade this weekend. The beach and sand castles with the kids. Can’t wait. Of course, after a few days I’m sure I will be itching to fire up Max