r/Unity3D•
-coolguy

Spawning objects at a certain time from a txt file

Question

How would I go about interpreting a .txt file which holds an object to be spawned at a specific time?
For example the list of objects in the text file would be in the format of:
time,propertyA,propertyB,propertyC,propertyD

How would I assign these properties to the object and have it spawn at a certain time(in ms)?
So far I can read the file text, but I'm unsure how to derive anything from it.

I haven't found any reliable tutorials on spawning objects at a specific time, with specific properties, using a text file.

Posted Mar 11, 2019, 4:43 AM UTC

17
igrushaLTD•

Text file is not a very good meta date format.

Try use XML file.

Use serialization to create the necessary objects.

​

Example Enemys list :

public class Enemys

{

public List<Enemy> EnemyList = new List<Enemy>();

}

public class Enemy

{

	[XmlAttribute]

public int id = 0;

[XmlAttribute]

public int armor = 0;

[XmlAttribute]

public int health = 0;

[XmlAttribute]

public int speed = 0;

}

Example Enemy XML :

<?xml version="1.0" encoding="utf-8"?> <Enemys > <EnemyList> <Enemy id="0" armor="100" health="100" speed="1"/> <Enemy id="1" armor="100" health="100" speed="1" /> </EnemyList> </Enemys>

​

Load to Game:

XmlSerializer xs = new XmlSerializer(typeof(Enemys));

Stream readFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

Enemys enemys = (Enemys)xs.Deserialize(readFileStream);

readFileStream.Close();

​

​

​

3
-coolguyOP•

The file I'm trying to read is not xml.

1
PhillSerrazina•

They're saying that you should probably use XML instead of a txt file. And I agree. I personally prefer Json, but to each their own.

Mind if I ask why you're trying to do in txt format?

2
-coolguyOP•

Because I'm reading from a specific file format

1
Siduron•

I'm not exactly sure in what context you would want to spawn objects this way, but I think a good way of reaching your goal is to take a look at the Timeline feature of Unity. I only recently started to use it myself and it's very useful for anything related to timed sequences.

If this is not what you're looking for, then just hardcode the data in a script instead of reading a file unless a critical feature relies on a file.

1
-coolguyOP•

The issue is it requires this SPECIFIC file format. No alterations can be made to the format. Data MUST be read from this file format.

It is not of my design.

1
Siduron•

Alright, in that case you could start out by creating a custom class that can hold these properties . While reading the lines of the file, create an instance of that class and assign the properties. Then add the object to a Dictionary with the specified time as the key.

Now you got a Dictionary with the keys as a kind of 'timeline' and the values hold an object with the details for spawning an object.

You can then use a coroutine with a while loop and some kind of check on the elapsed time to create instance of your objects. Use the data from the Dictionary value to manipulate any components of the object and you're done.

This is pretty straight forward and generic, so I don't know in what way this can help your project. Good luck!

1