I am building this cafe simulator kind of game. And when it comes to saving the current state of the scene(to server) ie the positions of each new object example :a new fridge a stove etc. What would be a good way of saving the properties of each of this new bought items and their positions.
FYI using firebase real-time db.
Any thoughts? Thanks in advance.
There’s like at least ten or twenty billion Youtube tutorials on loading / saving game state.
But if you want, here’s some more Load/Save steps:
Don’t use a binary serializer: it’s insecure and it makes debugging very difficult
Thanks for the reply, I was planning on doing the iteration model. Wanted to know of there was an easier method or something.
One more thing of you don’t mind… I was thinking about coming up with a system to only update the changed values to the db each time I’m saving the updated scene. Is going through all that trouble to save a bit data worth the time? Considering all I will be updating are a few vector positions and few ints?
I certainly cannot answer this. If the quantities represented thousands of real dollars, then perhaps yes. If they represent in-game currency that the user can get for free, perhaps not. Your call.
The way I’ve gone about saving the position/rotation/scale of an object is by creating a TransformValues
struct that groups these values together, which can be saved/loaded to a file or database. Then I’ve created some Transform extension methods to convert between Transform
and TransformValues
:
[Serializable]
public struct TransformValues {
[SerializeField] private Vector3 position;
[SerializeField] private Vector3 rotation;
[SerializeField] private Vector3 scale;
public TransformValues(Vector3 position, Vector3 rotation, Vector3 scale) {
this.position = position;
this.rotation = rotation;
this.scale = scale;
}
public TransformValues(Vector3 position, Quaternion rotation, Vector3 scale) {
this.position = position;
this.rotation = rotation.eulerAngles;
this.scale = scale;
}
public TransformValues(Transform transform, bool localValues = false) {
if(localValues) {
this.position = transform.localPosition;
this.rotation = transform.localEulerAngles;
}
else {
this.position = transform.position;
this.rotation = transform.eulerAngles;
}
this.scale = transform.localScale;
}
public void Apply(Transform transform) {
transform.position = position;
transform.rotation = Quaternion.Euler(rotation);
transform.localScale = scale;
}
public void ApplyLocal(Transform transform) {
transform.localPosition = position;
transform.localRotation = Quaternion.Euler(rotation);
transform.localScale = scale;
}
public Vector3 Position => position;
public Vector3 Rotation => rotation;
public Vector3 Scale => scale;
}
public static class TransformExtensions {
public static TransformValues ToValues(this Transform transform) => new TransformValues(transform);
public static TransformValues ToLocalValues(this Transform transform) => new TransformValues(transform, true);
public static void FromValues(this Transform transform, TransformValues values) => values.Apply(transform);
public static void FromLocalValues(this Transform transform, TransformValues values) => values.ApplyLocal(transform);
}
Then saving/loading a GameObject’s position/rotation/scale would be done like so:
public class SomeClass : MonoBehaviour {
//Save the position/rotation/scale of this object and send it to a database or wherever.
public TransformValues SaveState() => this.transform.ToValues();
//Re-apply the saved values pulled from the database or wherever.
public void LoadState(TransformValues state) => state.Apply(this.transform);
}
Wow thanks mate. This is really helpful.