Jump to content

objects cast rules


photo

Recommended Posts

Hello

in uscript

I load skinned mesh and then it must be casted to NodeReference 

in documentation I found nothing about how to cast ObjectMeshSkinned to NodeReference - it is not obvious for me

ObjectMeshSkinned SM = new ObjectMeshSkinned("SM.mesh");
Node N_SM = SM;
NodeReference NR_SM = new NodeReference(????);

it will be very helpful if I can refer to some table with possible conversions between types - does that table/map/list exist?

thanks

 

Edited by lightmap
Link to comment

Hello lightmap!
If I get you right, you are trying to do something like this:

/* ... */

ObjectMeshSkinned mesh_skinned = new ObjectMeshSkinned("yourmeshname.mesh");

/* ... */

NodeReference nodref;

/* ... */

noderef = class_cast("NodeReference", mesh_skinned);

The class_cast() function performs conversions between pointers to related classes, not only upcasts (from pointer-to-derived to pointer-to-base), but also downcasts (from pointer-to-base to pointer-to-derived). No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe.

It is true that both NodeReference and ObjectMeshStatic classes have the same parent class - Node. But the fact that two classes A and B have the same base-type does not make them interchangeable; an A is always an A, and a B is always a B. So I'm afraid such casting cannot be performed.


A NodeReference is a node that refers to an external *.node file on the disk.

 The only thing you can do here is the following:
1) Create an ObjectMeshSkinned from the specified *.mesh file
2) Export it to my_node.node file
3) Create a NodeReference referring to the my_node.node file

Here is an example (UnigineScript) with an ObjectMeshStatic illustrating the idea :

#include <core/unigine.h>

int init() {

	// create a mesh
	Mesh mesh = new Mesh();
	mesh.addBoxSurface("box_0", vec3(1.0f));
	// save a mesh into a file on the disk
	mesh.save("unigine_project/meshes/my_mesh.mesh");
	// create an instance of any class inherited from the Node class (e.g. ObjectMeshStatic)
	ObjectMeshStatic object_mesh = new ObjectMeshStatic("unigine_project/meshes/my_mesh.mesh");
	// assign a material to the mesh
	object_mesh.setMaterial("mesh_base","*");
	// release script ownership
	node_remove(object_mesh);
	// pass node ownership to the editor by adding the node to it
	engine.editor.addNode(object_mesh);
	
	// export the node into a .node file
	engine.world.saveNode("unigine_project/nodes/my_node.node",object_mesh);
	// import the exported node to check the result
	NodeReference noderef = new NodeReference("unigine_project/nodes/my_node.node");
	// set a position of the node
	noderef.setPosition(Vec3(4.0f, 0.0f, 1.0f));
	
	return 1;
}

I hope this helps!

If you have any more questions, don't hesitate to ask!

Thanks!

  • Like 1
Link to comment
×
×
  • Create New...