Notifications
Clear all

[Closed] how to set mxs scruct member using SDK?

i want to pass a mxs sctruct value to a sdk function and set some of its member value. what i have now is:


Value* setStructMember_cf(Value** arg_list, int count)
{
	check_arg_count(setStructMember, 3, count);
	if (is_struct(arg_list[0]))
	{
		int index;
		Struct* str = (Struct*)arg_list[0];
		index = str->definition->get_member_value(arg_list[1])->to_int(); // find a key index
		if (index) str->member_data[index] = arg_list[2]; // set value
		return &ok;
	}
	return &undefined;
} 

but this is wrong for sure. does anyone know how to do it right?

4 Replies

some get and set struct stuff here

the long and the short of it would then be

def_visible_primitive(setStructMember, "setStructMember");

Value* setStructMember_cf(Value** arg_list, int count)
{
	check_arg_count(setStructMember, 3, count);

	Value* val = arg_list[0];
	if(is_struct(val))
	{
		Struct* mxs_struct = (Struct*)val;
		mxs_struct->_set_property(arg_list[1], arg_list[2]);
		return &ok;
	}
	return &undefined;
}

from mxs then

struct test (testfield, anotherfield)
obj = (test 10 10)
setStructMember obj #anotherfield [1,2,3]
obj

thanks! finally i change my function to using _set_property too. but the interesting story is my first function works but gives very weird result.
try this:


  struct ss (pos)
  s = ss()
  t = ss()
  setStructMember s #pos [1,1,1]
 s.pos
 -- [1,1,1]
 setStructMember t #pos [1,0,0]
 t.pos
 -- [1,0,0]
 s.pos
 -- [1,0,0]
 

it changes the s.pos value as well. but s and t are not instances! and it happens only for types point2, point3, and point4. for all other types (including arrays) it works right. do you have any explanation of this behavior?

something odd with going via the definition I guess as it is shared across the structures maybe it’s changing the actual “definition”.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

if it’s a bug – it’s pretty bad bug.