Server Client bullet spawning

#1
I'm working with FPS template, and I can't make bullets spawn correctly on the dedicated server.
It looks like this is some kind of animation issue on the server since bullet spawn exactly where the player entity is located and not where player's rifle should be.
Image
Can you guys point me how to track/fix this issue?
Code for RMIs

Code: Select all

bool CPlayerComponent::ServerFire(RMI_NoParams && p, INetChannel* channel)
{
//TODO: Change fire method, so it won't depend on attachment socket.

Vec3 playerPos = GetEntity()->GetWorldPos();
CryLog("Char entity pos: x%f y%f z%f", playerPos.x, playerPos.y, playerPos.z);

if (ICharacterInstance* pCharacter = m_pAnimationComponent->GetCharacter())
{
IAttachment* pBarrelOutAttachment = pCharacter->GetIAttachmentManager()->GetInterfaceByName("barrel_out");

if (pBarrelOutAttachment != nullptr)
{
const QuatTS bulletOrigin = pBarrelOutAttachment->GetAttWorldAbsolute();

SEntitySpawnParams spawnParams;
spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->GetDefaultClass();
spawnParams.sName = "Bullet";
spawnParams.vPosition = bulletOrigin.t;
spawnParams.qRotation = bulletOrigin.q;

CryLog("Bullet spawn: x%f y%f z%f", bulletOrigin.t.x, bulletOrigin.t.y, bulletOrigin.t.z);

const float bulletScale = 0.05f;
spawnParams.vScale = Vec3(bulletScale);

// Spawn the entity
if (IEntity* pEntity = gEnv->pEntitySystem->SpawnEntity(spawnParams))
{
// See Bullet.cpp, bullet is propelled in the rotation and position the entity was spawned with
auto spawnComponent = pEntity->CreateComponentClass<CBulletComponent>();
spawnComponent->AddIgnoredEntity(GetEntityId());
}
}
}

SRmi<RMI_WRAP(&CPlayerComponent::ClientFire)>::InvokeOnAllClients(this,ShootParams{GetEntityId()});
return true;
}

bool CPlayerComponent::ClientFire(ShootParams&& p, INetChannel*)
{
Vec3 playerPos = GetEntity()->GetWorldPos();
CryLog("Char entity pos: x%f y%f z%f", playerPos.x, playerPos.y, playerPos.z);

if (ICharacterInstance* pCharacter = m_pAnimationComponent->GetCharacter())
{
auto* pBarrelOutAttachment = pCharacter->GetIAttachmentManager()->GetInterfaceByName("barrel_out");

if (pBarrelOutAttachment != nullptr)
{
const QuatTS bulletOrigin = pBarrelOutAttachment->GetAttWorldAbsolute();

SEntitySpawnParams spawnParams;
spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->GetDefaultClass();
spawnParams.sName = "Bullet";
spawnParams.vPosition = bulletOrigin.t;
spawnParams.qRotation = bulletOrigin.q;

CryLog("Bullet spawn: x%f y%f z%f", bulletOrigin.t.x, bulletOrigin.t.y, bulletOrigin.t.z);

const float bulletScale = 0.05f;
spawnParams.vScale = Vec3(bulletScale);

// Spawn the entity
if (IEntity* pEntity = gEnv->pEntitySystem->SpawnEntity(spawnParams))
{
// See Bullet.cpp, bullet is propelled in the rotation and position the entity was spawned with
pEntity->CreateComponentClass<CBulletComponent>();
}
}
}
return true;
}

Re: Server Client bullet spawning

#3
serverFire is defined as

Code: Select all

bool ServerFire(RMI_NoParams&& p, INetChannel *);
in Player.h file of standart template.
And I'm calling it from player.cpp action like this.
Basically, I was following that tutorial, and now I'm trying to extend template by myself.

Code: Select all

// Register the shoot action
m_pInputComponent->RegisterAction("player", "shoot", [this](int activationMode, float value)
{
// Only fire on press, not release
if (activationMode == eIS_Pressed)
{
SRmi<RMI_WRAP(&CPlayerComponent::ServerFire)>::InvokeOnServer(this,RMI_NoParams{});
}
});

// Bind the shoot action to left mouse click
m_pInputComponent->BindAction("player", "shoot", eAID_KeyboardMouse, EKeyId::eKI_Mouse1);

Re: Server Client bullet spawning

#4
so you tell server spawn bullet and server tell all client to spawn bullet ?
that is wrong!. you should use spawn networking related bullet. then all client will spawn bullet automaticly. and position and rotation synced with server.

https://docs.cryengine.com/display/CEPROG/Networking

Entities are by default not networked, but can be bound to the network during spawning - if a component calls INetEntity::BindToNetwork before IEntitySystem::InitEntity is called on the entity.

https://docs.cryengine.com/display/CPP/SRmi
do you register ?
ServerJumpRMI::Register(this, attachmentType, isServerCall, reliability);

and

https://docs.cryengine.com/display/CPP/ ... 784d05eb9a
Last edited by mknmknmknjk on Fri Oct 26, 2018 4:20 am, edited 1 time in total.

Re: Server Client bullet spawning

#6
Thanks for the assistance! I thought, that i can save a bit bandwidth with such spawning. I'll try to rework bullet logic as you said. But my main problem is that server spawns bullet somewhere in the player's feet, instead of rifle interface. All rmis registered.
Also thanks for the latest links, i just were searching for the Entity replication for 'building' functionality! :D

Re: Server Client bullet spawning

#7
So I removed ClientFire and edited ServerFire, now it spawns network replicated bullet. That is really handy! But it still spawns at the wrong place on the server. :(

Code: Select all

bool CPlayerComponent::ServerFire(RMI_NoParams && p, INetChannel* channel)
{
CRY_ASSERT(gEnv->bServer);

const Vec3 playerPos = GetEntity()->GetWorldPos();
CryLog("Char entity pos: x%f y%f z%f", playerPos.x, playerPos.y, playerPos.z);

if (ICharacterInstance* pCharacter = m_pAnimationComponent->GetCharacter())
{
IAttachment* pBarrelOutAttachment = pCharacter->GetIAttachmentManager()->GetInterfaceByName("barrel_out");

if (pBarrelOutAttachment != nullptr)
{
const QuatTS bulletOrigin = pBarrelOutAttachment->GetAttWorldAbsolute();

SEntitySpawnParams spawnParams;
spawnParams.pClass = gEnv->pEntitySystem->GetClassRegistry()->GetDefaultClass();
spawnParams.sName = "Bullet";
spawnParams.vPosition = bulletOrigin.t;
spawnParams.qRotation = bulletOrigin.q;

CryLog("Bullet spawn: x%f y%f z%f", bulletOrigin.t.x, bulletOrigin.t.y, bulletOrigin.t.z);

const float bulletScale = 0.05f;
spawnParams.vScale = Vec3(bulletScale);

// Spawn the entity
if (IEntity* pEntity = gEnv->pEntitySystem->SpawnEntity(spawnParams, false))
{
pEntity->GetNetEntity()->BindToNetwork();
auto spawnComponent = pEntity->CreateComponentClass<CBulletComponent>();
//spawnComponent->SetPosRot(bulletOrigin.t, bulletOrigin.q);
gEnv->pEntitySystem->InitEntity(pEntity, spawnParams);
}
}
}

return true;
}

Who is online

Users browsing this forum: No registered users and 1 guest