Jump to content

[SOLVED] How to get TextObject already in scene


photo

Recommended Posts

Hi,
Anyone can help me on how to get a textobject that is already on scene, for example selecting by NodeName?
The goal is to carefully place different textobject around the scene, and access them by script at runtime to change value.

I found on forum different example on how to create them from the scratch, but no example how to select existing one.

Example creating from scratch:

nigine::ObjectTextPtr text = Unigine::ObjectText::create();
text->setFontName("font.TTF");
text->setText(u8"Text changed by script");
text->setFontSize(40);
text->setRotation(Unigine::Math::quat(90.0, 0, 0.0f));
text->setPosition(Unigine::Math::vec3(0.0f, 0.0f, 1.0f));

I would like to do something like:
 

Unigine::ObjectTextPtr text = World::getNodeByName("ObjectNameInScene");
text->setText(u8"Text changed by script");

But of course they are different type.

Thanks



 

Edited by andrea.padovani
Link to comment

Dear @andrea.padovani,

Example from the documentation

#include <UnigineWorld.h>
using namespace Unigine;
/* .. */

// find a pointer to node by a given name
NodePtr baseptr = World::getNodeByName("my_meshdynamic");

// cast a pointer-to-derived from pointer-to-base with automatic type checking
ObjectMeshDynamicPtr derivedptr = checked_ptr_cast<ObjectMeshDynamic>(baseptr);

// static cast (pointer-to-derived from pointer-to-base)
ObjectMeshDynamicPtr derivedptr = static_ptr_cast<ObjectMeshDynamic>(World::getNodeByName("my_meshdynamic"));

// upcast to the pointer to the Object class which is a base class for ObjectMeshDynamic
ObjectPtr object = derivedptr;

// upcast to the pointer to the Node class which is a base class for all scene objects
NodePtr node = derivedptr;

Simple type casting will save the day.

Unigine::ObjectTextPtr textPtr = static_ptr_cast<Unigine::ObjectText>(World::getNodeByName("ObjectNameInScene"));
  
  if( textPtr != nullptr)
  {
  //Perform other actions on valid pointer.
  }

It is a standard practice to check pointer after casts and perform ops.

Rohit

  • Thanks 2
Link to comment
  • silent changed the title to [SOLVED] How to get TextObject already in scene
×
×
  • Create New...