How do I save continuous data points of my program ?

Hello all,

I am just a beginner in unity. I am creating a game ( A ball moving around in circle along x and z axis with y- axis being constant). I want to record every points of x, y and z as the circle begins to move (Or when I start the Play button). How do I continuously store the values as the x, y and z position changes every second ? I want to store their (x, y and z) position for every second.

Please I really need help.

Hi there! You want to make a list of Vectors, and store the ball’s position every frame.

private List<Vector3> ballPositions = new List<Vector3>();

public void Update(){
    ballPositions.Add(transform.position);
}

Bear in mind that the longer you play, the bigger this list is going to get, so you might encounter memory issues. You need to have this code in a script on the Ball itself as “transform” references the transform of the GameObject the script is attached to.

If you want to take it further, try using a Queue to store the positions instead of a list, and remove elements from the Queue to ensure the list doesn’t get too big.

You could also just store the positions every second as you said in the question, in that case you’ll have to make a timer so that you only store the position at fixed intervals- something like this will do fine.

private float timeElapsed;
private List<Vector3> ballPositions = new List<Vector3>();

public void Update(){
    timeElapsed += Time.deltaTime;
    if(timeElapsed >= 1){
        ballPositions.Add(transform.position);
        timeElapsed = 0;
    }
}

Adding to what @Shemamforash said, you could try implementing the same using Coroutine and yield return new WaitForSeconds(1) .

It would be something like this:
private List ballPositions = new List();

void Start(){
	StartCoroutine(SavePos())
}

IEnumerator SavePos(){
	ballPositions.Add(transform.position);
	yield return new WaitForSeconds(1);
}

Refer Coroutine, WaitForSeconds and WaitForSecondsRealtime for more details.

Also, in case you are going to save it for long hours, considering saving them in a file instead, Say whenever the List size exceeds a threshold (like 100), and remove those from the list.

Hey,

Thank you for such a quick reply and providing the solution @Shemamforash. I hope you could help me in one more question.

I have created this script to make the ball (Or Player component) to move circular in the axes. This script is attached to the ball (Or Player component).

The solution that you mentioned: Should it be placed in same Update() function of the same script or do I need different script for that ? Also, how to make the x, y, z and time counter variables located in this Player Controller script be accessed to another script (If I need to create another) ?

Thank you in advance. :slight_smile:

Umm, i see many answers but not one actually discuss the problem of memory.
Be it RAM or persistent (DB, HDD…), you only need to play for a few hours straight to occupy most of memory in mid-grade PCs. It’s even worse for anything else.

The correct way to do this is to register only changes in inputs. For example, you register the ball being spawned, so you know the initial state. Then you register a player input, then the lack of it (button down/button up). This will dramatically decrease the rate up updates, the save size, and with a timestamp will allow you to retrace player’s steps.

Edit : before anyone mentions, for objects that do not receive input, like a ball rolling around in a circle at same velocity, you only need to record when it began rolling, everything else can easily be calculated from that.