SA-MP Forums Archive
[Tutorial] Button Controllable Gates! - 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Button Controllable Gates! (/showthread.php?tid=248118)



Button Controllable Gates! - !Easy! - 12.04.2011

Button Controllable Gates

I got the idea from Raven's Roleplay, some of the gates there can be controlled using a button (thanks to CuervO for
the idea). Today, we will create our very own gates, that can be simply controlled by a button!

We will be making a simple Police gate (for beginners creating RP modes).

Put this at the top of your script:

pawn Код:
new pdgate; // Creates a new variable 'pdgate'.
new bool:pdgateopened; // A boolean to check if 'pdgate' is opened.
Then, put this under OnGameModeInit or OnFilterScriptInit:

pawn Код:
pdgate = CreateObject(971, 1588.965698, -1637.882690, 15.260185, 0.0000, 0.0000, 180.0000);
What does that do? That creates an object (971) and sets the variable of 'pdgate' to the id of the object (CreateObject returns the ID of the object, not the model) at the following co-ordinates:

Код:
1588.965698, -1637.882690, 15.260185
And with the following rotation co-ordinates:

Код:
0.0000, 0.0000, 180.0000
Easy, almost done, now, put this under OnPlayerKeyStateChange:

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_LOOK_BEHIND) // KEY_LOOK_BEHIND is MMB (middle mouse button).
    {
        if(IsPlayerInRangeOfPoint(playerid, 8.0, 1588.965698, -1637.882690, 15.260185)) // Checks if the player is in the area of the gate.
        {
            if(pdgateopened == false) // if pdgateopened is 'false'.
            {
                MoveObject(pdgate, 1588.965698, -1637.882690, 9.260185); // Moves the object.
                pdgateopened = true; // Sets the 'pdgateopened' variable to 'true' which is equivalent to 1.
                return 1;
            }
            else // If pdgateopened is 'true'.
            {
                 MoveObject(pdgate, 1588.965698, -1637.882690, 15.260185); // Move the object back to their old co-ordinates.
                 pdgateopened = false; // Sets the 'pdgateopened' variable to 'false' which is 0.
                 return 1;
            }
        }
    }
    return 1;
}
What does that code do?

OnPlayerKeyStateChange is a server-sided callback, this is only called when a player presses a key detected by the client. We are checking if the player's current key (newkeys) is 'KEY_LOOK_BEHIND' (which is MMB, middle mouse button), the '&' character is the same thing as '==' but different (no time to explain), then this will check if the player is near the co-ordinates of our gate, then checks if 'pdgateopened' is 0 (false), if so, move the object and set 'pdgateopened' to 1 (true). If the gate is opened (else), move the object back to their old co-ordinates and set 'pdgateopened' back to 0 (false).

And that's it!

Enjoy your gate!


Re: Button Controllable Gates! - Anastasia - 12.04.2011

Nice Work.


Re: Button Controllable Gates! - wheelman_WM - 12.04.2011

awesome


Re: Button Controllable Gates! - BASITJALIL - 12.04.2011

Its nice


Re: Button Controllable Gates! - CodeExtreme - 21.05.2011

God Job


Re: Button Controllable Gates! - GangsTa_ - 21.05.2011

A such tutorial was made like 2-3 times already..


Re: Button Controllable Gates! - Jantjuh - 21.05.2011

Great!


Re: Button Controllable Gates! - jonnyboy - 21.05.2011

very handy!