Jump to content

Can't make NetworkComponent work (master/slave)


photo

Recommended Posts

Hi, I'm trying to move our network master/slave sync code to the IG::NetworkComponentBase architecture as explained here

https://developer.unigine.com/en/docs/2.16.1/ig/custom_component?#network_ig_component

but I can't make it to work. For example, based on the documentation, this code should replicate "value" from master to slave, but the slave always prints "-1", proving the data never reach it. I have no warnings/errors related to this in the logs.

#pragma once

#include <UnigineComponentSystem.h>
#include <plugins/UnigineIGNetwork.h>

class NetComponent : public Unigine::Plugins::IG::NetworkComponentBase
{
public:
	NET_COMPONENT_DEFINE(NetComponent, Unigine::Plugins::IG::NetworkComponentBase);
	COMPONENT_INIT(init);
	COMPONENT_UPDATE(update);

private:
	void init();
	void update();

	NET_DEFINE_SLOT(setValue);
	void setValue(int value);
	int value = 0;
};

 

#include "NetComponent.h"

#include <UnigineGame.h>
#include <UnigineVisualizer.h>

NET_REGISTER_COMPONENT(NetComponent);

using namespace Unigine;

void NetComponent::init()
{
	value = 0;

	IG_ONLY_FOR_SLAVE;
	value = -1;
}

void NetComponent::update()
{
	Log::message("%s=%d\n",
		Unigine::Plugins::IG::Manager::get()->isMaster() ? "master":"slave",
		value);

	IG_ONLY_FOR_MASTER;
	setValue(Game::getFrame());
}

void NetComponent::setValue(int value)
{
	this->value = value;
	NET_CALL(setValue, value);
}

At compilation, VS2019 gives me this warning:

1>NetComponent.cpp
1>I:\wkspaces216\AH_UNG_DEV\include\plugins\UnigineIGNetwork.h(376,2): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
1>I:\wkspaces216\AH_UNG_DEV\source\AH.Core\NetComponent.h(9): message : see reference to function template instantiation 'void Unigine::Plugins::IG::Sender::init<NetComponent,int,int>(T *,int,int)' being compiled
1>        with
1>        [
1>            T=NetComponent
1>        ]
1>I:\wkspaces216\AH_UNG_DEV\include\plugins\UnigineIGNetwork.h(291,1): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
1>I:\wkspaces216\AH_UNG_DEV\source\AH.Core\NetComponent.cpp(6): message : see reference to function template instantiation 'void Unigine::Plugins::IG::NetworkRegistrator::registrateNetworkComponent<NetComponent>(void)' being compiled

What am I obviously missing here?

Link to comment

Hello! 

I copied your code, ran it and everything works correctly.

image.png

the master sends data and it reaches the slave correctly. 

 

Right now all conponents inside IG work by this way.

 

please describe in more detail if there is anything else in your project because I used an empty 2.16.1 project without any modifications. At the moment, only the problem with the order of the components is known. If the component was moved on the same node after initialization (property_num was changed), then there may be a delivery problem. 

Link to comment

The NetComponent is compiled in a DLL, which is then loaded as a plugin with Unigine. We do this for multiple components, because it allows us to easily separate code development and allows to enable/distribute features without rebuilding the whole application.

Indeed, when NetComponent is build along side the main app code, it does work. Though the documentation forgets to mention that you need a new line:

    IG::NetworkRegistrator::get()->init();

So, is it possible to have a network component in a DLL, or should be rework all of our code?

Link to comment

Yes, i got it. 

is it a usual shared library with your own logic or engine plugin?

theoretically yes you can put it in a DLL or engine plugin but you have to also call IG::NetworkRegistrator::get()->init(); inside each library in initialization stage. 

I will run some tests today on move the component to the engine plugin - if I find any problems, I will add to the answer.

Link to comment

yes, i tested it in few plugins, and it works! 

image.png

i make two component each was builded in separete plugin. and start application with both plugins:
```
-extern_plugin "IG,IGTEST,IGTEST2"
```

ig.zip

  • Like 1
  • Thanks 1
Link to comment
On 3/17/2023 at 8:30 PM, Amerio.Stephane said:

What about the compilation warning? Am I doing anything wrong, or should it be fixed on your side?

it is harmless warning about type convertation size_t to int on our side. it shouldn't cause any problems, we'll mute it.

Link to comment
×
×
  • Create New...