what does this mean: var color = (x&y) ? Color.white : Color.gray;

looked at the ref and came across some code I just didnt understand. Could anyone explain what happens at that line, like what does the questionmark and the “” sign do? what decides if “color” is white or gray?

function Start () {
// Create a new texture and assign it to the renderer’s material
var texture = new Texture2D(128, 128);
renderer.material.mainTexture = texture;

// Fill the texture with Sierpinski’s fractal pattern!
for (var y : int = 0; y < texture.height; ++y) {
for (var x : int = 0; x < texture.width; ++x) {
var color = (x&y) ? Color.white : Color.gray; // this???
texture.SetPixel (x, y, color);
}
}
// Apply all SetPixel calls
texture.Apply();
}

link: Unity - Scripting API: Texture2D.SetPixel

Hi there … when you see a statement of the form:

expression?value1:value2

you should read it as

if expression is true then value is value1 else value is value2

The expression

(x y)

is the logical “boolean” AND of “x and y”

The truth table for this is
0 1
0 0 0
1 0 1

Neil

Thanks!

Not in this case. is functioning as a logical bitwise operator. The giveaway, aside from the fact that the algorithm wouldn’t make sense if it were a boolean operation, and that the operands are ints, is that it’s a single . is the superior choice for boolean operations, and the one you’ll see used everywhere for that purpose.

How is that read?

Sadly correct - apparently US is one of those languages where any integral value (other than 0) returns true.

If false false = false;
If true false = false;
If false true = false;
If true true = true;

I would point out that the boolean and the boolean have similar but ultimately different behaviors.

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        if (false  DoSomething(1)) ;
        if (false  DoSomething(2)) ;
    }

    bool DoSomething(int i)
    {
        print(i);
        return true;
    }
}

Also :? is called a ternary operator. Worth reading http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx

If that were true, every pixel would be white except the one in the bottom left. Also, “sadly” throws me off. ???

I know what it’s supposed to say, but don’t know how the symbols are supposed to match that.

Nope, there’s an ‘’ operator.

I quite like C#'s enforcement that booleans are booleans, integers are integers.

http://upload.wikimedia.org/wikibooks/en/math/3/9/e/39ee2b3c0e8cd1058288b67aeee12251.png

Okay, I get the table now, but the rest of what you’re saying doesn’t sound like sober English.

What’s hard to understand? That the operator which combines the two int’s in a a special way meaning more than just bottom left will be gray? For example 12 == 0?

Or that the C# has made a decision that one cannot implicitly convert int to bool, something that US hasn’t done, and I prefer the former approach?

No, that’s clear.

I get what you’ve said, now. You were talking about what feeds the ?: operator when I thought you were talking about what feeds the operator. That is, I thought you were saying that x&y was a boolean operation, and at the same time, saying I was correct that it was a bitwise operation. Still, this I don’t get:

Debug.Log(1  2);  // Why is it 2, in UnityScript?

Ahh, no that wasn’t what I was saying :slight_smile:

It seems to be some FUBAR due to the JS specification… I’m confused as to why they do it, but it is documented behavior according to google:

It first evaluates the left hand operand, and if this returns false then, without going any further, it returns false for the whole expression. Otherwise it returns the value of the second operand: true or false for a Boolean value, or the actual value itself if non-Boolean.

http://www.devguru.com/technologies/ecmascript/quickref/logical_operators.html

I guess because the guy who set the rules for JS/Ecma thought, it would be cool to “easy” assign optional parameters

    function blah(parameter1) {
        var value = parameter || "defaultValue"; 
        // In C# it's equal to 
        var value = parameter??"defaulValue";
    }

is the exact opposite of || so it “makes sense”, though it was a horribly wrong choice from design perspective.