suriyun-production / battle-io-docs

This is document for Battle IO project (https://www.assetstore.unity3d.com/#!/content/101113?aid=1100lGeN)
https://suriyun-production.github.io/battle-io-docs/
1 stars 0 forks source link

attributes not working after lobby creator leave the game #60

Open Salja opened 1 year ago

Salja commented 1 year ago

Hello, i buyed BattleOI some time ago and now i played a bit around with, but i run into a issue

If you:

Create a new Lobby Join with 2 Player Press Join

If you Ingame then leave with the lobby creator the game after this put powerups and try to use attributes you can´t use the Attributes

Screenshot_1

rejoin with the old lobby creator to the game then the attributes works again, but this should be not happened if the master lobby creator leave a other ppl in the room should get the master and attributes should still works

i hope you can help me thank you very much

insthync commented 1 year ago

I will fix it.

Salja commented 1 year ago

thanks

Salja commented 1 year ago

will be nice if you can post the fix here if is possible already custom some stuff and i don´t want to update full project, but i tested the issue with a clean project and clean import and still same issue

Salja commented 1 year ago

maybe this will helps

https://youtu.be/fKEoV4eOmE8 https://youtu.be/H0aCqRtIo2w

insthync commented 1 year ago

Thank you it is helpful

Salja commented 1 year ago

I debug sice hours i don't know what is creating this bug i hope you have more luck about for me its really critical bug If all ppl have to leave If the lobby creator gets a disconnect or soemthing

insthync commented 1 year ago

Fix it by change AttributeAmounts to be a class

using ExitGames.Client.Photon;
using System.Collections.Generic;

[System.Serializable]
public class AttributeAmounts
{
    private Dictionary<int, short> attributeAmounts = new Dictionary<int, short>();
    public Dictionary<int, short> Dict { get { return attributeAmounts; } }

    private const int IntSize = sizeof(int);
    private const int ShortSize = sizeof(short);

    public AttributeAmounts Increase(int id, short value)
    {
        if (attributeAmounts.ContainsKey(id))
            attributeAmounts[id] = (short)(attributeAmounts[id] + value);
        else
            attributeAmounts.Add(id, value);
        return this;
    }

    public static byte[] SerializeMethod(object customobject)
    {
        AttributeAmounts data = (AttributeAmounts)customobject;
        short length = (short)(data.attributeAmounts == null ? 0 : data.attributeAmounts.Count);
        byte[] writeBytes = new byte[((IntSize + ShortSize) * length) + ShortSize];
        int index = 0;
        Protocol.Serialize(length, writeBytes, ref index);
        if (length > 0)
        {
            foreach (var attributeAmount in data.Dict)
            {
                Protocol.Serialize(attributeAmount.Key, writeBytes, ref index);
                Protocol.Serialize(attributeAmount.Value, writeBytes, ref index);
            }
        }
        return writeBytes;
    }

    public static object DeserializeMethod(byte[] readBytes)
    {
        AttributeAmounts data = new AttributeAmounts();
        Dictionary<int, short> attributeAmounts = new Dictionary<int, short>();
        int index = 0;
        int tempInt;
        short tempShort;
        Protocol.Deserialize(out tempShort, readBytes, ref index);
        short length = tempShort;
        if (length > 0)
        {
            Protocol.Deserialize(out tempInt, readBytes, ref index);
            Protocol.Deserialize(out tempShort, readBytes, ref index);
            attributeAmounts.Add(tempInt, tempShort);
            data.attributeAmounts = attributeAmounts;
        }
        return data;
    }
}

Then in SyncAttributeAmountsRpcComponent create in in Awake function

using Photon.Pun;

public class SyncAttributeAmountsRpcComponent : BaseSyncVarRpcComponent<AttributeAmounts>
{
    private CharacterEntity entity;
    protected override void Awake()
    {
        base.Awake();
        entity = GetComponent<CharacterEntity>();
        onValueChange.AddListener(OnValueChange);
        _value = new AttributeAmounts();
    }

Add variable null checking to CharacterEntity in SumAddStats property

    public virtual CharacterStats SumAddStats
    {
        get
        {
            if (refreshingSumAddStats)
            {
                var addStats = new CharacterStats();
                if (headData != null)
                    addStats += headData.stats;
                if (characterData != null)
                    addStats += characterData.stats;
                if (weaponData != null)
                    addStats += weaponData.stats;
                if (customEquipmentDict != null)
                {
                    foreach (var value in customEquipmentDict.Values)
                    {
                        addStats += value.stats;
                    }
                }
                if (AttributeAmounts != null)
                {
                    foreach (var kv in AttributeAmounts.Dict)
                    {
                        CharacterAttributes attribute;
                        if (GameplayManager.Singleton.Attributes.TryGetValue(kv.Key, out attribute))
                            addStats += attribute.stats * kv.Value;
                    }
                }
                if (appliedStatusEffects != null)
                {
                    foreach (var value in appliedStatusEffects.Values)
                        addStats += value.addStats;
                }
                sumAddStats = addStats;
                refreshingSumAddStats = false;
            }
            return sumAddStats;
        }
    }
insthync commented 1 year ago

And delete parameters from AttributeAmounts constructor calling,

Salja commented 1 year ago

Thank you very much you my hero =) works