[Tutorial] Automatic gates
#1

Yes, it has been done many times before, but I made this for someone who kept bugging me about it on Skype. Perhaps somebody else could use it as well?

Introduction
Automatic gates are common on quite a few servers, for example roleplay or Cops N' Robbers. (Most roleplay servers are Cops N' Robbers by the way)
This tutorial should cover the basics on creating a simple automatic gate. Any questions, comments, suggest and/or complaints can be posted in this topic.

PS: If you don't know what an automatic gate is, it's a gate that automatically opens when a player is near.
PPS: Some values in this tutorial (coordinates) are imaginary and are just for showing what the heck I'm talking about, I don't recommend using them for your actual gate.

Getting started
Before we start, we first need the coordinates of our gate. What I've stated below should do the trick:
  • 1. Open up some map editor and spawn a gate.
    2. Move the gate to the position of where you want it to be when the gate is closed, retrieve it's position and rotation (You usually double-click the gate) and note it somewhere (Note to get the gate ID as well)
    3. Now move the gate to where you want it to be when the gate is open so that vehicles, people, ducks and goats can pass. Now, you only need to retrieve the position and note it somewhere. The rotation angle and the object ID aren't important anymore.

    Noted that? Now we can start!

    Here's how I'll be doing my OWN gate first:

    (The values on the left are y coordinates and the values on the top are x coordinates)

    Let's say I've got a gate which I want to move from (8.0, 3.0) (x, y) to (32.0, 12.0), and for my gate I'll be using object ID 16442 which, indeed, is a cow. My Z position is constantly 10.0

    The gate
    Okay, somewhere on-top of your script you define two new variables. One will contain the gate object id, and the other will be a boolean which will tell us whether the gate is opened or not:
    pawn Код:
    new gate;
    new bool:gateopen;
    Our next step is creating a timer which will call a custom callback which we still need to create. We're adding this timer under either OnFilterScriptInit or OnGameModeInit, dependent on whether you're using this in a filterscript or your main gamemode. We'll be calling the callback in this tutorial 'GateCheck'.
    Because we're using On(...)Init anyways we'll also immediately create the gate. Fill in your correct coordinates (Don't copy mine, they won't work):
    pawn Код:
    public OnFilterScriptInit() // Alternatively, use OnGameModeInit if you implement it in your gamemode
    {
      SetTimer("GateCheck", 800, true); //This is used to create the timer. The "GateCheck" is the callback we'll be using,
      // the '800' is the amount of milliseconds between each call and the 'true' indicates that this timer is looping endlessly
      gate = CreateObject(16442, 8.0, 3.0, 10.0, 0.0, 0.0, 0.0);
      //'gate = CreateObject(...);' = Assigns the object id of the gate to the 'gate' variable
      //16442 = object model id < change it to the model id you use for your gate
      // 8.0, 3.0, 10.0 = coordinates of the gate
      // 0.0, 0.0, 0.0 = Rotation of the gate. None in this case.
      return 1;
    }
    You may want to change the timer interval from 800 milliseconds to something else. Keep in mind: 1000 ms (milliseconds) equals 1 second.

    Okay, now we've created both the gate and the timer, but there's no callback to handle anything at all!
    To create a callback, we first must create/forward it. It works like this:
    pawn Код:
    //DO NOT COPY THIS INTO YOUR SCRIPT (Don't copy anything straight away by the way)
    forward Callback(parameters);
    public Callback(parameters)
    {
      return 1;
    }
    That's how you create a callback. We now must make a callback for the automatic gate which'll be called thanks to the timer (Already did that part). Now our callback doesn't use any parameters at all, so we'll go with this (You can copy this):
    pawn Код:
    forward GateCheck();
    public GateCheck()
    {
      return 1;
    }
    Put this anywhere in your script, as long as it's under the '#include <a_samp>' line and outside of another callback/method.

    Now we can finally start doing something. That is, if you're not asleep yet. Which I bet you are.

    So here's the deal: We're going to loop through all players, check if anyone is near the gate and if so, open it (If it's not open already). We also need to kill the loop then. If nobody has been found, we'll just close it again (If it's open).

    pawn Код:
    forward GateCheck();
    public GateCheck()
    {
      for(new i; i < MAX_PLAYERS; i++) // Start the loop
      {
        if(IsPlayerInRangeOfPoint(i, 15.0, 8.0, 3.0, 10.0)) //Check if any player is in the area. Replace '8.0, 3.0, 10.0' with your own coordinates of your closed gate.
        {
          if(gateopen == false) // If the gate isn't open...
          {
            MoveObject(gate, 32.0, 12.0, 10.0, 3.5); //Then open it! Change '32.0, 12.0, 10.0' to the coordinates of your opened gate.
            gateopen = true; // Setting this to true indicates it's open(ing)
          }
          return; //This closes the callback
        }
      }
      //This is called if nobody has been found near the gate. Obviously, because 'return' would fully close the function and this wouldn't be used then.
      if(gateopen == true) //If the gate IS open, but there's no one near..
      {
        MoveObject(gate, 8.0, 3.0, 10.0, 3.5); // Change the '8.0, 3.0, 10.0' to the coordinates of your gate when it's closed.
        gateopen = false; //This indicates the gate is closed again. Or at least, closing.
      }
    }
    That should do the trick. If you think your gate is too slow (or too fast), change the last parameter at MoveObject: Change '3.5' to something different. Experiment a bit with it if you like.

    Now you've got what you should need to create an automatic gate. If you find bugs or have a comment/suggestion/complaint/big belly, feel free to post here and I'll help you out.

    I also uploaded the full script on pastebin: http://pastebin.com/b60m0rdU

    Do NOT PM me.

    Now I'm tired as heck and I've got a headache, see ya
Reply
#2

Nice tutorial .
Reply
#3

Nice tutorial cow :P Indent your code !!
Reply
#4

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
Nice tutorial cow :P Indent your code !!
It is lol

Thanks btw
Reply
#5

http://dracoblue.net/tidy/pawn/ ftw.
Reply
#6

Nice tutorial!
Finally somebody who can EXPLAIN
Reply
#7

niec
Reply
#8

nice tutorial
Reply
#9

You made a little mistake in this line

pawn Код:
if(IsPlayerInRangeOfPoint(playerid, 15.0, 8.0, 3.0, 10.0))
Good tutorial
Reply
#10

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
Nice tutorial cow :P Indent your code !!
Quote:
Originally Posted by Darnell
Посмотреть сообщение
It's good indented, but with 2 spaces instead of 4. There's nothing bad, I used to indent with 2 spaces too :P.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)