views:

443

answers:

2

I am quite lost with where and how to store the *.txt level files in my game.

What I want in my game is the availability of a few "prebaked" levels that come installed with the game but also the ability for users to create their OWN levels.

So what I did is created a new directory inside visual studio in the "Content" called levels, and put a "level1.txt" there. But the only way I can access the level *.txt files is by using full path, and I cannot INCLUDE them in my visual studio solution because it does not build:

 "C:\My Documents\blahblabh....\levels\level1.txt"
  • Do I put these "prebaked" maps instead into some directory near the executable? How do I create a directory inside the /bin directory Before I compile the project?
  • How do I dissalow users from deleting the "prebaked" levels that come installed with the game?
    (I no longer need this)
  • Also, Where do I store the User Custom created levels? I assume that should be done with StorageDevice. But that ties in with the first question on how to create a directory after game is deployed.

Any pointers would be welcome! Thank you

EDIT: Re: custom content processor. This seems way more complicated then I need. But I need to deploy my game to xbox360 as well, so will the Storage Device work?

A: 

You should definitely put "prebaked" levels with the executable. I don't use VS, so I can't tell you how to do that.

There really is no way to prevent users from deleting your levels, but you could use a hash to verify that they haven't been modified (users can still get around this by modifying your executable, but that should stop most users).

You should put custom levels in the user's folder, like C:\Documents and Settings\User\MyGame\levels\level.txt.

Zifre
+3  A: 

You could write an custom content processor to handle your level files, but it is probably overkill. This has the advantage that you can "compile" the levels to some format that would be more difficult for a user to modify.

You can just add it to your project (not in Content) and specify "Copy to Output Directory - Copy always" in properties. Create a new subfolder in your project and place it there - that'll be the directory it is copied to.

For disallowing users deleting prebaked levels, no way on Windows to allow for that. You could do a check in your code that would cause it to fail if a user were to delete some expected files.

For creating a new directory, you can use the StorageDevice.OpenContainer().Path to get the root path and use Directory.CreateDirectory. This'll work on PC and XBox.

Michael
Writing a custom content processor isn't that hard and it enforces good practices and force you to write sane validation while speeding up your game at load time due to a much simpler read format. It also makes it much easier to port to X360 after the fact. In short, it takes slightly more efforts, potentially prevents lots of bugs, and is the accepted way to do it. :)
jfclavette
@jfclavette: It's probably overkill for this task, but it'd definitely be a good learning experience - XNA is very focused on the content pipeline and there is no better way to figure it out.
Michael
"As long as the levels are published under "Content", you should be able to use Game.Content.RootDirectory to locate the files and read them in".This does Not work. Because if I include my "levels" directory under content, it does not get copied for some reason into the /bin directory. Note that I cannot include my "level1.txt" inside the Content/levels folder into visual studio because it won't compile.
drozzy
@drozzy - right, there is no copy-only content importer.
Michael