suriyun-production / mmorpg-kit-docs

This is document for MMORPG KIT project (https://www.assetstore.unity3d.com/#!/content/110188?aid=1100lGeN)
https://suriyun-production.github.io/mmorpg-kit-docs
49 stars 11 forks source link

[Question]Building Scene Objects #2415

Closed moepi2k closed 4 months ago

moepi2k commented 5 months ago

I have a question regarding building stuff which are pre placed in scene like workbench. it works fine when its inherit from a buildingentity. but since my workbenchs are permanent and i wanna get rid of all the unneccesery stuff of syncfields like hp and so on i made a new class which inherit from basegameentity directly and only add the queuedworkbench part like

using System.Collections.Generic;
using UnityEngine;

namespace MultiplayerARPG
{
    public class QueuedWorkbenchEntity_SceneObject : BaseGameEntity, ICraftingQueueSource, IActivatableEntity, IHoldActivatableEntity
    {
        [Category(6, "Workbench Settings")]
        [SerializeField]
        protected ItemCraftFormula[] itemCraftFormulas = new ItemCraftFormula[0];
        public ItemCraftFormula[] ItemCraftFormulas { get { return itemCraftFormulas; } }
        [SerializeField]
        protected int maxQueueSize = 5;
        public int MaxQueueSize { get { return maxQueueSize; } }
        [SerializeField]
        [Tooltip("If this is > 0 it will limit distance to craft an items with this workbench entity by its value")]
        protected float craftingDistance = 5f;
        public float CraftingDistance { get { return craftingDistance; } }
        [SerializeField]
        [Tooltip("If it is public queue, it will be queued publically, shared with other players")]
        protected bool publicQueue = true;
        public bool PublicQueue { get { return publicQueue; } }

        [Category("Sync Fields")]
        [SerializeField]
        protected SyncListCraftingQueueItem queueItems = new SyncListCraftingQueueItem();

        private Dictionary<int, ItemCraftFormula> _cacheItemCraftFormulas;
        public Dictionary<int, ItemCraftFormula> CacheItemCraftFormulas
        {
            get
            {
                if (_cacheItemCraftFormulas == null)
                {
                    _cacheItemCraftFormulas = new Dictionary<int, ItemCraftFormula>();
                    foreach (ItemCraftFormula itemCraftFormula in itemCraftFormulas)
                    {
                        if (itemCraftFormula == null || itemCraftFormula.ItemCraft.CraftingItem == null)
                            continue;
                        _cacheItemCraftFormulas[itemCraftFormula.DataId] = itemCraftFormula;
                    }
                }
                return _cacheItemCraftFormulas;
            }
        }

        public SyncListCraftingQueueItem QueueItems
        {
            get { return queueItems; }
        }

        public bool CanCraft
        {
            get { return true; }
        }

        public float TimeCounter { get; set; }

        public int SourceId
        {
            get { return Identity.HashAssetId; }
        }

        public override void OnSetup()
        {
            base.OnSetup();
            queueItems.forOwnerOnly = false;
        }

        protected override void EntityUpdate()
        {
            base.EntityUpdate();
            if (IsServer && PublicQueue)
                this.UpdateQueue();
        }

        public override void PrepareRelatesData()
        {
            base.PrepareRelatesData();
            if (CacheItemCraftFormulas.Count > 0)
                GameInstance.AddItemCraftFormulas(SourceId, CacheItemCraftFormulas.Values);
        }

        public bool ShouldBeAttackTarget()
        {
            throw new System.NotImplementedException();
        }

        public bool ShouldNotActivateAfterFollowed()
        {
            throw new System.NotImplementedException();
        }

        public bool CanActivate()
        {
            return true;
        }

        public void OnActivate()
        {
            BaseUISceneGameplay.Singleton.ShowCraftingQueueItemsDialog(this);
        }

        public float GetActivatableDistance()
        {
            return GameInstance.Singleton.conversationDistance;
        }

        public bool ShouldClearTargetAfterActivated()
        {
            throw new System.NotImplementedException();
        }

        public bool CanHoldActivate()
        {
            return true;
        }

        public void OnHoldActivate()
        {
            BaseUISceneGameplay.Singleton.ShowCraftingQueueItemsDialog(this);
        }
    }
}

whith this i can interact with the workbench and the queued ui opens, but i dont get any formulas. formulas only loading when i inherit from buildingentity. can u lead me in the right way what i miss here?

thanks in advance

moepi2k commented 5 months ago

i found out that u need to have a scriptable setted up same like buildingentity and add to database like: https://gyazo.com/fcdc430984ff3d5d0ed7ab66f2e01bc3

when u dont do it the formulas never loaded. is it possible to do it without this extra step. i dont need all those building items for scene objects since the palyer are not able to build it anyway

insthync commented 4 months ago

You have to add formula data to game database, you may try to do it in awake function, that is it.

moepi2k commented 4 months ago

Ohh i see, call this in awake

public override void PrepareRelatesData()
        {
            base.PrepareRelatesData();
            if (CacheItemCraftFormulas.Count > 0)
                GameInstance.AddItemCraftFormulas(SourceId, CacheItemCraftFormulas.Values);
        }

Will check when at pc, thanks you

moepi2k commented 4 months ago

mh the PrepareRelatesData is already called by gameinstance. so it should load the formulas to db.

insthync commented 4 months ago

Did you try to debug and make sure that it was called?

moepi2k commented 4 months ago

oh yea it never gets called O.o so why?

i put it in awake now and its working

insthync commented 4 months ago

It is because not all game entity will be listed in game instance an call the function by game instance.