#lets make a thread and I'll explain
1 messages · Page 1 of 1 (latest)
ok to start a serialized POCO
[Serialize]
class Stat
now a Mono that uses that class
class StatObj : Monobehaviour
public Stat stat;
so the mono goes on your GO's and you can fill out the stats in Stat as normal
you with me so far?
ok so another GO with a singleton marked as DDOL
class StatManager : MonoBehaviour
public static StatManager instance
pulic Dictionary<string,Stat> stats
now when the GO's with a StatObj are instantiated in Awake of StatObj they can
if StatManager.instance.stats.ContainsKey(name) stat = StatManager.instance.stats[name];
else StatManager.instance.stats.Add(name,stat)
so the stats are either saved first time round or retrieved when the Go is instantiated again
obviously you dont have to use the GO name as the key, you can use anything as long as it is unique
so when you Destroy a Go with a StatObj the underlying Stat lives on
okay I think im getting it. so we have a dictionary of stats, those stats are made to not destory, then what when we instanciate it again it searches for the existing stat objects?
yes, exactly
and because Stat is a POCO it will not be affected by Destroy, that is the important bit
sorry had a work call
I was just about to check my scripts
okay if you dont mind getting into my specific case. so In my situation I have "public Dictionary<Vector3, Room> rooms = new();" where I have rooms that are instanciated via an asset with a room script. each room holds a player stat. so would I want a plane poco for the room script and serialize that?
so just get rid of monobehavior for this and serialize it?
im not following this statement
well, if you take Monobehaviour off Room then you have no way to attach it to a game object. So you need another monobehaviour to hold a reference to your room class
basically, yes
what you need to make sure is only have data in Room which can survive Destroy/Instantiate,
So you may need to have some of the data in Room and some in RoomObj
oh wait. okay so that class will just keep alive the required data rather tahn the whole object. i think im getting it a bit more
sorry doing a bit of googling to make sure I understand.
np, this is a very common construction so it's good to understand it completely
btw, using a Vector3 as a Dictionary key is very dodgy indeed
oh is it? thats just what I found to instanciate rooms in the scene via a manager script.is there aa better way to do this?
convert the Vector3 to Vector3Int and use that
is that so we dont have like float positions?
yes
now im trying to change all instances of vector3 lol as I contemplate and understand this
not all instances, just for the Dictionary
wait I think I might get it. so we have the actual object, and monobehvior, then we have the serialized room class that holds the data, size position or whatever we want. when the manager instantiates the room it its basically applying the data to the prefab? or maybe im way off
well, kinda.
The first time an object is instantaited it saves its Room class into the Dictionary, when the object is instantaited again it reads that data from the Dictionary replacing that from the prefab
I think im still thinking of this of needing to duplicate the object, but its just using the prefab as a blue print and the data from the dictionary tells it where to place it and how many?
no, that has nothing to do with it
yeah this is the part im kind of missing
that was not a part of your original question
i got the code up on git hub if that makes it easier. keep in mind its pretty hack together
what don't you understand about the structure as I have laid out above?
im just not seeing how the serialized data and the object interact
oh right. let me read that again and se if ti make smore sense
ah okay, then for this instead of "stat" I can apply it to "room"?
yes, of course
boom. Ill play with thank you so much.
np, good luck