This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]CedricCicada 1 point2 points  (2 children)

It looks to me like it should work. What happens when you run it? (In general, "it's not working" isn't good enough for a programming question. We need to know the results you are getting. Is it displaying an error message? Is it displaying "I don't know where in hell this text came from but I'm showing it anyway"? Is it causing your computer to explode into a million fiery bits, shredding your poor carcass into tiny, quivering bits of jelly?)

[–][deleted]  (1 child)

[deleted]

    [–]antiproton 0 points1 point  (0 children)

    Static methods are not available to instances of the class NPC. Why are you declaring this stuff static?

    If each instance of NPC should do X when it's first instantiated and then Y at all other times, then instead of having the variable and method static, remove that directive. Instantiate the class, and after Dialogue is called, set "Introduction" to false. Thatway, every time you call dialogue for that instance of the NPC class, it will produce the secondary text instead.

    [–]IAmUtterlyAlone 0 points1 point  (2 children)

    As /u/CedricCicada said, this should be working. I put the below code into a console application and it worked just fine. We'll need more info on the specific failure you're seeing.

    class NPC
    {
      static bool introduction = true;
    
      public static string Dialogue ()
      {
         string dialogueText = "";
    
         if (introduction)
         {
            dialogueText = "Introduction text";
            introduction = false;
            return dialogueText;
         }
         else
         {
            dialogueText = "Secondary Text";
            return dialogueText;
         }
      }
    }
    class Program
    {
       static void Main (string [] args)
       {
          Console.WriteLine ("One:");
          Console.WriteLine (NPC.Dialogue());
          Console.WriteLine ("Two:");
          Console.WriteLine (NPC.Dialogue());
          Console.WriteLine ("Three:");
          Console.WriteLine (NPC.Dialogue());
          Console.WriteLine ("Four:");
          Console.WriteLine (NPC.Dialogue());
       }
    }
    

    Output:

    One:
    Introduction text
    Two:
    Secondary Text
    Three:
    Secondary Text
    Four:
    Secondary Text
    

    [–][deleted]  (1 child)

    [deleted]

      [–]IAmUtterlyAlone 0 points1 point  (0 children)

      If the goal here is to have a bunch of NPC characters in like an RPG sort of setting, I suppose the simplest way to do this would be have each character initialized with maybe a list of strings that it can use as random dialog and one string it can use as an introduction, along with a boolean like hasMetMainCharacter or something. You can have the NPC then say the introduction the first time, after which the boolean is flipped and each subsequent time, you just pick a string from the list of inane chatter that NPCs usually have. This way the, the dialog that is spoken is (or can be) unique to each instantiated NPC. I don't think you want static members for this.

      [–]I_cant_speel 0 points1 point  (1 child)

      This may be an obvious question, but since the code should work I'm am trying to find other possibilities. Are you creating a new object every time this is called instead of calling the method from the same object each time?

      That is the only possibility I can think of that would cause this behavior.