Search code examples
javarobocode

Robocode getName() requires variable?


So I want my Robot to behave differently when the scanned robot it called "RamFire", and I tried doing this:

public void onScannedRobot(ScannedRobotEvent e) {
    if (getName() = "RamFire") {
        // If the robot is called "RamFire" do this
    }
    else {
        // If not, do this
    }
}

But when I try to compile the code it says I made an error:

enter image description here

I'm new to Robocode and have never programmed with java so I have no idea what I'm doing wrong.


Solution

  • To compare values of variables you use ==
    To assign a value to a variable, you use =
    So in your case, you want to compare Values, so it should look like this:

    if (getName() == "RamFire")
    

    Or better yet, check the name via an equals call. Because strings can differ from each other while having the same content.

    new String("RamFire") == new String("RamFire") // returns false
    new String("RamFire").equals(new String("RamFire")) // returns true