1

I was making a small program and got to a point where I have to compare two InfinityScript.Vector values. I can't figure out how to do this properly. I tried using the == operator, that failed with

Operator '==' cannot be applied to operands of type 'InfinityScript.Vector3' and 'InfinityScript.Vector3'

My last attempt is most definitely wrong:

    Vector3 flag1 = new Vector3(33, 66, -255);
    Vector3 ori = player.Origin;
    if (ori = flag1) {
        //do something
    }

Which produces:

Cannot implicitly convert type 'InfinityScript.Vector3' to 'bool'

What is the right way to do this?

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536

5 Answers5

6

InfinityScript.Vector3 is missing a decent way to compare vectors. Not entirely unusual, floating point comparison is quite troublesome. You could use:

if (ori.DistanceTo(flag) < 0.5f) {
    // Close enough
    //...
}

Modify 0.5f to whatever value you consider "close enough". Don't make it 0.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Just wanted to ask, do I have to add f at the end of the numbers? @Hans Passant – user3713606 Dec 21 '14 at 14:59
  • You don't *have* to, I just never lie about numbers. The math here is single precision (float), not double. No point in burning the cycles to convert to double. – Hans Passant Dec 21 '14 at 15:06
  • This is a great answer to the actual problem. However the question as it is now is quite misleading, so in order for this question to be of use to future readers I would suggest @user3713606 to modify the question to something like " how to compare InfinityScript.Vector3" instead of the unrelated syntax error "... vector3 to bool" – HugoRune Dec 21 '14 at 15:07
2

= is assignment == compares

So you'd want

if (ori == flag1)

Rhys
  • 2,055
  • 2
  • 18
  • 23
  • I see you now have an answer. I'll leave my answer here just in case any future readers find it useful. Glad you've sorted your problem. – Rhys Dec 21 '14 at 16:03
1

You are using "=" instead of "==" in the if statement.

Amir Keren
  • 201
  • 2
  • 7
1

As others have said you want == to compare and not = (that's assignment)

I get that "it doesn't work", but that's no reason to try something else but instead to understand what's going on. You're getting an error that says you can't compare a type with the same type i can't think of many cases where that would happen, do you maybe use an out of date dll using the same class (which would mean it can't compare two versions of the same class together)?

You'd have to tell us more about how your project is organised (single project or multiple assemblies) and give us the source code for Vector3 or ideally if it's a simple project and not sensitive just post the solution ziped here.

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
  • Just wanted to say, I have used == operator all the time, however, I was messing a bit with the code, and forgot to put it all back to how it was. – user3713606 Dec 21 '14 at 14:53
  • @user3713606 Just the source for vector3 isn't enough, we also need the solution itself (so that we can see what you're compiling, how it links to vector 3, is it compiled at the same time, in another project in the same solution, in a separate dll pre built etc?) Odds are the issue is at the project level and not the code level. – Ronan Thibaudau Dec 21 '14 at 15:06
-1

You will need to use == when you are comparing the equality of two variables:

        if (ori == flag1)
        {//do something

        }

If you are still getting errors then it is possible that values you are comparing are of a different type. An example would be the variable ori is an integer and the variable value flag1 is of a different type

The following explains how you can compare the two values without knowing the types of the variables. C# compare two objects of unknown types (including reference and value types)

Community
  • 1
  • 1
Larry Lane
  • 2,141
  • 1
  • 12
  • 18