Skip to content

Lever animation script

ZephiriusZephirius Member Posts: 419
edited July 18 in Builders - Scripting
Trying to have the lever play its animation every time its pulled. Not just once! I'm all over the map on this one. I have the code, but it's a mess.
Thanks for any help...

code so far:
void main()
{
    object oPC = GetLastUsedBy();
    if (!GetIsPC(oPC)) return;

    object oSound = GetObjectByTag("EYEBALL");

    SoundObjectSetPosition(oSound, GetPosition(oPC));
    SoundObjectSetVolume(oSound, 127);
    SoundObjectPlay(oSound);

    AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));

    // Set a local integer.
    SetLocalInt(oPC, "LeverPulled", 0);
    if ( GetLocalInt(oPC, "LeverPulled"))
    {

    AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE));
    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect
    ( VFX_IMP_GLOBE_USE ), GetObjectByTag("TALK_01")); return;
    }


    if ( GetLocalInt(oPC, "LeverPulled") == 0 )
    {
    SetLocalInt(oPC, "LeverPulled", 1);

    AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE));
    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect
    ( VFX_IMP_GLOBE_USE ), GetObjectByTag("TALK_01"));
    }
}

Told you it was a messs. :neutral:

I don't understand what values to assign to this script, so that the lever aniamtion always plays. As it stands now the animation runs only once....?
Post edited by Zephirius on

Comments

  • ProlericProleric Member Posts: 1,316
    edited July 18
    At some point, you need ANIMATION_PLACEABLE_DEACTIVATE.

    Right now you have "activate" in both cases.

    Also, setting "LeverPulled" to 0 unconditionally means only the second case ever runs.

    The logic you need is - if "LeverPulled" is not set, set "LeverPulled" & activate, else clear "LeverPulled" & deactivate.

    This is assuming you want the level to move one way at first and the other way on the next attempt.
  • ZephiriusZephirius Member Posts: 419
    edited July 18
    Thanks Proleric.
    Edit:

    It's working now, if a bit clunky, but then again - I'm no coder. :smiley:
    Code so far -
    void main()
    {
        object oPC = GetLastUsedBy();
        if (!GetIsPC(oPC)) return;
    
        // Declare sound object.
        object oSound = GetObjectByTag("EYEBALL");
    
        SoundObjectSetPosition(oSound, GetPosition(oPC));
        SoundObjectSetVolume(oSound, 127);
        SoundObjectPlay(oSound); // Play Sound.
    
        AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
    
        // Pull lever with animation.
        AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE));
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect
        ( VFX_IMP_GLOBE_USE ), GetObjectByTag("TALK_01"));
    
        // Set a local integer.
        SetLocalInt(oPC, "LeverPulled", 1);
    
        if ( GetLocalInt(oPC, "LeverPulled") == 1 )
        {
            AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE));
    
            DelayCommand(0.1, AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE)));
            ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect
            ( VFX_IMP_GLOBE_USE ), GetObjectByTag("TALK_01"));
    
            SoundObjectPlay(oSound); // Play Sound.
            AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
    
        }
    }
    
    Post edited by Zephirius on
  • ZephiriusZephirius Member Posts: 419
    Now Proleric, try this on for size -
    Evertyhing works as it should, except for the sound files. When the sound plays, it plays two sound files simultaneously. That's no beuno... :neutral:

    new code -
    string sString1 = "vs_nbarbaf1_warn";
    string sString2 = "vs_nbarbam1_051";
    string sString3 = "vs_nwizarf1_bat2";
    string sString4 = "vs_nx0heurf_haha";
    string sString5 = "vs_nx2arief_can";
    string sString6 = "vs_nx2mephm_102";
    
    void main()
    {
        object oPC = GetLastUsedBy();
        if (!GetIsPC(oPC)) return;
    
        // Pull lever with animation.
        AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE));
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect
        ( VFX_IMP_GLOBE_USE ), GetObjectByTag("TALK_01"));
    
        switch ( d6(1) )
        {
            case 1: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString1)); break;
            case 2: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString2)); break;
            case 3: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString3)); break;
            case 4: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString4)); break;
            case 5: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString5)); break;
            case 6: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString6)); break;
            return;
        }
    
        // Set a local integer.
        SetLocalInt(oPC, "LeverPulled", 1);
    
        if ( GetLocalInt(oPC, "LeverPulled") == 1 )
        {
            switch ( d6(1) )
            {
                case 1: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString1)); break;
            case 2: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString2)); break;
            case 3: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString3)); break;
            case 4: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString4)); break;
            case 5: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString5)); break;
            case 6: AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            AssignCommand(oPC, PlaySound(sString6)); break;
    
    
            AssignCommand(GetObjectByTag("TALK_01"), ActionSpeakString("Muhahaha..."));
            }
        }
            AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE));
    
            DelayCommand(0.1, AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE)));
            ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect
            ( VFX_IMP_GLOBE_USE ), GetObjectByTag("TALK_01"));
    }
    

    If you see any glaring errors, please holler. In fact, shout it at the roof tops if it'll help. :smile:
  • meaglynmeaglyn Member Posts: 151
    You've still got an "if" statement that will always be true in there. I think you are over-complicating it by saving the state though.

    Something like this (which I have not tested...) maybe?:
    string sString1 = "vs_nbarbaf1_warn";
    string sString2 = "vs_nbarbam1_051";
    string sString3 = "vs_nwizarf1_bat2";
    string sString4 = "vs_nx0heurf_haha";
    string sString5 = "vs_nx2arief_can";
    string sString6 = "vs_nx2mephm_102";
    
    void main()
    {
        object oPC = GetLastUsedBy();
        if (!GetIsPC(oPC)) return;
    
        object oDest = GetObjectByTag("TALK_01");
        
        // Pull lever with animation.
        AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE));
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_GLOBE_USE), oDest);
        AssignCommand(oDest, ActionSpeakString("Muhahaha..."));
        
        switch ( d6(1) ) {
            case 1: AssignCommand(oPC, PlaySound(sString1)); break;
            case 2: AssignCommand(oPC, PlaySound(sString2)); break;
            case 3: AssignCommand(oPC, PlaySound(sString3)); break;
            case 4: AssignCommand(oPC, PlaySound(sString4)); break;
            case 5: AssignCommand(oPC, PlaySound(sString5)); break;
            case 6: AssignCommand(oPC, PlaySound(sString6)); break;
        }
        
        DelayCommand(0.1, AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE)));
    }
    

    Also, is it odd that you are having the PC play the sound? I left it that way.
  • ZephiriusZephirius Member Posts: 419
    Thanks meaglyn. I appreciate the assist. I have an uncanny knack for overcomplicating things. :smiley:
  • ZephiriusZephirius Member Posts: 419
    meaglyn wrote: »
    You've still got an "if" statement that will always be true in there. I think you are over-complicating it by saving the state though.

    Something like this (which I have not tested...) maybe?:
    string sString1 = "vs_nbarbaf1_warn";
    string sString2 = "vs_nbarbam1_051";
    string sString3 = "vs_nwizarf1_bat2";
    string sString4 = "vs_nx0heurf_haha";
    string sString5 = "vs_nx2arief_can";
    string sString6 = "vs_nx2mephm_102";
    
    void main()
    {
        object oPC = GetLastUsedBy();
        if (!GetIsPC(oPC)) return;
    
        object oDest = GetObjectByTag("TALK_01");
        
        // Pull lever with animation.
        AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE));
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_GLOBE_USE), oDest);
        AssignCommand(oDest, ActionSpeakString("Muhahaha..."));
        
        switch ( d6(1) ) {
            case 1: AssignCommand(oPC, PlaySound(sString1)); break;
            case 2: AssignCommand(oPC, PlaySound(sString2)); break;
            case 3: AssignCommand(oPC, PlaySound(sString3)); break;
            case 4: AssignCommand(oPC, PlaySound(sString4)); break;
            case 5: AssignCommand(oPC, PlaySound(sString5)); break;
            case 6: AssignCommand(oPC, PlaySound(sString6)); break;
        }
        
        DelayCommand(0.1, AssignCommand(OBJECT_SELF, ActionPlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE)));
    }
    

    Also, is it odd that you are having the PC play the sound? I left it that way.
    Yeah, I never really thought about how odd it would be for the sound to be centered on the PC. I will remedy that shortly.
  • ZephiriusZephirius Member Posts: 419
    Just tried your code meaglyn. Works perfectly. Much more efficient I'd say. :wink:
    Thanks again.
  • ProlericProleric Member Posts: 1,316
    Interesting... I would expect that code to play sounds concurrently (not consecutively). If so, DelayCommand on PlaySound for the duration of the previous sound would fix it.
  • MelkiorMelkior Member Posts: 204
    Something which can complicate the problem is that the floor levers seem to both work backwards (and I'm fairly sure they have never been corrected by any update). That is, when each lever is placed, it seems to already be in the "activated" position so if you have it play the activate animation, it "jumps" to the opposite position and then plays the animation back to where it was before. Following activations though, seem to work correctly.
    I've taken to never starting a floor lever in its default starting position but always setting it explicitly to either "activated" or "deactivated" before positioning it the way I want it.
  • ProlericProleric Member Posts: 1,316
    Melkior wrote: »
    ...I've taken to never starting a floor lever in its default starting position but always setting it explicitly to either "activated" or "deactivated" before positioning it the way I want it.
    Yes, you should always do that.

Sign In or Register to comment.