Reading Data of other items.

Options
MaliciouSin
edited August 2012 in Photon Server
Since photon is multithreaded, what is the best way to go about reading data of another item while avoiding issues. Example, lets say an MmoActor has a HP variable, how would I go about reading that variable. Obviously I can get a reference from the World.ItemCache, but it is it safe to read it directly?

Comments

  • this question is not a photon related question but rather a general threading question :)

    Reading data doesn't need to be synced between threads, its safe to read most variables from any threads. however, if you are writing the data and reading it back in then you need to sync access to the var somehow.

    Since its hp your talking about, it will change frequently so u def need to sync access across other threads. solution1: u can either write data to the variable atomically(Interlocked.Exchange) this will make the writing inside a single call so you are safe to read it whenever you want no matter what thread you're in. solution2: since you will be sending this info to the client you may have a collection which stores all the properties(hp, mp, etc) then all you have to do is to use a lock statement whenever you change the value inside the collection(pref a hashtable since it has thread-safe methods) then when u read it you'd lock on the same SyncRoot and read the value back.