Log in to reply

Needing help on a tiny script to open barrier arm gates.

  • Hey all,

    Been working on a tiny script to see if it was possible to toggle the position of the barrier arm gates (you know the red and white striped barrier gates at toll booths and the bigger ones like at the prison).

    With my script, I've been able to detect the entity/prop (depending on how I write it, it has the same outcome) and tell it to open with the _door_control native. Opening works no problems. Mixed reactions on closing though.

    For instance, when facing the prison, the right fragbarrierarmcustomcontrol opens and after 10 seconds, closes. Works fine. Try the same thing for the barrier arm on the exit side and it opens but will not close (with the exception if I open simple trainer and enter either ped spawn menu or teleport menu; actually if I toggle any item that uses a UI).

    Then the toll booth arms will just stay open. Same for the above when it closes. On top of it all, I can drive about 100 in game units away (meters? maybe 80 meters) and either a script resets to the close position or the Lod resets it.

    Trying to mess with the doortuning meta didn't seem to do anything (only tried update and common rpf - wonder if one in another location has a better reaction). Got to decrypting the doortuning ymt but after editing and reimporting it just crashes the game.

    Would like to approach this by script while avoiding having to edit any files but if thats necessary then I understand. Another note, I'm not on 2060 until more mods get updated but am running a build of 1868 (.0 I believe).

    Here's the code I've written (obviously its just a snippet, nothing in the ontick):

    private void onKeyDown(object sender, KeyEventArgs e)
        if (e.KeyCode == Keys.E)
            Ped playerPed = Game.Player.Character;
            Entity[] barrier_Gates = World.GetNearbyProps(playerPed.Position, 5f, Function.Call<int>(Hash.GET_HASH_KEY, "prop_sec_barrier_ld_02a"), Function.Call<int>(Hash.GET_HASH_KEY, "prop_sec_barrier_ld_01a"));
            for (int i = 0; i < barrier_Gates.Length; i++)
                float get_dist = Function.Call<float>(Hash.GET_DISTANCE_BETWEEN_COORDS, barrier_Gates[i].Position.X, barrier_Gates[i].Position.Y, barrier_Gates[i].Position.Z, playerPed.Position.X, playerPed.Position.Y, playerPed.Position.Z, false);
                if (playerPed.IsSittingInVehicle() && get_dist <= 10.0)
                    Function.Call(Hash._DOOR_CONTROL, barrier_Gates[i].Model.Hash, barrier_Gates[i].Position.X, barrier_Gates[i].Position.Y, barrier_Gates[i].Position.Z, 0, 0.0, 0.0, 1.0);
                    Function.Call(Hash._DOOR_CONTROL, barrier_Gates[i].Model.Hash, barrier_Gates[i].Position.X, barrier_Gates[i].Position.Y, barrier_Gates[i].Position.Z, 1, 0.0, 0.0, 0.0);                

  • Have you checked how R* does it in their scripts?

  • Not sure where or how to check that. Besides, I feel like the vanilla behavior either breaks the arm or nudges the arm up as the vehicle forces its way through and then it just stays up? So maybe, they're not supposed to come back down? I don't know.

    I suppose I would like to know what files to dig through to analyze? I've checked the doortuning meta in the update and common rpf (is there another one I need to check) or is there a script I need to find?

  • @dimedius For example you could make a simple program, looking through all the files mcal9909 linked to above.
    Since "_DOOR_CONTROL" and "prop_sec_barrier_ld_02a" probably are only used in a few scripts, you'll narrow down the potential interesting files.
    Write all occurrences (lines with your search word in them) together with the script name to a text file (row number and occurrence count as well ofc if you want).
    Then open your created text file, find out which scripts that seems interesting and simply open them and CTRL+F "_DOOR_CONTROL" and "prop_sec_barrier_ld_02a".

    That's one way of doing it.

  • Thanks mcal9909 and R3QQ! I'll do some digging asap!

    Edit: Didn't want to bring this back to the front back but did want to add an update.

    While testing another script and driving around, I went by the RR gates when the train was passing. It's a little buggy too where it doesn't close when the train goes by so either a mod I'm using is having a strange side effect (I don't have any that intentionally affects doors or gates).

    Though, in my vanilla unmodded online version, the barriers appear to be stronger and nudge up as you roll your vehicle through slowly. But in the doortuning meta there are flags for being SP or MP vehicle so maybe they behave differently. Bottom line seems to be the native way is a bit buggy.

  • @dimedius Fyi, it is 100% necessary to also edit the doortuning.ymt if you are making edits to the meta or the edits will not work. I noticed you mentioned in an earlier post that it crashed when you tried to edit it, so want to make sure you still try to edit it and are successful. How and with what program are you trying to edit and import it?

  • Oh hai!

    I was using codewalker. Exported it to xml or something if I recall and then made the edits, saved, reimported and then on load it crashes. It's very possible that I'm missing a step here or not doing something correctly in that process.

  • @dimedius Are you on the most recent version of CodeWalker that you can only get from the CW discord? Here is the discord and you can find the most recent version in the #release channel. https://discord.gg/hXXeNW

  • I'm not sure, I haven't checked in about a month or so. Apparently I downloaded it in June 20 this year based on the file details. So a couple months old.

    Edit: @chonkie just checked and the last v is 3.033 from march, looks like I'm running the latest. Thanks for the suggestion though.

    Though, if you do have the correct process I should be following to edit the ymt, can you let me know or point me to the latest, correct guide?

  • I'm happy to announce I was able to address some questions I had about said gates/barriers.

    Here is the released mod: Barrier Fix

    Here is the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using GTA;
    using GTA.Native;
    namespace barrierFixer
        public class barrierFixer : Script
            public barrierFixer()
                Tick += OnTick;
            private void OnTick(object sender, EventArgs e)
                Ped playerPed = Game.Player.Character;
                Prop[] barrier_Gates = World.GetNearbyProps(playerPed.Position, 30f, -1184516519, 1230099731);
                foreach (var gate in barrier_Gates)
                    if (playerPed.IsInVehicle())
                        float dirDist = World.GetDistance(playerPed.RightPosition, gate.RightPosition);
                        if (dirDist <= 6f)
                            if (gate.Model.GetHashCode() == -1184516519 || gate.Model.GetHashCode() == 1230099731)
                                Function.Call(Hash.SET_STATE_OF_CLOSEST_DOOR_OF_TYPE, 0XB965B659, gate.Position.X, gate.Position.Y, gate.Position.Z, false, 1f);
                                Function.Call(Hash.SET_STATE_OF_CLOSEST_DOOR_OF_TYPE, 0X4951D513, gate.Position.X, gate.Position.Y, gate.Position.Z, false, 1f);
                        else if (dirDist > 15f)
                            if (gate.Model.GetHashCode() == -1184516519 || gate.Model.GetHashCode() == 1230099731)
                                Function.Call(Hash.SET_STATE_OF_CLOSEST_DOOR_OF_TYPE, 0XB965B659, gate.Position.X, gate.Position.Y, gate.Position.Z, false, -0.1f);
                                Function.Call(Hash.SET_STATE_OF_CLOSEST_DOOR_OF_TYPE, 0X4951D513, gate.Position.X, gate.Position.Y, gate.Position.Z, false, -0.1f);

  • Awesome that you solved it.
    Adding a check for all peds, not just the player would be nice. Like once every 2 sec. With performance in mind. If nothing should happen if not in a vehicle, that check could come first for example.
    Good job!

  • Ya, I was thinking about doing that. Might get to it this weekend or so (trying not to release too many updates making people re download - not sure how the community feels about it).

    Oh and I can probably get rid of the windows.form too (was trying to test on the honking key).

    Also, I'm learning in tiny baby steps but I was looking for tutorial on how to make working ini files. Got anything good thats up to date?

  • How to make an INI file, or how to read one?

    Make an INI file in code:

    var settings = ScriptSettings.Load("./script.ini");
    settings.SetValue("Section1", "Key1", "Value1");
    settings.SetValue("Section2", "Key2", "value2");

    Which would create:

    Key1 = Value1
    Key2 = value2

    Read the INI file:

    var settings = ScriptSettings.Load("./script.ini");
    string value1 = settings.GetValue("Section1", "Key1", "Value1");
    string value2 = settings.GetValue("Section2", "Key2", "Value2");

    If you plan to just use an INI file named the same as your script (e.g. MyScript.dll and MyScript.ini), then instead of using ScriptSettings.Load, use Settings, like so:

    //save values
    Settings.SetValue("Section1", "Key1", "Value1");
    Settings.SetValue("Section2", "Key2", "value2");
    //get values
    string value1 = Settings.GetValue("Section1", "Key1", "Value1");
    string value2 = Settings.GetValue("Section2", "Key2", "Value2");

  • @dimedius said in Needing help on a tiny script to open barrier arm gates.:

    Oh and I can probably get rid of the windows.form too (was trying to test on the honking key).

    The only usings you need in the script you posted above are:

    using System;
    using GTA;
    using GTA.Native;

    Visual Studio should tell you about unnecessary usings.

  • Thanks @Jitnaught, these will come in handy in the very near future.

    I'm looking to have the script read the values from an ini file (not interested in it making one, though I've seen some samples of that too).

    And additional thanks about the resources, anything that helps slim down the code is appreciated!

  • @dimedius

    I saw your 1.1 version. Great progress.

    I like to use this native to get the hash for readability (called once):
    Hash secBarrier1 = GAMEPLAY::GET_HASH_KEY("prop_sec_barrier_ld_01a");
    Hash secBarrier2 = GAMEPLAY::GET_HASH_KEY("prop_sec_barrier_ld_02a");

    Saw that you're already using Codewalker after writing this.

    But for others:

    Codewalker is a great tool to find out the name, hash of objects etc. You can move around in the world finding them.
    alt text
    (Press Load DLC, Press T, Press select objects, Right click on an object gives info to the right.

  • @R3QQ


    I was using the native function call in one of my other scripts and it seems it can be a burden performance wise. Like for this mod, its pretty light weight but in my other one it references way more objects.

    Right now I've moved to using the hashes straight up instead of trying to convert them (as suggested). Just wanting clarification on whats better suited for the scripts.

  • @dimedius
    Everything that can be called once should be called once. Since performance hit depends on how often you call it, in this case it doesn't matter.
    For example Menyoo could pre-hash but in many cases won't since the performance hit is neglectable: https://github.com/MAFINS/MenyooSP/search?q=get_hash_key&type=
    I think Alexander Blade does this as well.

    You generate the hash before the loop and then store it a variable that can be used in your loop.
    However if you really want to, you could make a variable and set it to the hash value, since you know the hash already. Just make sure there's documentation/comments to identify what the hash represents.

Log in to reply

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.