Frictional Games Forum (read-only)
[Solved] Puzzle Script Problem? - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [Solved] Puzzle Script Problem? (/thread-25756.html)



[Solved] Puzzle Script Problem? - Vale - 07-27-2014

Alright, few things here.

First off is the problem I'm having.
Im creating a script for a puzzle in my custom story which works smoothly for the most part. The only error is in the fact that you can still move the pieces of the puzzle once it's completed, though the if()'s in the script should prevent that.
Here is the script below.

Spoiler below!

void OnStart()
{
AddEntityCollideCallback("Player", "script_shelf_fall", "func_shelf_fall", true, 1);
AddEntityCollideCallback("shelf_fall", "shelf_impact_area", "func_shelf_impact", true, 1);
FadeGlobalSoundVolume(0.00f, 0.01f);
AddTimer("", 5.0f, "fix_volume");
SetEntityPlayerInteractCallback("leverarea_1", "func_leverpuzzle1", false);
SetEntityPlayerInteractCallback("leverarea_2", "func_leverpuzzle2", false);
SetEntityPlayerInteractCallback("leverarea_3", "func_leverpuzzle3", false);
SetEntityPlayerInteractCallback("leverarea_4", "func_leverpuzzle4", false);
SetEntityPlayerInteractCallback("leverarea_5", "func_leverpuzzle5", false);
SetEntityPlayerInteractCallback("leverarea_6", "func_leverpuzzle6", false);
SetLocalVarInt("leverpuzzle_var", 2);
SetLocalVarInt("levers_functional", 1);
SetLocalVarInt("lever1_state", 2);
SetLocalVarInt("lever2_state", 1);
SetLocalVarInt("lever3_state", 2);
SetLocalVarInt("lever4_state", 2);
SetLocalVarInt("lever5_state", 2);
SetLocalVarInt("lever6_state", 1);
}
//This is where I add in callbacks for the script, blahblah
//main script below.
void func_leverpuzzle1(string &in asEntity)
{
if(GetLocalVarInt("levers_functional") == 0)
{

}
else if(GetLocalVarInt("levers_functional") == 1)
{
if(GetLocalVarInt("leverpuzzle_var") < 6)
{
PlayGuiSound("lock_door.snt", 1.0f);
switch(GetLocalVarInt("lever1_state"))
{
case 1:
SetLocalVarInt("lever1_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever1_up", true);
SetEntityActive("lever1_down", false);
break;
case 2:
SetLocalVarInt("lever1_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever1_down", true);
SetEntityActive("lever1_up", false);
break;
}
switch(GetLocalVarInt("lever3_state"))
{
case 1:
SetLocalVarInt("lever3_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever3_up", true);
SetEntityActive("lever3_down", false);
break;
case 2:
SetLocalVarInt("lever3_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever3_down", true);
SetEntityActive("lever3_up", false);
break;
}
switch(GetLocalVarInt("lever1_state"))
{
case 1:
SetLocalVarInt("lever5_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever5_up", true);
SetEntityActive("lever5_down", false);
break;
case 2:
SetLocalVarInt("lever5_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever5_down", true);
SetEntityActive("lever5_up", false);
break;

}
}
}
}
///////////
void func_leverpuzzle2(string &in asEntity)
{
if(GetLocalVarInt("levers_functional") == 0)
{

}
else if(GetLocalVarInt("levers_functional") == 1)
{
if(GetLocalVarInt("leverpuzzle_var") < 6)
{
PlayGuiSound("lock_door.snt", 1.0f);
switch(GetLocalVarInt("lever2_state"))
{
case 1:
SetLocalVarInt("lever2_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever2_up", true);
SetEntityActive("lever2_down", false);
break;
case 2:
SetLocalVarInt("lever2_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever2_down", true);
SetEntityActive("lever2_up", false);
break;
}
switch(GetLocalVarInt("lever3_state"))
{
case 1:
SetLocalVarInt("lever3_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever3_up", true);
SetEntityActive("lever3_down", false);
break;
case 2:
SetLocalVarInt("lever3_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever3_down", true);
SetEntityActive("lever3_up", false);
break;
}
switch(GetLocalVarInt("lever6_state"))
{
case 1:
SetLocalVarInt("lever6_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever6_up", true);
SetEntityActive("lever6_down", false);
break;
case 2:
SetLocalVarInt("lever6_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever6_down", true);
SetEntityActive("lever6_up", false);
break;
}
}
}
}
/////////
void func_leverpuzzle3(string &in asEntity)
{
if(GetLocalVarInt("levers_functional") == 0)
{

}
else if(GetLocalVarInt("levers_functional") == 1)
{
if(GetLocalVarInt("leverpuzzle_var") < 6)
{
PlayGuiSound("lock_door.snt", 1.0f);
switch(GetLocalVarInt("lever3_state"))
{
case 1:
SetLocalVarInt("lever3_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever3_up", true);
SetEntityActive("lever3_down", false);
break;
case 2:
SetLocalVarInt("lever3_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever3_down", true);
SetEntityActive("lever3_up", false);
break;
}
switch(GetLocalVarInt("lever4_state"))
{
case 1:
SetLocalVarInt("lever4_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever4_up", true);
SetEntityActive("lever4_down", false);
break;
case 2:
SetLocalVarInt("lever4_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever4_down", true);
SetEntityActive("lever4_up", false);
break;
}
switch(GetLocalVarInt("lever6_state"))
{
case 1:
SetLocalVarInt("lever6_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever6_up", true);
SetEntityActive("lever6_down", false);
break;
case 2:
SetLocalVarInt("lever6_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever6_down", true);
SetEntityActive("lever6_up", false);
break;
}
}
}
}
//////////////
void func_leverpuzzle4(string &in asEntity)
{
if(GetLocalVarInt("levers_functional") == 0)
{

}
else if(GetLocalVarInt("levers_functional") == 1)
{
if(GetLocalVarInt("leverpuzzle_var") < 6)
{
PlayGuiSound("lock_door.snt", 1.0f);
switch(GetLocalVarInt("lever4_state"))
{
case 1:
SetLocalVarInt("lever4_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever4_up", true);
SetEntityActive("lever4_down", false);
break;
case 2:
SetLocalVarInt("lever4_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever4_down", true);
SetEntityActive("lever4_up", false);
break;
}
switch(GetLocalVarInt("lever1_state"))
{
case 1:
SetLocalVarInt("lever1_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever1_up", true);
SetEntityActive("lever1_down", false);
break;
case 2:
SetLocalVarInt("lever1_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever1_down", true);
SetEntityActive("lever1_up", false);
break;
}
switch(GetLocalVarInt("lever2_state"))
{
case 1:
SetLocalVarInt("lever2_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever2_up", true);
SetEntityActive("lever2_down", false);
break;
case 2:
SetLocalVarInt("lever2_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever2_down", true);
SetEntityActive("lever2_up", false);
break;
}
}
}
}
////////////
void func_leverpuzzle5(string &in asEntity)
{
if(GetLocalVarInt("levers_functional") == 0)
{

}
else if(GetLocalVarInt("levers_functional") == 1)
{
if(GetLocalVarInt("leverpuzzle_var") < 6)
{
PlayGuiSound("lock_door.snt", 1.0f);
switch(GetLocalVarInt("lever5_state"))
{
case 1:
SetLocalVarInt("lever5_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever5_up", true);
SetEntityActive("lever5_down", false);
break;
case 2:
SetLocalVarInt("lever5_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever5_down", true);
SetEntityActive("lever5_up", false);
break;
}
switch(GetLocalVarInt("lever2_state"))
{
case 1:
SetLocalVarInt("lever2_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever2_up", true);
SetEntityActive("lever2_down", false);
break;
case 2:
SetLocalVarInt("lever2_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever2_down", true);
SetEntityActive("lever2_up", false);
break;
}
}
}
}
//////////
void func_leverpuzzle6(string &in asEntity)
{
if(GetLocalVarInt("levers_functional") == 0)
{

}
else if(GetLocalVarInt("levers_functional") == 1)
{
if(GetLocalVarInt("leverpuzzle_var") < 6)
{
PlayGuiSound("lock_door.snt", 1.0f);
switch(GetLocalVarInt("lever6_state"))
{
case 1:
SetLocalVarInt("lever6_state", 2);
AddLocalVarInt("leverpuzzle_var", -1);
SetEntityActive("lever6_up", true);
SetEntityActive("lever6_down", false);
break;
case 2:
SetLocalVarInt("lever6_state", 1);
AddLocalVarInt("leverpuzzle_var", 1);
SetEntityActive("lever6_down", true);
SetEntityActive("lever6_up", false);
break;
}
}
}
}
/////////////

The puzzle involves pressing levers into their slots, which will toggle the position of certain other levers. When they're all pushed down, the variable "leverpuzzle_var" should equal 6 (for 6 switches pressed.) However, theres either an error in control flow or with the variable, as I can still toggle the levers after I push all 6 in.
Anyone see the problem here?
Oh, and the if "levers_functional" is for another part I'm adding later, but for now its set to be true (aka '1') as default, so Ignore that.

Oh, and secondly. When theyre all pushed in I want to have another function run. What is the best way of doing that?

THANKS


RE: Puzzle Script Problem? - Daemian - 07-27-2014

Humm. Checking...

I think you should monitor the value of levers_functional
Code:
AddDebugMessage( "leverpuzzle_var("+leverpuzzle_var+")", false);
Those levers may be increasing the value of that variable several times cause you're using SetEntityPlayerInteractCallback to trigger the function instead of a callback that detects only when the state changes.
If I'm correct, you should see that variable getting too big by the time you finished the puzzle, so it's always > than 6 and the code keeps on.


RE: Puzzle Script Problem? - Vale - 07-27-2014

Mmm. You misunderstood, and I underdescribed. It uses examine areas for interaction to toggle their states, because they aren't quite... levers. However, I will take your advice and monitor the value of the variable. Thanks for your time.


RE: Puzzle Script Problem? - Daemian - 07-27-2014

Oh. Ok. Yeah you should still check what's happening with that var.
Can't you disable a lever area after the player was done with it? Or you need it for later?


RE: Puzzle Script Problem? - PutraenusAlivius - 07-27-2014

(07-27-2014, 05:09 AM)valetheimpaler Wrote: Mmm. You misunderstood, and I underdescribed. It uses examine areas for interaction to toggle their states, because they aren't quite... levers. However, I will take your advice and monitor the value of the variable. Thanks for your time.
You could just deactivate the examine area when you're done...

(07-27-2014, 02:07 AM)valetheimpaler Wrote: Oh, and secondly. When they're all pushed in I want to have another function run. What is the best way of doing that?

Call a new function. Name the function whatever you want and then after that put (); (refer to first code below). After that your function is basically done. For the callback, use the same function line but don't use the semi colon (refer to second code below). Below is an example (refer to last code).

Spoiler below!

FIRST
PHP Code:
FUNCTIONNAME(); 

SECOND
PHP Code:
void FUNCTIONNAME()
{
///whatever you want to do here let it be spawning a monster or a dildo


THIRD
PHP Code:
void OnStart()
{
ExecCustFunc();
}

void ExecCustFunc()
{
//Execute shit!





RE: Puzzle Script Problem? - Mudbill - 07-27-2014

A script as big as that would be much easier to read if it was properly indented and formatted. Perhaps provide a http://www.pastebin.com link?


RE: Puzzle Script Problem? - Vale - 07-27-2014

Alright first captain, that answers my second question. Thank you.
http://pastebin.com/SFP0z0Hx
There's a link to the code.
And I'll post a video so you can understand what the puzzle is exactly.

holy jeezus, I figured it out. Theres one tiny error in my code, which originates from me copying and pasting that switch piece and inputting new numbers for each.
Look at the switch after line 65 in the pastebin and you can probably see whats wrong.
When I was trying to get a video of the puzzle I managed to get it to stop while one lever was still up, so I figured that there was an error along those lines.
Thanks again, and sorry for the trouble!