Jump to content

[SOLVED] ComponentStruct strange behaviour with PARAM String


photo

Recommended Posts

Hello,

I have a ComponentStruct embedded in a Component defined like this

class MyComponent :	public Unigine::Plugins::IG::NetworkComponentBase
{
public:
	COMPONENT(MyComponent, Unigine::Plugins::IG::NetworkComponentBase);
	COMPONENT_INIT(init);
	COMPONENT_SHUTDOWN(shutdown);
	void init();
	void shutdown();
  	PROP_NAME("MyComponent");
      
    struct MyType : public Unigine::ComponentStruct {
		PROP_PARAM(String, label, "", "Label");
    };
	PROP_ARRAY_STRUCT(MyType, mytypes);
};

And it is dynamically added at runtime with this code:

int prop_index = nodeMrf->addProperty("MyComponent");
auto prop_param = nodeMrf->getProperty(prop_index)->getParameterPtr();

prop_param->getChild("mytypes")->setArraySize(2);
for (int i = 0; i < 2; ++i) {
	auto p = prop_param->getChild("mytypes")->getChild(i);
	p->getChild("label")->setValueString("ok");
}

This works great... until I try to retrieve the "label" values.

for (int i = 0; i < v->mytypes.size(); ++i) { // v is a MyComponent*
	MyComponent::MyType const& t = v->mytypes[i];
	Log::message("1>  %s\n", t.label.getValueAsString().get());
	Log::message("2>  %s\n", t.label.get());
	Log::message("3>  %s\n", t.label.getValueAsString().get());
}

This outputs:

1>  
2>  ok
3>  ok
1>  
2>  ok
3>  ok

getValueAsString() returns nothing, unless I first call get().

What is going on? Am I miss-using the API?

Edited by Amerio.Stephane
Link to comment

PropertyParameterString::getValueAsString does not update the value, this method is needed for intern serialization. 
I don’t know why it is placed in public methods, it probably needs to be hidden.

To work you can use the conversion operator

//PROPP_PARAM(String, str_param)
//PROPP_PARAM(Int, int_param)

String strvalue = str_param;
int ivalue = int_param;

or the get() method in cases where the compiler cannot call this operator properly, for example:

Log::message("print: %d %s\n", ivalue.get(), strvalue.get());


also you can add new component like this:

	MyComponent * c = ComponentSystem::get()->addComponent<MyComponent>(nodeMrf);
	c->mytypes.resize(2);
	for (int i = 0; i < 2; i++)
		c->mytypes[i]->label = "ok";

 

  • Like 1
Link to comment

Okay, I'll remove all usage of getValueAsString(). Better to mark it private, indeed.

Thanks for the cleaner way to add the Component too :)

Link to comment
  • silent changed the title to [SOLVED] ComponentStruct strange behaviour with PARAM String
×
×
  • Create New...