Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk 3ds® Max® / MaxScript / Creating an array by executing a string?
  RSS 2.0 ATOM  

Creating an array by executing a string?
Rate this thread
 
46726
 
Permlink of this thread  
avatar
  • madguru
  • Posted: 30 August 2010 06:11 AM
  • Total Posts: 49
  • Joined: 11 November 2006 03:27 PM

Hi,

I have some arrays that are created depending on incoming data. That’s why I would like to create them as needed using strings to build the names and execute to create them.

obj_id=1
val_group_num
=2
o_buf_ary
=#()

 
str_command = ("if o_buf_ary_"+obj_id as string+"_"+val_group_num as string+".count == undefined do ( \n global o_buf_ary_" obj_id as string+"_"+val_group_num as string+"=#() \n o_buf_ary[o_buf_ary.count+1]=o_buf_ary_" obj_id as string+"_"+val_group_num as string+"\n)")
 
execute str_command

So first it checks to see if an array with that name exists. If not, it creates an empty array, then adds that new array to an existing array that stores all of these dynamically created arrays.

Problem is, I’m not sure the array is being created. Is there something wrong with this code?

Thanks,

Adnan



Replies: 0
avatar
  • Anubis
  • Posted: 30 August 2010 07:22 AM

-- str_command is:
if 
o_buf_ary_1_2.count == undefined do (
  global 
o_buf_ary_1_2 #()
  
o_buf_ary[o_buf_ary.count+1] o_buf_ary_1_2
)

-- 
it return error: -- Unknown property"count" in undefined
-- that comes from "if o_buf_ary_1_2.count ..."

-- without '.count' will works:
if 
o_buf_ary_1_2 == undefined do (
  global 
o_buf_ary_1_2 #()
  
o_buf_ary[o_buf_ary.count+1] o_buf_ary_1_2
)

o_buf_ary --> #(#())
o_buf_ary[1] == o_buf_ary_1_2 --> true


Max 9 through 2009, XP-Pro x64 SP2
ASUS EAH3450 Series (Driver 8.470).
Core 2 Duo E8400 3GHz, 4Gb Ram, DX9.0c.

Replies: 0
avatar
  • madguru
  • Posted: 31 August 2010 04:46 AM

Thanks for the reply Anubis!

I had tried it without .count also, without luck. It works if it is hardcoded without .count, but via strings within these loops it does not.

I ended u doing this to make it work:

-- create array if it does not exist
 str_command 
= ("if o_buf_ary_"+obj_id as string+"_"+val_group_num as string+" == undefined do ( \n global o_buf_ary_" obj_id as string+"_"+val_group_num as string+"=#() \n)")
 
execute str_command

 str_command 
= ("o_buf_ary_"+obj_id as string+"_"+val_group_num as string "[1+o_buf_ary_"+obj_id as string+"_"+val_group_num as string ".count] = point2 " w_pixel as string " " h_pixel as string)
 
execute str_command
 
 tmp_o_buf_ary[tmp_o_buf_ary
.count+1]=("o_buf_ary_" obj_id as string+"_"+val_group_num as string)
 
o_buf_ary=makeUniqueArray tmp_o_buf_ary
 sort o_buf_ary

I fed the name of the new array as a string into tmp_o_buf_ary, which I then removed duplicates from and sorted into o_buf_ary. I accessed these arrays by using execute (string).

-Adnan



Replies: 0