10 Replies - 1468 Views - Last Post: 19 October 2012 - 01:21 PM Rate Topic: -----

#1 Hedgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 12

Problem - Game Character Info Program

Posted 18 October 2012 - 11:15 PM

Hello, I am creating a program in C# that lists information for characters from a game. I am using Visual Studio 2012 to help with interface creations. I want the person using it to be able to type a name in a character's first or last name, and have it display information about the character.
What the program should do:

Gets a string from the user (a character's first/last name).

The string goes through a switch to set int i to # and string n to "[###]". (where # is, its replaced by actual numbers.)

The program displays an image depending on what "i" is set to (i + ".png").

The program reads a text file with the following information in it: (Embedded Resource)

Quote

[000]
Reimu Hakurei
Reimu's description here

[001]
Marisa Kirisame
Marisa's description here

[002]
Sanae Kochiya
Sanae's description here


The program then should search the text file for [###]. (whichever number n is set to after going through the switch)

Then it grabs the next line after the [###] (character's name) and inputs it to characterName

It then grabs the line after the character's name and inputs it in characterDesc.

Then it displays characterName and characterDesc.

What it does:
gets string from person using the program
then nothing when I hit enter/press the search button.

I did get the program to work fine when the information was in arrays but I don't want an array that has 130 slots available with over 100 words in each slot. (there is over 130 characters in the game series and I'm going to have all of them along with a menu for their theme songs eventually.)

I am just learning C# right now also, so I don't know very much C#. I am in Software Development classes in college but currently have only taken Java and C++, getting into C# next quarter. So if anyone helps with code, please comment stuff so I can understand it better.

My code so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace TouhouCharacters
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            characterName.Hide();
            characterImage.Hide();
            searchBox2.Hide();
            enterSearch2.Hide();
        }
        public String n;
        public int i = 0;
        public void getSearch(object sender, EventArgs e)
        {
            String search = searchBox.Text.ToLower();
            switch(search) {

                case "reimu":
                    n = "[000]";
                    i = 0;
                    return;
                case "hakurei":
                    n = "[000]";
                    i = 0;
                    return;

            }
            //Starts erroring here, getting the text file and reading it and searching through it.
            System.Reflection.Assembly textFile;
            textFile = System.Reflection.Assembly.GetExecutingAssembly();
            textFile.GetManifestResourceStream("example.txt"); //Using an embedded resource.
            string allRead = textFile.ReadToEnd(); //I got the .ReadToEnd() from 
                 //http://www.dreamincode.net/code/snippet1278.htm , and when I copy/paste it, ReadToEnd doesn't have
                 //a red line under it, but when I changed things around it starts erroring.
            string regMatch = n;
            if (Regex.IsMatch(allRead, regMatch))
            {
                characterName.Text = "\n" + textFile;
                System.Reflection.Assembly thisExe;
                thisExe = System.Reflection.Assembly.GetExecutingAssembly();
                System.IO.Stream cimage =
                thisExe.GetManifestResourceStream("TouhouCharacters." + i + ".png");
                characterImage.Image = Image.FromStream(cimage);
                searchBox.Hide();
                enterSearch.Hide();
                enterSearch2.Show();
                searchBox2.Show();
                characterImage.Show();
                characterName.Show();

            } //Errors ends here.
        }
        private void getSearch2(object sender, EventArgs e)
        {
            String search = searchBox2.Text.ToLower();
            switch (search)
            {

                case "reimu":
                    n = "[000]";
                    i = 0;
                    return;
                case "hakurei":
                    n = "[000]";
                    i = 0;
                    return;

            }
        }
                                    
        private void searchBox_TextChanged(object sender, EventArgs e)
        {
            this.AcceptButton = enterSearch;
        }

        private void searchBox2_TextChanged(object sender, EventArgs e)
        {
            this.AcceptButton = enterSearch2;
        }



    }
}



Can anyone please help with the searching a text file with embedded resources? Everything I do makes it error and have been looking through google for different solutions but none of them have worked. I also can't figure out how to make the charactersName get the next line of the text file for the character's name. Same with the description. I assume doing
characterName.Text = "\n" + textFile
will NOT work, I was testing that just to try everything I could think of.

Thank you for your time and any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem - Game Character Info Program

#2 foohoo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-August 07

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 01:21 AM

Hi Hedgen!

One thing I am thinking that could make this easier is to use an XML file to store your data rather than a text file. This will allow you to search and retrieve data easily. e.g.

<characterlist>
    <character>
        <name>Reimu Hakurei</name>
        <description>Description here</Description>
    </character>
...
</characterlist>



you can then use LINQ to XML to iterate through and search the data using the nodes. You can compare the text contained in the "Name" node to the text entered by the user and then display the "name" and "description" data in the GUI.

http://broadcast.ore...le-linq-to.html
OR!
http://www.dreaminco...79-linq-to-xml/

I hope that helps a bit!

This post has been edited by foohoo: 19 October 2012 - 01:27 AM

Was This Post Helpful? 2
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 01:56 AM

Agree with foohoo, and not just because of his Shinigami avatar. XML serialization of a collection of Character objects (from a class you've created) is 100% the way to go.

You currently have a switch statement, which looks like you're planning to manually code each case statement for all 130 characters. This is not the right way to go.

Also, you have a variable called textFile which is not a text file... it's actually a System.Reflection.Assembly object. That's confusing and bad practice.

If you absolutely must use a text file, use a FileStream object to convert it to/from memory.
Was This Post Helpful? 2
  • +
  • -

#4 Hedgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 12

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 07:05 AM

Alright, thanks for the reply guys, I'll look into that. :smile2:
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 07:07 AM

View PostMrShoes, on 19 October 2012 - 02:56 AM, said:

Agree with foohoo, and not just because of his Shinigami avatar. XML serialization of a collection of Character objects (from a class you've created) is 100% the way to go.


This tutorial should help with that.
Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 0
  • +
  • -

#6 Hedgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 12

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 08:21 AM

So if I am understanding this right, for what I am doing this would be the basic (plain) pseudo code with the xml:

-Get name (first or last) from user.
-search through the xml document first through the first names
- if textBox.text == fname //(fname is first name)
--var full_name = fullname where fname
--var desc = description where fname
-else
--search through the document through the last names
--if textBox.text == lname
---var full_name = fullname where fname
---var desc = description where fname
--else
---display "character not found"


Like I said, very basic pseudo code, I am just wondering if that would be the basic idea of doing it, or if that is a "worse" way. I could probably just do one if else statement using "if textBox.text == fname OR textBox.text == lname" so there would be less code.
Was This Post Helpful? 0
  • +
  • -

#7 Hedgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 12

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 08:30 AM

I ended up not finishing the pseudo code because I was thinking of 10 things at once, and I can't find the edit button.

-Get name (first or last) from user.
-search through the xml document through first and last names
- if textBox.text == fname OR textBox.text == lname//(fname is first name, lname is last name)
--var full_name = fullname where fname
--var desc = description where fname
--name.text = full_name
--descr.text = desc
-else
--display "character not found"


I added the lines:
--name.text = full_name
--descr.text = desc

which is placing the information into the text boxes I have.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 08:36 AM

Stop.

Textboxes aren't there to be your variable storage. Stop doing this BS of comparing directly to Textbox.text.

You should have an object that contains your player. It would have properties for things like
  • Name
  • Strength
  • List<object> for their inventory/bag of stuff
  • Health
  • Location
  • etc.


Then you are simply serializing/deserializing the player object to/from XML. You don't really search the XML for anything. You just deserialize it back into your object.

It looks like you are planing to use a single XML for the entire game structure. I hate this. Make a directory for the players then serialize each one to its own file. And maybe serialize the game state to a file for things like:
  • Game elapsed time
  • Next player
  • SaveFilesPath
  • etc.
  • Things that are specific to the game, not a player.


There is a real lack of Object Oriented thinking going on here. I think it would benefit you greatly to work on OOP concepts before you rush into building this. Work smarter not harder. A few hours of upgrading your thinking so you can do better architecture will save you countless hours and big aspirin bill for all the headaches.


Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#9 Hedgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 12

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 08:41 AM

View PosttlhIn`toq, on 19 October 2012 - 08:36 AM, said:

..

I'm not creating a game. I am creating a program for listing information of characters from a vertical shooter series. All it is is descriptions, their image, names, probably what their theme song is, and other misc information.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 08:50 AM

Ok. But that doesn't change the advice.

You should still do this in a more OOP approach and I really don't think you want to do it in one giant file. A database is probably the best approach. Second to that would be separate XML files.

That way all you have to do is deserialze each file into a player object that contains the properties you want: images, names song and so on.

If you try to do all that in one large file you risk corruption of the file loosing ALL the data instead of just one player. You also could have a GIANT file if there is lots of image data in it.

If you have a separate file for each player you can think of each file as the trading card for the player. Now it becomes easy to trade files, add them, delete them and so on.
Was This Post Helpful? 0
  • +
  • -

#11 Hedgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 12

Re: Problem - Game Character Info Program

Posted 19 October 2012 - 01:21 PM

I actually want to do it in one big file. I am going to be constantly backing up the code and files anyways. Also all of the images are going to be embedded resources and the code I have for the images works fine
(this is the code for grabbing the image)
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream cimage =
thisExe.GetManifestResourceStream("TouhouCharacters." + i + ".png");
characterImage.Image = Image.FromStream(cimage);

and i changes for each of the game's characters (first character i = 0, second i = 1, so on) and each image is less than 100KB
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1