gnomeyDew
gnomeyDew

Reputation: 1

C# - I need to figure out how to make GUI that listens for buttons being pressed in a IF statement

So currently, I am writing a small text adventure game in C# and I am using Visual Studio Express 2013. In the forms method, I have a button "buttonContinue" method that is called when I press the button; pretty simple. I also have a button "buttonA", "buttonB", "buttonC" which are the controls for the game.

Now. How the game is working currently is that you press the buttonContinue to progress through the game and when you press the continue button, you have a random chance of coming across an event such as coming across a monster, finding an NPC, or coming across an "event" (event is not important at the moment). As of now, buttonA and buttonB is being used to eat an apple that you might pick up during the game or to drink water that you might be carrying.

Here is the problem: I want to make buttonA and buttonB to function differently when in a battle or an event. I want buttonA to be a button to fight the monster, buttonB is to try to hide from the monster, buttonC is to make an attempt to run. As of now the only solution I'v come up with is to make a while LOOP that checks if buttonA, B, or C has been pressed when encountered a monster -BUT- the GUI does not PAUSE or LISTEN for buttonA, B, or C is being pressed as an IF statement.

Here is the small part of the code from my game:

Attributes.monsterDead = false;
    while (Attributes.monsterDead == false)
    {
        if (buttonAClicked == true)
        {
            Goblin.GoblinAttack();
            buttonAClicked = false;
        }
        if (buttonBClicked == true)
        {

        }
        if (buttonCClicked == true)
        {

        }
   }

Now where the WHILE statement is called, since there is nothing that waits and listens for these if statements are true, it just goes on a infinite loop with no time limit causing the program to completely freeze.

I am not very skilled in C# yet as I am still learning and taking class for it. Furthermore, this is my first post in this website. Please help me fix this problem, I can give you any more information if needed! Thank you!

EDIT: I decided to include the source-code so I can get this problem fixed faster.
Form1.cs - pastebin(DOT)com/idJaR20V
Goblin.cs - pastebin(DOT)com/Ti1L4XzN
Attrivutes.cs - pastebin(DOT)com/GfJAJ1u5

I'm still inexperienced with C# programming and it's functions but would it be possible to run both buttonContinue and buttonA method at the same time? Make it so buttonContinue is disabled when encountered a monster and have only buttonA, B, C have functions while the buttonContinue is still running? :/

If i were to initiate another method while a different method is running, wouldn't one of the method stop working and pop out all the data/progress it was in? I'm not too sure.

Upvotes: 0

Views: 127

Answers (1)

mybirthname
mybirthname

Reputation: 18127

Create 3 different buttons X, Y, Z. Put wanted functionality for battle in this 3 buttons in OnClick functionality.

When you are in event A, B, C visible. In this case X, Y, Z are not visible.

When you are in battle X, Y, Z visibile. In this case A, B, C are not.

Upvotes: 1

Related Questions