Notifications
Clear all

[Closed] Are dynamic structs possible?

are dynamic structs possible?

What I’d like to be able to do is dynamically add properties to a struct to house unanticipated data on the fly.


struct dynamicStruct(
	
	fn AddPropertyToSelf propertyName =( 

		--??? how would I create this function?

	),
	property1,
	property2

)


9 Replies
1 Reply
(@denist)
Joined: 10 months ago

Posts: 0

hmm… it sounds like you have no idea what structure, class, or object is?

I’m a bit familiar with classes & objects and in principle I understand them if not in application. I spent most of my previous job scripting Maya/ python to deal with json data.

Max script to structs are new to me. The examples in Max script help are too simplified to be useful. I have gathered form various threads that structs are more useful than the help files explain. I don’t quite understand what distinguishes a struct form a class. I understand both hold properties and methods, but beyond that I am unsure.

I experimented and found that you can set the property of a struct to another struct, so I know some degree of recursion is possible.

If the data I am reading in were static in structure a simple struct would suffice.
but unfortunately it is not : the number of properties and even the names of the properties coming in are not up to me. So a single , discreet and explicit set of struct properties would not suffice to hold all the varients. Thus I am seeking a dynamic approach.

I don’t know if Denis will give you a better approach but you might look at “execute” command in order to create your dynamic struct.

 j83

In languages such as C++, structs and classes are exactly the same, except for in C++, structs default to public, classes default to private.

For your current task, I would suggest approaching this in a different manner. The only thing I can think of that even begins to remotely support what you proposed is perhaps templates as implemented in C++. This is a form of meta-programming, where classes definitions are based on their input types (replacing the class “T” or “Type” with the provided class/type) and also only the methods used are generated at compile time.

In Maxscript, there is no such notion, and even its implementation of structs perhaps leaves a bit to be desired. However, perhaps you could store some type of array, and append certain things to it.

Perhaps each possible type of property you could store could be encapsulated in another struct, and these are then stored in an array. Then you could iterate through these arrays, checking for certain types, and reacting based on those.

I’m unfamiliar with the context in which you’re working, so please disregard if the notion of a container such as an array would not provide help.

Either way, I think perhaps you may want to go back to the drawing board how you might want to solve this problem.

I was pondering feranti’s idea.

Instead of trying to make a dynamic struct, I’d generate a struct definition as a string, adding properties to the string as new unexpected data comes in.


structString = "struct myStruct ( "


 --and as each new data propertycome in:

propertyString= "someNewPropertyName"
structString += propertyString+","

--and finally:

structString += ")"
execute(structString)

of course then I’d need to iterate over the struct properties and add values…
It might work but seems a bit like an inelegant work-around.

arrays could do the trick, but I’m dealing with json data so it would be nice to be able to grab a value by a property name instead of array indexes.

of course the real challenge comes when I hit nested json objects, so the above code would need to foster recursion…

1 Reply
(@denist)
Joined: 10 months ago

Posts: 0

do i understand you correct? you want to convert JSON object properties to mxs structure. JSON property usually is name and value. the value might be an object, and in this case we have to use a recursion. correct?
i don’t think that structure is a good solution. you will get a lot of unique struct definitions with unclear content.
i would do it a different way. i would define a simple Struct… let’s say:


  struct JSON_Prop (name, value) 
  

where value can be another JSON_Prop or an array of JSON_Props… and i would provide some search, find, and sort methods to work with this data.

DenisT-
thats very close to the JSON.NET approach, actually.

I think it might be possible via the sdk where struct “members” are stored in a hash table for index look up with an associated value array. But as Denis says there are far far easier ways of approaching this.

thiskinda shows what you would be dealing with.

I agree with Denis on this. Look at bseach array function to look for props faster in your array.