Author Topic: (SOLVED) Local variable unexpectedly blanks when going into a for loop.  (Read 432 times)

Code: [Select]
function BSEHelpNode::setIDsOfChildren( %this , %parentID )
{
    if( %parentID !$= "" )
        %pIDhead = %parentID @ ".";
    else
        %pIDhead = "";

    for( %j = 0 ; %j < %this.getCount() ; %j++ )
    {
        %this.getObject( %j ).id = %pIDhead @ (%j + 1);
        %this.getObject( %j ).setIDsOfChildren( %this.id );
    }
    return 1;
}
It has to be a recursive function so that it can have sections in sections in sections in sections and so on.

%pIDhead stays its value until the for loop. The BSEHelpNodes are ScriptGroups.
« Last Edit: July 16, 2013, 09:46:34 AM by Axo-Tak »

Add in some echoes and show us the result.

(hit post on accident, I am still trying to fix this)
Add in some echoes and show us the result.
Code: [Select]
function BSEHelpNode::setIDsOfChildren( %this , %parentID )
{
    echo("<item=" @ %parentID SPC %this.id @ ">");
    if( %parentID !$= "" )
    {
        echo(%parentID SPC %this.id @ ": has a parent");
        %name = %parentID @ ".";
    }
    else
    {
        echo(%parentID SPC %this.id @ ": has no parent");
        %name = "";
    }
    echo(%parentID SPC %this.id @ ": before for" SPC %name);
    for( %j = 0 ; %j < %this.getCount() ; %j++ )
    {
        echo(%parentID SPC %this.id @ ": after for" SPC %name);
        echo(%parentID SPC %this.id @ ": iterator " @ %j + 1 SPC %this.getCount());
        %this.getObject( %j ).id = %name @ (%j + 1);
        %this.getObject( %j ).setIDsOfChildren( %this.id );
    }
    echo("</item=" @ %parentID SPC %this.id @ ">");
    return 1;

Quote from: Console
<item= >
 : has no parent
 : before for
 : after for
 : iterator 1 6
<item= 1>
 1: has no parent
 1: before for
</item= 1>
 : after for
 : iterator 2 6
<item= 2>
 2: has no parent
 2: before for
</item= 2>
 : after for
 : iterator 3 6
<item= 3>
 3: has no parent
 3: before for
 3: after for
 3: iterator 1 2
<item=3 1>
3 1: has a parent
3 1: before for 3.
</item=3 1>
 3: after for
 3: iterator 2 2
<item=3 2>
3 2: has a parent
3 2: before for 3.
</item=3 2>
</item= 3>
 : after for
 : iterator 4 6
<item= 4>
 4: has no parent
 4: before for
 4: after for
 4: iterator 1 1
<item=4 1>
4 1: has a parent
4 1: before for 4.
</item=4 1>
</item= 4>
 : after for
 : iterator 5 6
<item= 5>
 5: has no parent
 5: before for
</item= 5>
 : after for
 : iterator 6 6
<item= 6>
 6: has no parent
 6: before for
</item= 6>
</item= >
« Last Edit: July 16, 2013, 12:55:14 AM by Axo-Tak »

OK I provided the echos.

Code: [Select]
function BSEHelpNode::setIDsOfChildren( %this )
{
    if( %this.id $= ""  )
        %idHead = "";
    else
        %idHead = %this.id @ ".";
    
    for( %i = 0 ; %i < %this.getCount() ; %i++ )
    {
        %obj = %this.getObject( %i );
        %obj.id = %idHead @ (%i + 1);
        
        if( %obj.getCount() > 0 )
            %obj.setIDsOfChildren();
    }

    return 1;
}

I rewrote the code.