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
Script Help Stuck at a lever script
Soskot Offline
Junior Member

Posts: 4
Threads: 1
Joined: Mar 2013
Reputation: 0
#1
Stuck at a lever script

Greetings community of Frictional Games.
This is my first post at the forum and I'm currently working on a custom story together with three other guys for a school project.
And I am doing the scripting-part of the project and I got stuck with a problem, I've tried searching for help, but didn't find any.

Explanation of the puzzle:
I have three simple levers, and I want a certain combination for it to work, for example 1 up and 2 down. I've got so far that I made it work, and the lights above the levers gets lit, the problem is that I can just do it up & down on every lever and then the puzzle is solved.
I need the variable to reset to 0 whenever the lever is pulled to the wrong alState, but it doesn't.
I think I know what I have to do to solve this problem, but the problem is that it doesn't work as I've coded it.
Here's the script:
//Variables
int lev1;
int lev2;
int lev3;
int glev1;
int glev2;
int glev3;
int blev1;
int blev2;
int blev3;
int RedEnabled = 0;
int BlueEnabled = 0;
int GreenEnabled = 0;

void OnStart()
{
PlayMusic("music_threelights.ogg", true, 1.0f, 0, 0, true);
GiveItemFromFile("lantern", "lantern.ent");
GiveItemFromFile("tinderbox", "tinderbox.ent");
//Red Lights
SetEntityConnectionStateChangeCallback("lever_simple01_3", "func_red1");
SetEntityConnectionStateChangeCallback("lever_simple01_5", "func_red2");
SetEntityConnectionStateChangeCallback("lever_simple01_6", "func_red3");
//Green Lights
SetEntityConnectionStateChangeCallback("lever_simple01_7", "func_green1");
SetEntityConnectionStateChangeCallback("lever_simple01_8", "func_green2");
SetEntityConnectionStateChangeCallback("lever_simple01_9", "func_green3");
//Blue Lights
SetEntityConnectionStateChangeCallback("lever_simple01_1", "func_blue1");
SetEntityConnectionStateChangeCallback("lever_simple01_2", "func_blue2");
SetEntityConnectionStateChangeCallback("lever_simple01_4", "func_blue3");
}
void CheckDoorUnlocker()
{
if (RedEnabled == 1 && BlueEnabled == 1 && GreenEnabled == 1)
{
SetSwingDoorLocked("metal_1", false, true);
AddPlayerSanity(25);
}
}
void CheckVariablesForBlue()
{
if (blev1 == 1 && blev2 == 1 && blev3 == 1)
{
SetLampLit("torch_custom_blue_1", true, true);
SetLampLit("torch_custom_blue_2", true, true);
SetLampLit("torch_custom_blue_3", true, true);
SetLampLit("torch_custom_blue_4", true, true);
BlueEnabled = 1;
CheckDoorUnlocker();
}
}
void func_blue1(string &in asEntity, int alState)
{
if (alState == 1)
{
blev1 = 1;
}
if (alState != 1)
{
alState = 0;
}
CheckVariablesForBlue();
}
void func_blue2(string &in asEntity, int alState)
{
if (alState == 1)
{
blev2 = 1;
}
if (alState != 1)
{
alState = 0;
}
CheckVariablesForBlue();
}
void func_blue3(string &in asEntity, int alState)
{
if (alState == 1)
{
blev3 = 1;
}
if (alState != 1)
{
alState = 0;
}

CheckVariablesForBlue();
}

I have tried setting the variables instead of alState to 0 aswell, but it didn't work.

If you had trouble understanding something, please tell me and I'll try to explain it better.

Any help is appreciated!

And btw, it feels great to be a part of the community!

Regards,
Soskot
(This post was last modified: 03-28-2013, 04:38 PM by Soskot.)
03-28-2013, 04:23 PM
Find Reply
NaxEla Offline
Senior Member

Posts: 415
Threads: 5
Joined: Dec 2012
Reputation: 28
#2
RE: Stuck at a lever script

Ok, you're making things too complicated. All you need to do is this: create a state change callback for each lever and make them call the same function. Every time a lever changes state, it will check whether each lever is in the correct position, and if all three are correct, it will do something.

Here's a script for it:
PHP Code: (Select All)
void OnStart()
{
    
SetEntityConnectionStateChangeCallback("lever_1""LeverStateChange");
    
SetEntityConnectionStateChangeCallback("lever_2""LeverStateChange");
    
SetEntityConnectionStateChangeCallback("lever_3""LeverStateChange");
}

void LeverStateChange(string &in entityint state)
{
    if((
GetLeverState("lever_1") == 1) && (GetLeverState("lever_2") == -1) && (GetLeverState("lever_3") == -1)) {
        
// Do something
    
}


In Ruins [WIP]
03-28-2013, 05:20 PM
Find Reply
Soskot Offline
Junior Member

Posts: 4
Threads: 1
Joined: Mar 2013
Reputation: 0
#3
RE: Stuck at a lever script

Thanks for the reply.
Will this also reset the state if I do the wrong combination, so I just can't do up and down on all levers and the puzzle is solved?

Edit: After testing it out, I noticed that the puzzle doesn't even work now.
The lamps doesn't lit whenever their states are as they should.

void LeverStateChange(string &in entity, int state)
{
    if((GetLeverState("lever_small01_1") == 1) && (GetLeverState("lever_small01_2") == -1) && (GetLeverState("lever_small01_3") == -1)) {
        SetLampLit("torch_custom_blue_1", true, true);
SetLampLit("torch_custom_blue_2", true, true);
SetLampLit("torch_custom_blue_3", true, true);
SetLampLit("torch_custom_blue_4", true, true);

    }
}
(This post was last modified: 03-28-2013, 06:26 PM by Soskot.)
03-28-2013, 05:54 PM
Find Reply
Soskot Offline
Junior Member

Posts: 4
Threads: 1
Joined: Mar 2013
Reputation: 0
#4
RE: Stuck at a lever script

bump, why isn't it working? :o
04-08-2013, 05:21 PM
Find Reply
The chaser Offline
Posting Freak

Posts: 2,486
Threads: 76
Joined: Jun 2012
Reputation: 113
#5
RE: Stuck at a lever script

(03-28-2013, 05:20 PM)NaxEla Wrote: Ok, you're making things too complicated. All you need to do is this: create a state change callback for each lever and make them call the same function. Every time a lever changes state, it will check whether each lever is in the correct position, and if all three are correct, it will do something.

Here's a script for it:
PHP Code: (Select All)
void OnStart()
{
    
SetEntityConnectionStateChangeCallback("lever_1""LeverStateChange");
    
SetEntityConnectionStateChangeCallback("lever_2""LeverStateChange");
    
SetEntityConnectionStateChangeCallback("lever_3""LeverStateChange");
}

void LeverStateChange(string &in asEntityint alState)
{
    if((
GetLeverState("lever_1") == 1) && (GetLeverState("lever_2") == -1) && (GetLeverState("lever_3") == -1)) {
        
// Do something
    
}


Fixed. There was "int state" instead "int alState".
Try if it works now, Soskot Wink

THE OTHERWORLD (WIP)
[Image: k6vbdhu]

Aculy iz dolan.
(This post was last modified: 04-08-2013, 06:01 PM by The chaser.)
04-08-2013, 06:01 PM
Find Reply
NaxEla Offline
Senior Member

Posts: 415
Threads: 5
Joined: Dec 2012
Reputation: 28
#6
RE: Stuck at a lever script

(04-08-2013, 06:01 PM)The chaser Wrote:
(03-28-2013, 05:20 PM)NaxEla Wrote: Ok, you're making things too complicated. All you need to do is this: create a state change callback for each lever and make them call the same function. Every time a lever changes state, it will check whether each lever is in the correct position, and if all three are correct, it will do something.

Here's a script for it:
PHP Code: (Select All)
void OnStart()
{
    
SetEntityConnectionStateChangeCallback("lever_1""LeverStateChange");
    
SetEntityConnectionStateChangeCallback("lever_2""LeverStateChange");
    
SetEntityConnectionStateChangeCallback("lever_3""LeverStateChange");
}

void LeverStateChange(string &in asEntityint alState)
{
    if((
GetLeverState("lever_1") == 1) && (GetLeverState("lever_2") == -1) && (GetLeverState("lever_3") == -1)) {
        
// Do something
    
}


Fixed. There was "int state" instead "int alState".
Try if it works now, Soskot Wink

It actually doesn't matter what it's called :p. Its just the variable name

In Ruins [WIP]
04-08-2013, 06:05 PM
Find Reply
The chaser Offline
Posting Freak

Posts: 2,486
Threads: 76
Joined: Jun 2012
Reputation: 113
#7
RE: Stuck at a lever script

It's usually "alState" not "state".

I think that it would be better for everyone to use the "common" language for the support, and the "custom" for stories, where no help is needed.

Just my thoughts Wink

Could you Soskot please provide us with your actual script?

THE OTHERWORLD (WIP)
[Image: k6vbdhu]

Aculy iz dolan.
(This post was last modified: 04-08-2013, 06:09 PM by The chaser.)
04-08-2013, 06:08 PM
Find Reply
Soskot Offline
Junior Member

Posts: 4
Threads: 1
Joined: Mar 2013
Reputation: 0
#8
RE: Stuck at a lever script

(04-08-2013, 06:08 PM)The chaser Wrote: It's usually "alState" not "state".



I think that it would be better for everyone to use the "common" language for the support, and the "custom" for stories, where no help is needed.



Just my thoughts Wink



Could you Soskot please provide us with your actual script?
Hey The Chaser and thanks for your reply, although I actually managed to solve this problem in another way. So I don't need the help you were going to provide, but thank you anyways Smile
04-08-2013, 06:39 PM
Find Reply
The chaser Offline
Posting Freak

Posts: 2,486
Threads: 76
Joined: Jun 2012
Reputation: 113
#9
RE: Stuck at a lever script

I see. It has been a pleasure anyways, you seem like a good dude Wink

THE OTHERWORLD (WIP)
[Image: k6vbdhu]

Aculy iz dolan.
04-08-2013, 06:55 PM
Find Reply
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#10
RE: Stuck at a lever script

(Note: this is somewhat off-topic.)

About the variable names: yeah, it's true that they can be whatever you like, on the other hand, it is also true that most people will find it confusing if you don't use the "standard" names (the ones from the engine script page), since this has long been somewhat misunderstood on the forum.

However, when scripting your custom story, giving descriptive ("custom") names to your variables is quite important as it helps you write more meaningful, readable, maintainable, and ultimately less buggy code, so this outweighs the needs of the forum. 'Cause, while for small code snippets, it might be feasible to rename variables when posting in such a way that the forum users might find the code less confusing, you simply can't do this every time, and for longer code snippets, and of course, when you get help, you then need to integrate it back into your own code, which might introduce accidental errors.

So, I guess the forum users will have to gradually become apt at reading the code that has variables with custom names; and this will probably become even more true when the next game by frictional is released.

In essence, when helping, realize that the relationships and interactions of the various parts of the code are more important than the actual variable names (unless, of course, there's a spelling error, in which case the code might not compile at all, or if variable names used are so cryptic no one can make sense of them.)

But back to the "standard" names. I want to explain what all those prefixes mean.
It's a variation on what's known as "Hungarian notation", which is basically just a variable naming convention some programers follow. It works by prefixing the variable name with a mnemonic or two which carry additional information about the variable, most frequently, information about it's type.

I personally don't like it too much, but it is justifiable to use it for Amnesia scripting - if you want to - since there's no specialized IDE which can do things like tell you the type of the variable when you hover your mouse over it, and similar.
This is especially convenient if you have long functions, where you might use their parameter variables somewhere not close to the "function header", so if you want to do something with it, you might need to know its type first, and without those mnemonics, you might be forced to constantly scroll up and down, which is a pain.

So, here goes:
Most of those variable names are probably copied directly from the internal C++ implementations. C++ programmers often make heir own types, and those who use Hungarian notation prefix type names (class names) with a "c", which is short for "class", while instances (variables) of those types are prefixed with "a", which pretty much has the same meaning as in these English phrases: "a tree", "a car", "a bird", "a person" - it's used to denote a concrete variable of some type (class). There are some other prefixes, but let's stick to just these.

Now, for variables of predefined primitive types (like int, float, bool, etc...) and strings, it is often convenient to encode type information into variable names as well.
So, frictional games used:
b - for bool,
f - for float,
s - for string, and
l (yeah, lowercase L...) - for int.

Why "l", you might ask. Well, "i" was used for something else ("inteface"), but there is another integer type called long, which is similar to (or in some cases same as) int. So, "l" here really denotes an integer.

Then they combined the "a" from above with one of the type mnemonics, followed a descriptive name of the variable, so you got something like: abSomeName (a boolean variable, described by "SomeName").

Now, since in Amnesia scripting you normally don't create your own types, the "a" probably seems somewhat redundant, but, as I've said, the function signatures were probably lifted straight off the C++ code, with minor alterations.

Take a look at these:
PHP Code: (Select All)
float RandFloat(float afMinfloat afMax);
int RandInt(int alMinint alMax);
bool StringContains(stringasStringstringasSubString);
void FadeInSound(stringasSoundNamefloat afFadeTimebool abPlayStart); 
So, if somewhere in your code (or maybe in the script of the Amnesia's own levels) you see a name which starts with an "a", you immediately know (assuming that the scripter adhered to the convention, and is not messing with you) that it's a variable, and not something else. If you see that it starts with "af", then it's a float variable, and if it's starts with "as" then it's a string variable, etc.
(This post was last modified: 04-08-2013, 09:34 PM by TheGreatCthulhu.)
04-08-2013, 09:31 PM
Find Reply




Users browsing this thread: 1 Guest(s)