Frictional Games Forum (read-only)
Turn on/off - 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: Turn on/off (/thread-10835.html)



Turn on/off - eliasfrost - 10-18-2011

So I made a radio and I want to make it so that when you press it, you turn it on and when you press it again, it'll turn off. But I'm not sure how this is gonna work. I have the idea that I have to use boolean values as flags for wether it's on or off but that's pretty much as far as I've gotten. I can turn it on, but I can't turn it off. I have two code samples, the first one is only for turning it on to work, and the second one is an attempt to create another interaction function to turn it off once it's on, but it failed miserably.

First code piece:
Code:
void OnStart()
{
SetLocalVarInt("radio", 0);
SetEntityPlayerInteractCallback("Radio_1", "func1", true);
}

void func1(string &in asEntity)
{
if(GetLocalVarInt("radio") == 0)
{

PlayMusic("42_mono.ogg", true, 1, 2, 1, true);
SetLocalVarInt("radio", 1);

}
}

And here's the second code:

Code:
void OnStart()
{
    SetLocalVarInt("radio", 0);
    SetEntityPlayerInteractCallback("Radio_1", "func1", true);
}

void func1(string &in asEntity)
{
    if(GetLocalVarInt("radio") == 0)
    {

        PlayMusic("42_mono.ogg", true, 1, 2, 1, true);
        SetLocalVarInt("radio", 1);
    }

    if(GetLocalVarInt("radio") == 1)
    {

        StopMusic(2, 1);
        SetLocalVarInt("radio", 0);

    }
}

I have a feeling that since the "radio" var is loaded to 0 at the start of the code, it will trigger the first if statement and turn the variable to 1 and then it directly moves down to the next if statement and trigger it, again changing the variable back to 0. I wonder if there is a way to cancel the reading process in the middle of a function? So that I can end the function after each if statement. This is just what I think, please tell me if I'm far off. Tongue Thanks

EDIT: Whoops! I accidentally put this in the wrong board, would a moderator be so kindly as to move it to the CS section? Thanks!


RE: Turn on/off - Tanshaydar - 10-18-2011

Why don't you make it a button entity? So you can use StateChange callbacks too.


RE: Turn on/off - eliasfrost - 10-18-2011

>.< You're right, I totally forgot about the entity types. I'll see what I can do! Tongue



RE: Turn on/off - eliasfrost - 10-18-2011

I've run into another problem, when I use the entity, it runs the script just like it should, but I can't interact with it again? What I want to do is to be able to switch song by clicking the radio, but I can only interact with it once. Here's my script, I thought the SetEntityInteractionDisabled("Radio_1", false); and ResetProp("Radio_1"); would fix this, but it didn't. >.<

Here's the code so far:

Code:
void OnStart()
{
    SetLocalVarInt("radio", 0);
    SetEntityPlayerInteractCallback("Radio_1", "func1", true);
}

void func1(string &in asEntity)
{
    if(GetLocalVarInt("radio") >= 0)
    {
        AddLocalVarInt("radio", 1);
        StopMusic(2, 1);
        playmusic();
    }
    SetEntityInteractionDisabled("Radio_1", false);
        ResetProp("Radio_1");
}

void playmusic()
{
    if(GetLocalVarInt("radio") == 0)
    {
        StopMusic(2, 1);
    }

    if(GetLocalVarInt("radio") == 1)
    {
        StopMusic(2, 1);
        PlayMusic("1.ogg", true, 1, 2, 1, false);
    }

    if(GetLocalVarInt("radio") == 2)
    {
        StopMusic(2, 1);
        PlayMusic("2.ogg", true, 1, 2, 1, false);
    }

    if(GetLocalVarInt("radio") == 3)
    {
        StopMusic(2, 1);
        PlayMusic("3.ogg", true, 1, 2, 1, false);
    }

    if(GetLocalVarInt("radio") == 4)
    {
        StopMusic(2, 1);
        PlayMusic("4.ogg", true, 1, 2, 1, false);
    }

    if(GetLocalVarInt("radio") == 5)
    {
        StopMusic(2, 1);
        PlayMusic("5.ogg", true, 1, 2, 1, false);
    }
}



RE: Turn on/off - Your Computer - 10-18-2011

This:
Code:
SetEntityPlayerInteractCallback("Radio_1", "func1", true);

Should be:
Code:
SetEntityPlayerInteractCallback("Radio_1", "func1", false);



RE: Turn on/off - eliasfrost - 10-18-2011

Oh, herp. Always seem like I miss out on the small things xD Thanks a lot, once again, YouComputer! Smile