0
votes

I'm wondering if there is a simple way to store the path of a texture in a string. I'm wondering this because I'm using a save system for a game where I need to store the path of a texture in a file to later load it. I've tried using Texture2D.Name but that just returns "" or null. If I can't get the full path that's okay, but at the very least I'd like to have the name of the texture.

EDIT: I think I explained it a bit bad or I don't understand the answers properly, I'll give explaining a new try.

I've made a script that will save the Position, HP etc for an "Enemy" class. This then gets stored in a .txt file on the \My Documents folder. Once I want to load the information back in I create a new instance of an Enemy(Vector2, int). Then I load the information and use an int.Parse to convert my string to an int > new Enemy(new Vector2(int.Parse(string1), int.Parse(string2)), int.Parse(string3)) <

What I then want is to somehow save the name of the texture to later load it back in using a similar method mentioned above. (Retrieve it from a .txt as a string)

4
Why not build a texture manager, and store the texture path when you load the texture in the manager. - Ben
Are you loading the texture dynamically from disk, or is it part of your content project? - 3Dave
It's part of the content - PeppeJ

4 Answers

1
votes

A Texture2D is strictly an in-memory object, there is no relation between a Texture2D and a file (except that a Texture2D can be loaded from and saved to files). If you want to remember the file name you loaded it from, you should hold on to the file name when you load the texture.

EDIT In response to your comment:

Assuming you're not talking about a screenshot, you'll need to do something like this:

FileStream fs = new FileStream("myFile.png", FileMode.OpenOrCreate);
myTexture.SaveAsPng(fs, myTexture.Width, myTexture.Height);
fs.Flush();
1
votes

Most objects don't have usable names, and even if they would it may not necessary to be safe to be used as file name.

Unless you have good reason to expect good name on you object and actually use it, I'd recommend simply Guid.NewGuid().toString() as file name - safe and unique.

0
votes

The ContentManager has a RootDirectory property that identifies the folder where content manager content is stored. The path is relative to the executable folder, so you can build a path to the content using that, along with the name you specify when loading the content.

For example, if you load a .PNG texture like this:

Texture2D texture = Content.Load<Texture2D>("SomeTexture")

Then the path to that texture could be built like this:

string path = Content.RootDirectory + Path.DirectorySeparator + "SomeTexture" + ".png"

Note that you need some knowledge of the file type in order to know what the extension is. This is also assuming that you've not change the name in the content item properties, so the key you use in the Load call matches the file name, which it does by default.

If you store your content in subfolders then you just append those as well to build up the full path.

If you need the full disk path, rather than a relative one, then you can get the executable path and prepend that to Content.RootDirectory. Google is your friend in that regard.

0
votes

If your game is content project based, the easiest is to use the path relatives to the content project path root.

In your Content Project:

the relative path to file is "Textures/Animated/Rambo"

In your Script:

 TextureAsset = "Textures/Animated/Rambo";

In your Engine

 Texture = Content.Load<Texture2D>( "Textures/Animated/Rambo");     

 //Or if you don't want to load through Content

 Texture = Texture2D.LoadFromFile(Path.Combine(Content.RootDirectory, "Textures/Animated/Rambo")); 

If you want to save I recommend you proceed in the same way, and force to save in the content project folder.