Vain
Vain
NNormal
Created by Vain on 8/31/2024 in #🤝ask-a-question
Changing model data doesn't update the model for other client, am I missing something ?
Hello, I've started using the Realtime API around a week and a half ago with moderate success (players can move independently from others and the Realtime.Instantiate seems to work just fine), however I'm having issues when it comes to the fact of having a player interact with another. To break down the classes I have made and their links, I have : - Attributes, which stocks every variable needed by players, this uses [RealtimeModel] and has 37 [RealtimeProperty]'s - Entity that inherits from RealtimeComponent<Attributes>, methods that are worthy to note are "DealAutoDamage(Entity target)" and "ReceiveDamage(...)" (this method takes a bunch of parameters and I didn't want to list all of them (I'll post the code of those two at the end of the post) - Character that inherits from Entity and - among other stuff - offers a method that calls DealAutoDamage on a target (which is an Entity) I use Realtime.Instantiate in a different script that spawns players using the following line :
GameObject playerObject = Realtime.Instantiate(prefabName: "PlayerComponents", ownedByClient: true, preventOwnershipTakeover: true, useInstance: realtime);
GameObject playerObject = Realtime.Instantiate(prefabName: "PlayerComponents", ownedByClient: true, preventOwnershipTakeover: true, useInstance: realtime);
Now, characters also have a baseline health regen (which I set to 0 for the time being) that triggers every second. What I have remarked is that when one player (A) deals damage to another (B), the health of B goes down on his game and then is reset at its original value. On the player B game, the health of B never changed at all. I'm assuming that changes are made locally on player A game, but aren't actually updated on the datastore, then when the health regen happens, the update is sent by player B on the "new" (never actually changed) value on their game. I guess that might be related to the fact the player is owned by the client, but I tried removing that part and using realtime.clientID to know which character is owned by the player for the movement logic, changing [RealtimeModel] to [RealtimeModel(true)] and trying to both make RequestOwnership and model.RequestOwnership in the different methods that are concerned here, but nothing worked and the player's health kept not being in synch... My question is : how can I fix that so that when player A attacks player B, player B actually receives damage and the value is updated on all client ? Here is the different bits of code I'm using :
public bool ReceiveDamage(Entity hitter, float physDmg, float magDmg, float physPen, float magPen, float critChance, float critMult)
{
if (model.health <= 0) return false; // in case the entity receive damage after its death for whatever reason
// dégâts reçus = dégâts de base * (pen + (1-pen) * 100 / (def + 100))
float phys = physDmg * (physPen + (1 - physPen) * 100 / (model.physDef + 100));
model.health = Math.Max(0,
model.health - (critChance >= Random.Range(0, 1) ? phys * critMult : phys)
- magDmg * (magPen + (1 - magPen) * 100 / (model.magDef + 100))
);
if (hitter.GetType() == typeof(Character))
{
model.LastHitters.Push((Character) hitter);
}
return model.health <= 0;
}
public bool ReceiveDamage(Entity hitter, float physDmg, float magDmg, float physPen, float magPen, float critChance, float critMult)
{
if (model.health <= 0) return false; // in case the entity receive damage after its death for whatever reason
// dégâts reçus = dégâts de base * (pen + (1-pen) * 100 / (def + 100))
float phys = physDmg * (physPen + (1 - physPen) * 100 / (model.physDef + 100));
model.health = Math.Max(0,
model.health - (critChance >= Random.Range(0, 1) ? phys * critMult : phys)
- magDmg * (magPen + (1 - magPen) * 100 / (model.magDef + 100))
);
if (hitter.GetType() == typeof(Character))
{
model.LastHitters.Push((Character) hitter);
}
return model.health <= 0;
}
protected virtual bool DealAutoDamage(Entity target)
{
return target.ReceiveDamage(this, model.attack, 0, model.physPen, model.magPen, model.critChance, model.critMult);
}
protected virtual bool DealAutoDamage(Entity target)
{
return target.ReceiveDamage(this, model.attack, 0, model.physPen, model.magPen, model.critChance, model.critMult);
}
Any help provided would be greatly appreciated 😅
10 replies