Domanda

So I have a little sensor in my 'scene' and when it gets blocked I want my program to wait 4 seconds and if the sensor is still blocked I want it to alert the user.

The script works fine if the sensor remains blocked.

However, if you block the sensor for less than the 4 seconds and then remove the part from the sensor (which should result in no alert since the sensor is no longer blocked) the alert still runs even though the condition is no longer true.

Can any buddy see what I am doing wrong here?

The OnBlocked() function runs when the sensor is blocked and the OnCleared() function runs when the sensor is cleared.

The OnisBlockedUpdated() function runs when the isBlocked property is updated.

function OnBlocked( sender : Demo3D.Visuals.PhysicsObject, load : Demo3D.Visuals.Visual )
{
    sender.isBlocked = true;
    sender.PropertiesUpdated();
}

function OnCleared( sender : Demo3D.Visuals.PhysicsObject, load : Demo3D.Visuals.Visual )
{
    sender.isBlocked = false;
    sender.PropertiesUpdated();
}

function OnisBlockedUpdated( sender : Demo3D.Visuals.BoxVisual, value : System.Boolean, oldValue )
{
    var blockingObj : Demo3D.Visuals.BoxVisual = sender.BlockingLoad;

    if (value)
    {   
        print("isBlocked");
        wait(4);
        sender.PropertiesUpdated();
        if(value)
        {
            alert("The object '" + blockingObj + "' is too tall for this bay.");
        }
        else
        {
            return;
        }
    }
    if(!value)
    {
        print("cleared");
        return;
    }   
}
È stato utile?

Soluzione

if you block the sensor for less than the 4 seconds and then remove the part from the sensor (which should result in no alert since the sensor is no longer blocked) the alert still runs even though the condition is no longer true

The function is still running while you're doing your wait(), and the value parameter will be passed in by-value, not by-reference, so it isn't going to change in the middle of the body of the function.

If you want to evaluate that value after the wait has completed, you're going to have to get the current value, rather than the value passed into the function.

Try:

if (value)
{   
    print("isBlocked");
    wait(4);
    sender.PropertiesUpdated();
    if(sender.isBlocked) // *** This is the line I changed
    {
        // ...

Although I'm not sure if sender is going to be the same object as in the OnBlocked or OnCleared function, since it is a different type. You may need to figure out some way to get a reference to that same sender : Demo3D.Visuals.PhysicsObject object.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top