Archive 17/01/2023.

Multithreading and Events

Elendil

I am calling function from secondary thread without any problem until I saw this on console:

ERROR: Sending events is only supported from the main thread

this is function called from second thread

void Player_LightOnOff(bool OnTrue)
{
	if (Urho3D::Player::Player_Instance != nullptr)
	{
		Urho3D::Player::Player_Instance->LightOnOff(OnTrue);
	}
}


void Urho3D::Player::LightOnOff(bool OnTrue)
{
	if (OnTrue)
	{
		m_light->SetEnabled(true);
	}
	else
	{
		m_light->SetEnabled(false);
	}
}

code is working I have no crashes, but should I call it really from main thread?

Modanung

Are you using SendEvent(StringHash) somewhere?

Elendil

No, as I remember and from quick check, and if yes, I am 100% sure that not from second thread.

EDIT: No I am not use SendEvent

weitjong

If the m_light is an object instance derived from Urho3D::Component class then that would explain it. The Component::SetEnabled() method will send an event before it returns, but in your case instead of sending the event, the engine simply logging it as a programming error and bailing out.

I think you should move that logic to the main thread if you would like to take advantage of Urho3D event handling, specifically the E_COMPONENTENABLEDCHANGED event of your light component.

Elendil

Thanks, m_light is Urho3D component and your answer describe it what cause that error.