Both null and not null? Testing for the existence of a data element

  • I've managed to figure out how to provide a textbox on each scene in a tour. I've even made it dynamic by creating a correlation between scene names and my data fields:

    Code
    <scene name="scene_01" ...
    <scene name="scene_02" ...
    <scene name="scene_03" ...
    
    
    ...
    
    
    <data name="desc_scene_01">Scene 1.</data>
    <data name="desc_scene_02">Scene 2.</data>

    Not every scene has a description so for those scenes, I want to test for the existence of the data element and just hide the text layer. That is where I'm having a problem.

    I can do this to get what the data elements should be IF they exist with no problem:

    Code
    txtadd(dataName,'desc_',get(scene[get(xml.scene)].name));
    txtadd(dataAssignName,'data:',get(dataName));
    set(dataContent, get(data[get(dataName)].content));

    I can even do a trace to see what the content of the field is and the trace returns null if the scene doesn't have a corresponding data element (such as scene_03 above). This returns null for scene_03...returns content for scenes 1 and 2.

    Code
    trace(get(dataContent));


    But, when I do this on the very next line it says, "not null"

    Code
    if (get(dataContent) == null, trace('null'),trace('not null'));

    HUH? How can get(dataContent) evaluate to null on one line and then when I do a comparison on the very next line and compare it to null, it evaluates to false?

  • I've tried the following:

    Code
    if (get(dataContent) === 'null', trace('null'),trace('not null'));
    if (get(dataContent) === null, trace('null'),trace('not null'));
    
    
    if (get(dataContent) == 'null', trace('null'),trace('not null'));
    if (get(dataContent) == null, trace('null'),trace('not null'));


    The first two evaluate to null regardless of whether I have a data node, the second two evaluate to not null regardless.

    I may wind up putting the call in the scene tag itself, but architecturally, I'm putting this information in an include file and trying to avoid messing with the generated main file as much as possible in case I ever want to regenerate a mass of files for some reason (possibly if there is a version upgrade or if I change my site concept). I'm hoping to write some tooling to help me in managing the site content and the less I need to touch generated code, the better (although I suppose I could just generate that page myself and overwrite the krpano xml files entirely).

    The main reason though is that I've just started learning this tool. As a long-time geek, not understanding why something works the way it does bothers me as something like this could bite me down the road if the behavior is inexplicable to me.

  • Hi,

    the correct way for testing if a variable exists would be this: (not get() and no quotes):

    Code
    if(variablename !== null, trace(the variable exists) );
    if(variablename === null, trace(the variable does not exists) );

    Best regards,
    Klaus

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!