-2

I am on a project to make a text-based RPG game using thread. What I am wondering is, I created a monster class that contains an attack-player method. I have been trying to make a monster to attack player(which is me) automatically with thread. it does attack automatically though, but it does not actually affect the player's status(player's hp etc). I am trying to put the player object as a parameter of auto attack method(which is run method), but I am not really sure what I thought is the right way. I would like to know to set player as a parameter of the run method so that the attack method can affect player's status!

here is the code. attack method works well, but run method does not affect player's status. I probably am able to figure out what the problem is, but what should I do to put the put the player as the parameter so that run method is able to actually affect the player. Thank you!

public void run() {

    while (!stop) {

        try {

            attack(player);

            sleep(2000);

        } catch (Exception e) {
            return;
        }
    }
}

public void attack(Player player) {

    damage = att - player.defence;

    if (att < player.defence) {

        player.hp -= 0;

    } else player.hp -= damage;
}
Chanyoung
  • 1
  • 3
  • You can't make something a parameter of the run function. Run cannot have parameters. You can make it a parameter of the constructor, or make a setter on the thread that sets a member variable of the thread. But without code we can't help you further, or tell you if there's a better way to achieve this. – Gabe Sechan Sep 11 '22 at 17:23
  • you can't add parameters to run(). I'm pretty sure there is a better way to do it, can you provide more code please? idk you could remove this post and make a better one or edit this one – Crih.exe Sep 11 '22 at 17:42
  • 1
    And you don't want to extend Thread. Instead, implement Runnable or Callable, pass your data into the object's constructor parameter and pass it into your Thread. – DontKnowMuchBut Getting Better Sep 11 '22 at 17:52
  • 1
    Unless you want your game to take action in the background while awaiting user input, you probably should not be using threads at all. – VGR Sep 11 '22 at 18:01
  • 1
    @K.Nicholas, It's not smart to extend the `Thread` and override it's `run()` method. Much smarter to declare a `class Monster implements Runnable` with a constructor that takes the desired args, and then create a `new Thread(new Monster(...))`. – Solomon Slow Sep 11 '22 at 19:41
  • There is already a question asking this https://stackoverflow.com/q/877096/217324. In your case i suspect this isn't going to lead to good results, because thread context switching is out of your control and possibly not helpful for your purposes. – Nathan Hughes Sep 12 '22 at 02:16

1 Answers1

-1

you have not given the entire code. I am assuming your code to be liked below :

class Test implements Runnable{
 volatile boolean stop=false;
public void setStop(boolean stopParams){ this.stop=stopParams;}
public void run() {

    while (!stop) {

        try {

            attack(player);

            sleep(2000);

        } catch (Exception e) {
            return;
        }
    }
}

public void attack(Player player) {

    damage = att - player.defence;

    if (att < player.defence) {

        player.hp -= 0;

    } else player.hp -= damage;
}
}

Souvik Dey
  • 39
  • 3