eliasfrost 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 1,769 
	Threads: 34 
	Joined: Mar 2007
	
 Reputation: 
39
		
	 | 
	
		
			
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.    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]](http://media.indiedb.com/images/global/indiedb_88x31.png)  
			
				
(This post was last modified: 10-18-2011, 12:09 PM by eliasfrost.)
 
				
			 
		 |  
	 
 | 
 
	| 10-18-2011, 11:46 AM  | 
	
		
	 | 
 
 
	
		
		Tanshaydar 
 
 
		
			From Beyond 
			
			
			
 
			
	Posts: 3,085 
	Threads: 17 
	Joined: Mar 2009
	
 Reputation: 
67
		
	 | 
	
		
			
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  | 
	
		
	 | 
 
 
	
		
		eliasfrost 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 1,769 
	Threads: 34 
	Joined: Mar 2007
	
 Reputation: 
39
		
	 | 
	
		
			
RE: Turn on/off 
			 
			
				>.< You're right, I totally forgot about the entity types. I'll see what I can do!   
			 
			
			
 
			
		 |  
	 
 | 
 
	| 10-18-2011, 11:52 AM  | 
	
		
	 | 
 
 
	
		
		eliasfrost 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 1,769 
	Threads: 34 
	Joined: Mar 2007
	
 Reputation: 
39
		
	 | 
	
		
			
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]](http://media.indiedb.com/images/global/indiedb_88x31.png)  
			
		 |  
	 
 | 
 
	| 10-18-2011, 07:16 PM  | 
	
		
	 | 
 
 
	
		
		Your Computer 
 
 
		
			SCAN ME! 
			
			
			
 
			
	Posts: 3,456 
	Threads: 32 
	Joined: Jul 2011
	
 Reputation: 
235
		
	 | 
	
		
			
RE: Turn on/off 
			 
			
				This: 
SetEntityPlayerInteractCallback("Radio_1", "func1", true);
 
Should be:
 SetEntityPlayerInteractCallback("Radio_1", "func1", false);
  
			 
			
			
 
			
		 |  
	 
 | 
 
	| 10-18-2011, 07:31 PM  | 
	
		
	 | 
 
 
	
		
		eliasfrost 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 1,769 
	Threads: 34 
	Joined: Mar 2007
	
 Reputation: 
39
		
	 | 
	
		
			
RE: Turn on/off 
			 
			
				Oh, herp. Always seem like I miss out on the small things xD Thanks a lot, once again, YouComputer!   
			 
			
			
 
			
		 |  
	 
 | 
 
	| 10-18-2011, 07:35 PM  | 
	
		
	 | 
 
 
	 
 |