Frictional Games Forum (read-only)

Full Version: Let's play "why doesn't my script work?"!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Okay, so here's the scenario. Four valves (each starting at the middle position) turn in a specific order to open a portcullis. The correct order goes as such: R R L R. If the player gets one wrong, steam spurts out of a pipe and the order resets. Seems simple enough, right? Right. Here's the code.

Code:
void valve1turn(string &in asEntity, int alState)
{
if(alState == 1)
    {
    AddLocalVarInt("gateopen", 1);
    if(GetLocalVarInt("gateopen") == 4)
        {
        SetMoveObjectState("castle_portcullis_1", 0.7);
        }
    else
        {
        AddLocalVarInt("gateopen", 1);
        }
    }
if(alState == 0)
    {
    SetMoveObjectState("castle_portcullis_1", 0);
    PlaySoundAtEntity("", "13_press_done.snt", "steamarea", 0, false);
    CreateParticleSystemAtEntity("", "ps_steam_puff.ps", "steamarea", false);
    SetLocalVarInt("gateopen", 0);
    for(int i=1;i<=4;i++)SetWheelAngle("valvepuzzle_"+i, 0, true);
    }
}
void valve2turn(string &in asEntity, int alState)
{
if(alState == 1)
    {
    AddLocalVarInt("gateopen", 1);
    if(GetLocalVarInt("gateopen") == 4)
        {
        SetMoveObjectState("castle_portcullis_1", 0.7);
        }
    else
        {
        AddLocalVarInt("gateopen", 1);
        }
    }
if(alState == 0)
    {
    SetMoveObjectState("castle_portcullis_1", 0);
    PlaySoundAtEntity("", "13_press_done.snt", "steamarea", 0, false);
    CreateParticleSystemAtEntity("", "ps_steam_puff.ps", "steamarea", false);
    SetLocalVarInt("gateopen", 0);
    for(int i=1;i<=4;i++)SetWheelAngle("valvepuzzle_"+i, 0, true);
    }
}
void valve3turn(string &in asEntity, int alState)
{
if(alState == 0)
    {
    AddLocalVarInt("gateopen", 1);
    if(GetLocalVarInt("gateopen") == 4)
        {
        SetMoveObjectState("castle_portcullis_1", 0.7);
        }
    else
        {
        AddLocalVarInt("gateopen", 1);
        }
    }
if(alState == 1)
    {
    SetMoveObjectState("castle_portcullis_1", 0);
    PlaySoundAtEntity("", "13_press_done.snt", "steamarea", 0, false);
    CreateParticleSystemAtEntity("", "ps_steam_puff.ps", "steamarea", false);
    SetLocalVarInt("gateopen", 0);
    for(int i=1;i<=4;i++)SetWheelAngle("valvepuzzle_"+i, 0, true);
    }
}
void valve4turn(string &in asEntity, int alState)
{
if(alState == 1)
    {
    AddLocalVarInt("gateopen", 1);
    if(GetLocalVarInt("gateopen") == 4)
        {
        SetMoveObjectState("castle_portcullis_1", 0.7);
        }
    else
        {
        AddLocalVarInt("gateopen", 1);
        }
    }
if(alState == 0)
    {
    SetMoveObjectState("castle_portcullis_1", 0);
    PlaySoundAtEntity("", "13_press_done.snt", "steamarea", 0, false);
    CreateParticleSystemAtEntity("", "ps_steam_puff.ps", "steamarea", false);
    SetLocalVarInt("gateopen", 0);
    for(int i=1;i<=4;i++)SetWheelAngle("valvepuzzle_"+i, 0, true);
    }
}

Here's where it sucks. If I input the correct order, nothing happens. The portcullis is still stuck there. If I move any of the wheels other than the third to any position, nothing happens. Left or right, wheels 1, 2, and 4 do nothing. Wheel 3 acts as it should; right (the wrong direction) does the steam thing and resets the wheels, and left clicks as it should.

So, what do? Why won't this work? I feel like my logic is fairly sound withing the coding. It's late, and I don't want to work on this any more, so I was hoping someone more experienced had some insight. Smile
Assuming the wheels function similarly to the levers and the wheel that you turn to work the burner near the end of the main game, wouldn't script calculate it as the wheel end positions being at -1 and 1?
And if not that, then maybe similar to the cogwheel levers in the sewer? They end at 0 and 2.

What you could also do is add Debug Messages in the coding of your 1st, 2nd and 4th wheels. You may through this be able to determine exactly where your code is going wrong.
Ha, I'm an idiot. You're absolutely right, the problem was that they started at 0 and ended at 1. I blame the fact that I was tired. Tongue

However, the issue still remains that when I do it all right, the door doesn't move at all. I'm certain it's a moveobject, I've used it before...
Why do you have three functions that do exactly the same thing?
Where?

There are four valves, and each function pertains to a different valve. What do you mean?
valve1turn, valve2turn and valve4turn do the exact same thing (valve3turn only differs in the state checks). The issue, however, that i can spot is that you're adding to the local map variable more times than you should. Turn one valve correctly, the local map variable becomes 2 by the time the function ends. Turn the second valve correctly, it adds up to 3, notices that it doesn't equal 4 and then moves on to the else, adding up to 4. Turn the third valve correctly, the local map variable adds up to 5, and so on. Therefore, even though the variable reaches 4 at some point, it is never at 4 when checked for 4; it is either greater than or less than 4 at the time it is checked.
Thank you. I will look into that tomorrow. Smile
It always takes me so long to reply. You can read this if you want, but I'm sure you would have the general idea already

There are two ways I believe you could fix this from past experience... (I'm a nooby scripter Tongue)
1. Make the door explode Tongue The downside is it is noisy and if done incorrectly, can be a bit messy and a downside, but it clears the path. You would do this with
Code:
SetPropHealth("<portcullis>", 0.0f);
2. Use force:Well, the first thing you have to do is make sure the door unlocks after all the conditions are met. I don't know exactly where that code is in your code because I cannot read it specifically coz of my script knowlege, then maybe apply force that pushes the door open from either in front or behind.
Code:
SetSwingDoorLocked("<portcullis>", false, true);
SetSwingDoorDisableAutoClose("<portcullis>", true);  
SetSwingDoorClosed("<portcullis>", false, true);
AddPropForce("<portcullis>", x, y, z, "world");
The first line unlocks the door. The next one makes sure that when force is applied, the door doesn't just shut itself again. The next code opens the door ever so slightly, and then the AddPropForce simply pushes the door open Smile
Code:
Change:
"<Portcullis>" - Change to the name of your door as indicated in your Level Editor.
x, y, z, - Change to the needed co-ordinate. Your door should open either along the x or z axis :)

I may not be the best but that MAY help or give you an insight as to what to do Smile
(04-12-2013, 09:08 AM)Your Computer Wrote: [ -> ]valve1turn, valve2turn and valve4turn do the exact same thing (valve3turn only differs in the state checks). The issue, however, that i can spot is that you're adding to the local map variable more times than you should. Turn one valve correctly, the local map variable becomes 2 by the time the function ends. Turn the second valve correctly, it adds up to 3, notices that it doesn't equal 4 and then moves on to the else, adding up to 4. Turn the third valve correctly, the local map variable adds up to 5, and so on. Therefore, even though the variable reaches 4 at some point, it is never at 4 when checked for 4; it is either greater than or less than 4 at the time it is checked.

Oh yeah. So it should be changed to
PHP Code:
if(GetLocalVarInt("gateopen") >= 4
Since it's larger than 4. Unless it's somehow 4 (MAGIC), i added an 4 so it became "larger than/equal to" 4.
As long as the outcome does not reach an integer >= 4 before all four valves are turned to the correct position, it should work fine Smile
Pages: 1 2