Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lamp puzzle scripting help
Soon Offline
Member

Posts: 50
Threads: 11
Joined: Sep 2010
Reputation: 0
#1
Lamp puzzle scripting help

Hey,

I'm doing a puzzle in a custom story where there's 4 lamps that have to be lit in a certain sequence to unlock a door. I'm not real sure where to begin with the scripting to make it work the way I want it to.

I want all the candles to be able to be lit before it blows them out showing that it was the wrong sequence. The only part of it I can figure out is that I have to use the on ignite callbacks in the editor. Then from there I'm sure its a super complicated string of if statements. I was looking for a way for it to "check" if a certain lamp has been lit, but all I can find in that department is SET lamp it. There is no GET lamp lit.

I'm starting to wonder if it's even possible. Would GetConnectionStateCallback work? I know it's used for buttons and levers but I'm not sure if it will work on lamp entities or not.

Any help would be appreciated.
10-03-2010, 07:18 PM
Find
Entih Offline
Junior Member

Posts: 47
Threads: 4
Joined: Sep 2010
Reputation: 0
#2
RE: Lamp puzzle scripting help

What you are trying to do here is actually a classic case of scripting and coding a keypad combination, something I have experience with mapping for Half Life 2. What I would do is name the lamps something meaningful. If there are four, make it 'lamp_combo_1' through 4, maybe even name them with the numbers in the order you want them lit for simplicity. If they must be lit in a certain order without instantly failing on a wrong light, have all four point to the same callback function.

Then, you may want to use local variables to check on how many were lit in total, and how many were lit in proper order.

Essentially, the correct lights and total lights count upward as the player lights them, and then you compare them against each other. If correct lights is less than total lights, it was a failure on the player's part. If they are equal, the player got it right.

To check for this, since all four lamps can point to the same callback (or so I believe, haven't tested extensively), you may have something like this:

if(Type == 'OnIgnite')
{

    if(GetLocalVarInt("TotalLights") < 4)
    {
        if((EntityName == "lamp_combo_1") && (GetLocalVarInt("TotalLights") == 0))
        {
            AddLocalVarInt("CorrectLights", 1);
...
        if((EntityName == "lamp_combo_2") && (GetLocalVarInt("TotalLights") == 1))
        {
            AddLocalVarInt("CorrectLights", 1);
...
    if(GetLocalVarInt("TotalLights") == GetLocalVarInt("CorrectLights"))
...

If you want, I can try and work out the script in more detail in a test map of mine.
10-03-2010, 07:40 PM
Find
Soon Offline
Member

Posts: 50
Threads: 11
Joined: Sep 2010
Reputation: 0
#3
RE: Lamp puzzle scripting help

I've been tryin to use that code you put there. I can't seem to get it working. If you could test it a little bit more and post the full code, that'd be very helpful. Thanks!
10-03-2010, 10:35 PM
Find
Entih Offline
Junior Member

Posts: 47
Threads: 4
Joined: Sep 2010
Reputation: 0
#4
RE: Lamp puzzle scripting help

Alright then, just whipped together your exact scenario in my map after I saw your reply. Hopefully this code helps you understand a bit better.

void combolit(string &in asEntity, string &in asType)
{
    if(asType == "OnIgnite")
    {
    
        // Here I named the candles in ascending order, 1 being what I want lit first, 4 last.  Using a local variable, I keep
        // track of how far along I am in the sequence.  If the right one is lit at the right time, it adds one to how many
        // were gotten right, if not, it does nothing.
        if((asEntity == "candle_combo_1") && (GetLocalVarInt("combototal") == 0))
        {
            AddLocalVarInt("comboright", 1);
        }
        if((asEntity == "candle_combo_2") && (GetLocalVarInt("combototal") == 1))
        {
            AddLocalVarInt("comboright", 1);
        }
        if((asEntity == "candle_combo_3") && (GetLocalVarInt("combototal") == 2))
        {
            AddLocalVarInt("comboright", 1);
        }
        if((asEntity == "candle_combo_4") && (GetLocalVarInt("combototal") == 3))
        {
            AddLocalVarInt("comboright", 1);
        }
        
        // I add one to the total lit regardless
        AddLocalVarInt("combototal", 1);
        
        // If we have all 4 lit...
        if((GetLocalVarInt("combototal") == 4))
        {
            // Were all the ones lit in correct order?
            if(GetLocalVarInt("combototal") == GetLocalVarInt("comboright"))
            {
                PlaySoundAtEntity("rawr", "guardian_ontop.snt", "Player", 0.0f, false);
            }
            // Or did we get off track somewhere.
            else if(GetLocalVarInt("comboright") < GetLocalVarInt("combototal"))
            {
                // If we got something wrong, reset the checking values
                SetLocalVarInt("comboright", 0);
                SetLocalVarInt("combototal", 0);
                
                // And kill the candles (timer due to trouble otherwise)
                // so player can try again.
                AddTimer("killcandles", 0.25f, "killcandles");
            }
        }
    }
}

void killcandles(string &in asTimer)
{
for(int i = 1; i <= 4 ; i++)
    {
        SetLampLit("candle_combo_" + i, false, false);
    }
}

Gotta love how abstractly you have to think for this stuff!
10-03-2010, 11:08 PM
Find
Soon Offline
Member

Posts: 50
Threads: 11
Joined: Sep 2010
Reputation: 0
#5
RE: Lamp puzzle scripting help

Wow! That did it! You are awesome. That script is still pretty confusing to me, but that's because I don't have any experience with integers yet. Thanks a ton for the comments in the script. It helps a load for me explaining what each part is doing. I'm gonna take a few minutes and just stare at the script and really try to figure it out so I can learn how to do things like this on my own in the future.

Thanks again for your help. It means a ton! Just have to tweak the script a little now and get some of the more special effect stuff worked in there.
Help me out a little further in undertanding this script please.

Do the integers just start at 0 as a default? And the part where it is "getting" the combototal integer to start out is 0 because it should be 0 on the first one. And before it goes to the next part of the if statement it has to wait on the function to be called again by lighting up another lamp because they all share the same callback. And because it doesn't go through every if statement at once it goes down to the line where it adds 1 to the combototal no matter which part of the if statement was true.

So... if you dont go to the right candle first, lets say the 2nd one. It "gets" the combototal that is not equal to 1 because it is 0 on the first, it doesn't add to the combocorrect integer. Thus setting it up for failure on the line that checks to see if the combocorrect is less than the combototal. Right?

I'm sorry if this text is super confusing because I'm trying to figure it out as I'm typing. If I'm on the right track though, please lemme know! Smile
(This post was last modified: 10-04-2010, 01:14 AM by Soon.)
10-04-2010, 12:58 AM
Find
Entih Offline
Junior Member

Posts: 47
Threads: 4
Joined: Sep 2010
Reputation: 0
#6
RE: Lamp puzzle scripting help

You're pretty close with that!

The local integer variables apparently default to 0 if you have not used them before, so you can get away with just get-ing them from 0 onward unless you need them to start higher.

That column of if's is exactly what it looks like, as well. If the player hits the wrong one at any point, that one will not trigger the if statement. Say you light up candle 3 first. It will look at it and essentially think, "Is this candle 3, and have we lit up 2 candles before it?" (Or depending on perspective, "Is this candle 1, and did we light up 0 others before it?"). Since the total candles lit before it would be 0, but its candle 3, that would be a big 'no', and correct hits would be less than total.

So pretty much, you got it.
10-04-2010, 02:01 AM
Find
Soon Offline
Member

Posts: 50
Threads: 11
Joined: Sep 2010
Reputation: 0
#7
RE: Lamp puzzle scripting help

Great! Thanks again for your help, I really appreciate it. I made sure to give you credit for the script with a comment in my script, so people don't think I was the mastermind behind that. I mean, the idea for the puzzle was mine, but it wouldn't have been possible without someone who knew how to execute it helping me Smile
10-04-2010, 03:13 AM
Find
Alexander Offline
Junior Member

Posts: 22
Threads: 2
Joined: Sep 2010
Reputation: 0
#8
RE: Lamp puzzle scripting help

You can also add some debug messages to the code to help give a better visual of how the script is working when running in debug mode.

void combolit(string &in asEntity, string &in asType)
{
    if(asType == "OnIgnite")
    {
    
        // Here I named the candles in ascending order, 1 being what I want lit first, 4 last.  Using a local variable, I keep
        // track of how far along I am in the sequence.  If the right one is lit at the right time, it adds one to how many
        // were gotten right, if not, it does nothing.
        if((asEntity == "candle_combo_1") && (GetLocalVarInt("combototal") == 0))
        {
            AddLocalVarInt("comboright", 1);

        }
        if((asEntity == "candle_combo_2") && (GetLocalVarInt("combototal") == 1))
        {
            AddLocalVarInt("comboright", 1);

        }
        if((asEntity == "candle_combo_3") && (GetLocalVarInt("combototal") == 2))
        {
            AddLocalVarInt("comboright", 1);

        }
        if((asEntity == "candle_combo_4") && (GetLocalVarInt("combototal") == 3))
        {
            AddLocalVarInt("comboright", 1);

        }
        
        // I add one to the total lit regardless
        AddLocalVarInt("combototal", 1);
        AddDebugMessage("combototal: "+GetLocalVarInt("combototal"), false);
        AddDebugMessage("comboright: "+GetLocalVarInt("comboright"), false);
        
        // If we have all 4 lit...
        if((GetLocalVarInt("combototal") == 4))
        {
            // Were all the ones lit in correct order?
            if(GetLocalVarInt("combototal") == GetLocalVarInt("comboright"))
            {
                PlaySoundAtEntity("rawr", "guardian_ontop.snt", "Player", 0.0f, false);
                AddDebugMessage("combototal and comboright are equal: SUCCESS!!!", false);
            }
            // Or did we get off track somewhere.
            else if(GetLocalVarInt("comboright") < GetLocalVarInt("combototal"))
            {
                // If we got something wrong, reset the checking values
                SetLocalVarInt("comboright", 0);
                SetLocalVarInt("combototal", 0);
                AddDebugMessage("comboright is less than combototal", false);
                
                // And kill the candles (timer due to trouble otherwise)
                // so player can try again.
                AddTimer("killcandles", 0.25f, "killcandles");
            }
        }
    }
}

void killcandles(string &in asTimer)
{
for(int i = 1; i <= 4 ; i++)
    {
        SetLampLit("candle_combo_" + i, false, false);
        AddDebugMessage("Turning lights off: candle_combo_"+i, false); //comment this out if you don't want massive spam xD -- or just delete it :D
    }
}
Since I have no scripting experience, I tend find debug messages a great way to learn the code alot faster. Big Grin

Thanks for writing up the script, Entih. I'll be sure to save it back... Never know when good stuff like this will come in handy. And also, as Soon said, it's good to stare at it and study it for awhile. Smile

*Edit - Forgot to add something. :/
10-04-2010, 09:35 AM
Find




Users browsing this thread: 3 Guest(s)