#lets make a thread and I'll explain

1 messages · Page 1 of 1 (latest)

rancid aurora
#

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?

coarse sundial
#

okay yeah I think so

#

oh yeah okay i get it

rancid aurora
#

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

coarse sundial
#

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?

rancid aurora
#

yes, exactly

#

and because Stat is a POCO it will not be affected by Destroy, that is the important bit

coarse sundial
#

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?

rancid aurora
#

yes

#

so a POCO Room and a Monobehaviour RoomObj which consumes Room

coarse sundial
#

so just get rid of monobehavior for this and serialize it?

coarse sundial
rancid aurora
#

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

rancid aurora
#

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

coarse sundial
#

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.

rancid aurora
#

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

coarse sundial
#

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?

rancid aurora
#

convert the Vector3 to Vector3Int and use that

coarse sundial
#

is that so we dont have like float positions?

rancid aurora
#

yes

coarse sundial
# rancid aurora yes

now im trying to change all instances of vector3 lol as I contemplate and understand this

rancid aurora
#

not all instances, just for the Dictionary

coarse sundial
#

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

rancid aurora
#

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

coarse sundial
#

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?

rancid aurora
#

no, that has nothing to do with it

coarse sundial
#

yeah this is the part im kind of missing

rancid aurora
#

that was not a part of your original question

coarse sundial
#

i got the code up on git hub if that makes it easier. keep in mind its pretty hack together

rancid aurora
#

what don't you understand about the structure as I have laid out above?

coarse sundial
rancid aurora
coarse sundial
#

oh right. let me read that again and se if ti make smore sense

coarse sundial
rancid aurora
#

yes, of course

coarse sundial
#

boom. Ill play with thank you so much.

rancid aurora
#

np, good luck