void StartHazard(object oTrap);
void StopHazard(object oTrap);

void StartHazard(object oTrap)
{
    SetLocalInt(oTrap, "bActive", TRUE);
    ExecuteScript("cas_plcvfxobject", oTrap); // Run the hazard script on the placeable
}

void StopHazard(object oTrap)
{
    SetLocalInt(oTrap, "bActive", FALSE);
}

void main()
{
    object oActivator = GetLastUsedBy(); // Default: Used by a switch
    if (!GetIsObjectValid(oActivator))
    {
        oActivator = GetLastSpeaker(); // If triggered by conversation, get the speaking PC
    }

    if (!GetIsObjectValid(oActivator))
    {
        return; // Safety check, should never happen
    }

    object oSelf = OBJECT_SELF; // The switch or conversation object
    object oTrap = GetObjectByTag("CAS_PLC_VFXTRAP"); // Find the trap placeable in the same area

    if (!GetIsObjectValid(oTrap))
    {
        SendMessageToPC(oActivator, "No hazard found in this area.");
        return;
    }

    int bActive = GetLocalInt(oTrap, "bActive");

    if (bActive == FALSE) 
    {
        SendMessageToPC(oActivator, "You activate the hazard!");
        StartHazard(oTrap);
    }
    else 
    {
        SendMessageToPC(oActivator, "You deactivate the hazard.");
        StopHazard(oTrap);
    }
}
