SA-MP Forums Archive
OnKeyStateChange? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: OnKeyStateChange? (/showthread.php?tid=620153)



OnKeyStateChange? - MrCesar - 26.10.2016

Hello guys, how can i make an object to move if i hit for eg 'Y' near it? aka opening/closing gates etc...
thanks alot


Re: OnKeyStateChange? - iLearner - 26.10.2016

Example script for a gate:

PHP код:

new Gate, Bool:Open;
ongminit()
{ 
    
//create obj on gminit
    
Gate = CreateDynamicObject(971,1033.2990000,-363.1990000,76.5000000,0.0000000,0.0000000,329.9960000);
}
public 
OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(
PRESSED(KEY_NO)) // key N
    
{
        if(
IsPlayerInRangeOfPoint(playerid,3,1037.6825,-366.1670,73.8657) || IsPlayerInRangeOfPoint(playerid,3,1028.8965,-360.5697,73.9318)) //Gate 1
        
{
            switch(
Open) //Check whether the gate opened or closed
            
{
                case 
true: //the gate opened
                
{
                    
MoveDynamicObject(Gate,1033.2990000,-363.1990000,76.5000000,3.5,0.0000000,0.0000000,329.9960000); //Close the gate
                    
Open = false; //false for closed
                
}
                case 
false: //If the gate closed
                
{
                    
MoveDynamicObject(Gate,1028.880,-365.665,76.500,3.5,0.000,0.000,-98.703); //Open the gate
                    
Open = true; //true for opened
                
}
            }
        }
    }
 return 
1;
} 



Re: OnKeyStateChange? - MrCesar - 27.10.2016

fixed; how can I add an automatic timer to close the gate?


Re: OnKeyStateChange? - lackmail - 27.10.2016

Quote:
Originally Posted by MrCesar
Посмотреть сообщение
fixed; how can I add an automatic timer to close the gate?
Код:
SetTimer("CloseGate", 10000, false); // put this line after gate opening code it sets a 10 sec timer you can increase or decrease the time

forward CloseGate();// when 10 sec completed 
public CloseGate()
{
       //put gate closing code here
}



Re: OnKeyStateChange? - iLearner - 27.10.2016

This will close the gate after 20 seconds if its still open.

PHP код:
 case false: //If the gate closed 
                
{ 
                    
MoveDynamicObject(Gate,1028.880,-365.665,76.500,3.5,0.000,0.000,-98.703); //Open the gate 
                    
Open = true; //true for opened 
                    
SetTimer("closegate", 20000, false);
                } 
// the function
forward closegate();
public 
closegate()
{
// if the gate is open
   
if(Open == true)
   {
       
MoveDynamicObject(Gate,1033.2990000,-363.1990000,76.5000000,3.5,0.0000000,0.0000000,329.9960000);       //Close the gate 
      
Open = false; //false for closed 
      
return 1;
  }
} 



Re: OnKeyStateChange? - MrCesar - 27.10.2016

Quote:
Originally Posted by lackmail
Посмотреть сообщение
Код:
SetTimer("CloseGate", 10000, false); // put this line after gate opening code it sets a 10 sec timer you can increase or decrease the time

forward CloseGate();// when 10 sec completed 
public CloseGate()
{
       //put gate closing code here
}
Quote:
Originally Posted by iLearner
Посмотреть сообщение
This will close the gate after 20 seconds if its still open.

PHP код:
 case false: //If the gate closed 
                
{ 
                    
MoveDynamicObject(Gate,1028.880,-365.665,76.500,3.5,0.000,0.000,-98.703); //Open the gate 
                    
Open = true; //true for opened 
                    
SetTimer("closegate", 20000, false);
                } 
// the function
forward closegate();
public 
closegate()
{
// if the gate is open
   
if(Open == true)
   {
       
MoveDynamicObject(Gate,1033.2990000,-363.1990000,76.5000000,3.5,0.0000000,0.0000000,329.9960000);       //Close the gate 
      
Open = false; //false for closed 
      
return 1;
  }
} 
thanks alot!