How to bind an object to a node?

Hi,

 

I created a custom component with the .net addon and I’d like to bind some additional objects to each node of it. Is there any way to do that?

Hi,

To attach components to a parent node you can use ISimNode.AttachTo() method. So this snippet would attach comp2 to comp1:

ISimComponent comp1 = app.World.FindComponent("comp1");
ISimComponent comp2 = app.World.FindComponent("comp2");
comp2.RootNode.AttachTo(comp1.RootNode);

-k

Hi Keke,

Thanks for replying. But what I want is to add some data to a node.

for example:

ISimComponent comp2 = app.World.FindComponent("comp2");
comp2.RootNode.AddData(key, jsonData); //I'm finding some method like 'AddData' here

Well there are many ways you could store data in a node. You could use for example a property or a note behaviour. Note behaviour might be more suitable for large strings. Here’s a small snippet to create a string property and a note behaviour:

IProperty prop = comp1.CreateProperty(typeof(string), PropertyConstraintType.NotSpecified, "Data");
prop.Value = "Your data";

INote note = comp1.RootNode.CreateBehaviorByType(BehaviorType.Note) as INote;
note.Note = "Your data";

To access existing properies and note behaviours use ISimComponent.GetProperty() and ISimComponent.FindBehaviour() methods. IBehavior needs to be cast to INote type for accessing its contents on (INote.Note).

-k

Another way, if there is no need for data persistancy, is by using IApplication.SetSessionValue() method. In there you can store any object like, for example, a Dictionary which you can use to link some data to an ISimNode

Thanks Keke & ccamilo, your answers are really helpful.