Jump to content

On delete node or component event


photo

Recommended Posts

Здравствуйте! Есть ли где-нибудь событие или функция обратного вызова типа Update или Init, которое позволило бы узнать, что нода или компонент были удалены (например методом DeleteForce, DeleteLater или перезагрузкой мира)?

Link to comment

Hi, you can implement the shutdown method of a custom component, and then add this component to the node which destruction you want to monitor.

#pragma once
#include <UnigineComponentSystem.h>
#include <plugins/Unigine/IG/UnigineIG.h>
#include <plugins/Unigine/IG/UnigineIGNetwork.h>

/** 
* The sole purpose of this component is to have a callback called when this component is shutdown.
* This can be useful if you need to be alerted when a node is being destroyed.
*/
class ShutdownComponent :
	public Unigine::Plugins::IG::NetworkComponentBase
{
public:
	COMPONENT(ShutdownComponent, Unigine::Plugins::IG::NetworkComponentBase);
	COMPONENT_SHUTDOWN(shutdown);

	void shutdown();

	void setCallback(Unigine::CallbackBase* callback) { this->callback = callback; }
	Unigine::CallbackBase* getCallback() const { return callback; }

	PROP_NAME("ShutdownComponent");
	PROP_PARAM(String, message, "", "Message", "If not empty, logs this message when the component is shut down");

private:
	Unigine::CallbackBase* callback = nullptr;
};

 

#include "ShutdownComponent.h"

using namespace Unigine;

REGISTER_COMPONENT(ShutdownComponent)

void ShutdownComponent::shutdown()
{
	String msg = message.get();
	if (!msg.empty())
		Log::message("%s", msg.get());
	if (callback)
		callback->run();
}

 

Link to comment
×
×
  • Create New...