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
Turn on/off
eliasfrost Offline
Posting Freak

Posts: 1,769
Threads: 34
Joined: Mar 2007
Reputation: 39
#1
Turn on/off

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:
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:

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!

[Image: indiedb_88x31.png]
(This post was last modified: 10-18-2011, 12:09 PM by eliasfrost.)
10-18-2011, 11:46 AM
Find
Tanshaydar Offline
From Beyond

Posts: 3,085
Threads: 17
Joined: Mar 2009
Reputation: 67
#2
RE: Turn on/off

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

10-18-2011, 11:50 AM
Website Find
eliasfrost Offline
Posting Freak

Posts: 1,769
Threads: 34
Joined: Mar 2007
Reputation: 39
#3
RE: Turn on/off

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

[Image: indiedb_88x31.png]
10-18-2011, 11:52 AM
Find
eliasfrost Offline
Posting Freak

Posts: 1,769
Threads: 34
Joined: Mar 2007
Reputation: 39
#4
RE: Turn on/off

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:

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);
    }
}

[Image: indiedb_88x31.png]
10-18-2011, 07:16 PM
Find
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#5
RE: Turn on/off

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

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

Tutorials: From Noob to Pro
10-18-2011, 07:31 PM
Website Find
eliasfrost Offline
Posting Freak

Posts: 1,769
Threads: 34
Joined: Mar 2007
Reputation: 39
#6
RE: Turn on/off

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

[Image: indiedb_88x31.png]
10-18-2011, 07:35 PM
Find




Users browsing this thread: 2 Guest(s)