Giter Site home page Giter Site logo

Comments (5)

Ron2 avatar Ron2 commented on June 2, 2024

Hello, JelleVM,

Good question. You're talking about the DomNodeAdapter's GetAttribute<T>. If you go to the DomNodeAdapter's DomNode, then you can call the DomNode's IsAttributeDefault(AttributeInfo) which I think will do what you want.

        /// <summary>
        /// Gets whether or not the attribute's value (like by calling GetAttribute) is equal to
        /// the default value.</summary>
        /// <param name="attributeInfo">Attribute metadata</param>
        /// <returns>True if the attribute's value is equal to the default value</returns>
        public bool IsAttributeDefault(AttributeInfo attributeInfo)

So, your code would change from:

if (myDomNodeAdapter.HasAttribute<float>(attributeInfo))

to:

if (myDomNodeAdapter.DomNode.IsAttributeDefault(attributeInfo))

Right?

We could add another convenience method to DomNodeAdapter, to save the client from having to call (and discover) the DomNode.IsAttributeDefault() method. I don't think I would call this convenience method "HasAttribute" though, because conceptually, a DomNode "has" all the attributes that are defined for it.

--Ron

from atf.

JelleVM avatar JelleVM commented on June 2, 2024

Hi Ron, thanks for your reply. Thanks for pointing me to the IsAttributeDefault method, I hadn't thought of that. However, I think it doesn't fully solve the problem. (Though I understand what you're saying that "conceptually, a DomNode 'has' all the attributes that are defined.").

What would you do in the following scenario: your DomNode has an optional variable, say of type int. If this variable is not there, you want your programme to compute it. If it is there already, you don't do anything, to avoid unnecessary computation. This would be expressed by the following code:

int m_someVariable; if (HasAttribute(assets.SomeType.myOptionalTypeAttribute)) m_someVariable = GetAttribute(assets.SomeType.myOptionalTypeAttribute); else m_someVariable = ComputeValueOfOptionalAttribute(); // heavy computation

Now the int value of the attribute could be 0. So if we'd use DomNode.IsAttributeDefault() instead and the variable value happens to be equal to the default value (i.e. 0), we will be doing heave extra work for nothing, I think. Am I right?

Could maybe DomNode use a new method:

public bool HasAttribute(AttributeInfo attributeInfo) /\* or "IsAttributeNull" or "HasOptionalAttribute" */ { return GetLocalAttribute(attributeInfo) == null; }

from atf.

Ron2 avatar Ron2 commented on June 2, 2024

Hi JelleVM,

I see the problem; thank you for carefully explaining it. I think you're right, that this would be a useful method. Client code can call GetLocalAttribute() and check the result, but that's not very readable.

I need to think about the name for a bit, write unit tests, and ask coworkers before I check this in. (I'm in Japan for a conference this week, announcing that LevelEditor is open source!) The problem I see with HasAttribute(AttributeInfo) is that it might imply that the method is checking if the AttributeInfo has been defined for that DomNode's DomNodeType. How about AttributeWasSet()? Or AttributeHasBeenSet()?

/// <summary>
/// Gets whether or not the attribute has ever been set, even if it was set to its
/// default value.</summary>
/// <param name="attributeInfo">Attribute metadata</param>
/// <returns>True if the attribute has even been set and false otherwise.</returns>
public bool AttributeWasSet(AttributeInfo attributeInfo)
{
    return GetLocalAttribute(attributeInfo) != null;
}

--Ron

from atf.

JelleVM avatar JelleVM commented on June 2, 2024

Hi Ron,

Thanks for considering it! It's indeed quite an uncommon user case, but that's why I brought it up.
The name for the function is entirely up to you and your team of course, but I agree that your suggestions are probably clearer for a generic user case.
Have fun in Japan -- more open source ATF stuff, yeay! :-)

from atf.

Ron2 avatar Ron2 commented on June 2, 2024

I'll check this new method on DomNode shortly. Thanks again for the good suggestion!

/// <summary>
/// Gets whether or not the attribute has been set, even if it was set to its default value.</summary>
/// <param name="attributeInfo">Attribute metadata</param>
/// <returns>True iff the attribute has been set to anything other than null</returns>
/// <remarks>Setting an attribute to null is special, and makes the attribute look like it
/// was never set.</remarks>
public bool IsAttributeSet(AttributeInfo attributeInfo)
{
    return GetLocalAttribute(attributeInfo) != null;
}

from atf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.