Hack Forums
[C#] Check string in text document and set correct texture accordingly XNA - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Check string in text document and set correct texture accordingly XNA (/showthread.php?tid=5573807)



Check string in text document and set correct texture accordingly XNA - Hogtalare1 - Mar 13, 2017

We're currently working on a school project where we are going to make a very simple little game, but here is my problem:

I currently have a text document in which I write coordinates where I want to place a certain sprite using "streamreader" formatted like this: CoordinateX:CoordinateY:texturename.
I have made another classfile called "level" with methods with get and set functions for each value.

Code
while ((k = blockposition.ReadLine()) != null)
            {
                string[] database = k.Split(':');
                tilestemppos.X = float.Parse(database[1]);
                tilestemppos.Y = float.Parse(database[0]);
                tilestexturess = database[2];
                tilesposlist.Add(new level(tilestemppos.X, tilestemppos.Y,tilestexturess));
            }

Here it adds the strings and floats from the text document into the list "List<level> tilesposlist = new List<level>();".

Code
for (int i = 0; i < tilesposlist.Count; i++)
            {
                tiles_pos.X = tilesposlist.ElementAt(i).blockpositionX;
                tiles_pos.Y = tilesposlist.ElementAt(i).blockpositionY;
                spriteBatch.Draw(tiles, tiles_pos, Color.White);
            }

Here it draws the "tiles" sprite which is a Texture2d using the correct coordinates.

Now I want the program to check the "tilestexturess" in the text document and change the "tiles" texture to the correct texture from all the textures I have.

I was thinking of having another list where I put all textures and then make it check if the names match. How do I do this in the most simplest form?


* - ShadowCloudGH - Mar 13, 2017

I would probably be looking to create struct and slap a dictionary of the struct against a texture name. I am presuming that the same x and y would never map to different textures in the same context?

So end up with:

var levelTextures = new Dictionary<myStruct, textureName>();

With a large amount of cross referencing, dictionary are probably going to be your best bet. If you need to update these on a high ratio you'd probably want to investigate some SlimLock functions to prevent reading out of date info. If you have a pretty finite list of textures, consider using an Enum to map the textures. It's just much more elegant to reference than direct string comparisons.

While this might not give you a copy and paste answer, I figure this would point you in the right direction.


* - stratex - Mar 14, 2017

You seem to have two posts? I think you've made a mistake where tiletemppos.X is taking the Y value and vice versa.


* - Hogtalare1 - Mar 16, 2017

Thank you guys. Solved it by doing two different text documents, one for coordinates and one for sprites. It all works fine now :D