1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

CreateVehicleFromTextFile

Discussion in 'Snippets' started by FiNT, Jan 20, 2017.

  1. FiNT

    FiNT Member

    Messages:
    12
    Likes Received:
    3
    Joined:
    Jan 9, 2017
    Hi, I just wanted to share my 'CreateVehicleFromTextFile' its based on SA-MP LoadStaticVehiclesFromFile.


    PHP:
    public void CreateVehicleFromTextFile(string path)
    {
                
    string[] read File.ReadAllLines(path);
                foreach (
    string veh in read)
                {
                    
    string str veh.Trim(';');
                    
    string[] args str.Split(',');
                    if(
    args.Count() != 9) continue;
                    
    API.createVehicle(API.vehicleNameToModel(args[0]), new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3])), new Vector3(float.Parse(args[4]), float.Parse(args[5]), float.Parse(args[6])), int.Parse(args[7]), int.Parse(args[8]));
                }
     }
    Thanks to for the idea - Squalalah.

    vehs.txt format should be like:
    NAME,X,Y,Z,ROTX,ROTY,ROTZ,COLOR1,COLOR2;
    PHP:
    Bullet,73.98887,-796.12,30.91368,-0.1344612,2.318757,-110.884,94,90;
    Jester,107.0299,-780.5944,30.55625,0.409864,2.275177,70.31763,8,49;
    put this at API.onResourceStart or anywhere you load cars:
    PHP:

    CreateVehicleFromTextFile
    ("vehs.txt");
    savecmd:
    Exmalpe: /saveveh Bati
    PHP:
    [Command("saveveh")]
            public 
    void savevehicle_cmd(Client playerstring name)
            {
                if (
    API.isPlayerInAnyVehicle(player))
                {
                    var 
    pos API.getEntityPosition(player);
                    var 
    angle API.getEntityRotation(player);
                    
    NetHandle veh API.getPlayerVehicle(player);
                    
    File.AppendAllText("vehs.txt"string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8};\r\n"namepos.Xpos.Ypos.Zangle.Xangle.Yangle.ZAPI.getVehiclePrimaryColor(veh), API.getVehicleSecondaryColor(veh)));
                    
    API.sendNotificationToPlayer(playerstring.Format("vehicle {0} was saved."name), true);
                }
                else 
    API.sendChatMessageToPlayer(player"You are not in any vehicle.");
            }


    save command:
    PHP:
    [Command("saveveh")]
            public 
    void savevehicle_cmd(Client player)
            {
                if (
    API.isPlayerInAnyVehicle(player))
                {
                    var 
    pos API.getEntityPosition(player);
                    var 
    angle API.getEntityRotation(player);
                    
    NetHandle veh API.getPlayerVehicle(player);
                    
    File.AppendAllText("vehs.txt"string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8};\r\n"player.vehicle.displayNamepos.Xpos.Ypos.Zangle.Xangle.Yangle.ZAPI.getVehiclePrimaryColor(veh), API.getVehicleSecondaryColor(veh)));
                    
    API.sendNotificationToPlayer(playerstring.Format("vehicle {0} was saved."player.vehicle.displayName), true);
                }
                else 
    API.sendChatMessageToPlayer(player"You are not in any vehicle.");
            }
    Enjoy guys I hope I helped :=)
     
    Last edited: Jan 20, 2017
    kova98, 1B0Y and Float like this.
  2. Float

    Float Member

    Messages:
    29
    Likes Received:
    2
    Joined:
    Dec 2, 2016
    Really nice , can you convert this into somekind of tutorial?
     
  3. Squalalah

    Squalalah Well-Known Member

    Messages:
    105
    Likes Received:
    29
    Joined:
    Jan 16, 2017
    Good idea ! I propose too :

    Code:
    
            private void CreateVehicleFromTextFile(string path)
            {
                string[] read = File.ReadAllLines(path);
                foreach(string veh in read)
                {
                    string str = veh.Trim(';');
                    string[] args = veh.Split(',');
                    //OPTIONAL if(args.Count != 9) ErrorMessage(blabla) continue;
                    vehicles.Add(API.createVehicle(API.vehicleNameToModel(args[0]), new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3])), new Vector3(float.Parse(args[4]), float.Parse(args[5]), float.Parse(args[6])), int.Parse(args[7]), int.Parse(args[8])));
                }
            }
    
     
    Last edited: Jan 20, 2017
    FiNT likes this.
  4. -Andreas

    -Andreas Well-Known Member

    Messages:
    172
    Likes Received:
    25
    Joined:
    Sep 29, 2016
    What's the purpose of storing the vehicle data in a text file instead of declaring the data in the script itself?
     
  5. Squalalah

    Squalalah Well-Known Member

    Messages:
    105
    Likes Received:
    29
    Joined:
    Jan 16, 2017
    What's the purpose of storing the vehicle data in a text file instead of database ?
    What's the purpose of declaring the data in the script instead of database ?

    It's just an option. If you want to access to your vehicles directly in .txt, and use it for other projects.
    If you write a command like "/savepos <vehiclemodel> <blabla>" IG, who save your position and the vehiclemodel into .txt file to use it after in your code...
     
  6. -Andreas

    -Andreas Well-Known Member

    Messages:
    172
    Likes Received:
    25
    Joined:
    Sep 29, 2016
    I wasn't trying to sound rude lol, but just trying to understand why you would save it in a textfile and retrieve it the way you do. Like, does it mean better performance on the long run etc. But thanks for your reply..
     
  7. Squalalah

    Squalalah Well-Known Member

    Messages:
    105
    Likes Received:
    29
    Joined:
    Jan 16, 2017
    Me too, i just wanted to use your reasoning in another way.
    A .txt file system is slow compared to mysql system (for example). In SAMP, old gamemodes sometimes used .INI file system to save data.
    It is just an optional way to store data
     
    Last edited: Jan 20, 2017
  8. -Andreas

    -Andreas Well-Known Member

    Messages:
    172
    Likes Received:
    25
    Joined:
    Sep 29, 2016
    Ah yeah I see. I hadn't considered using a .txt file like this to store data before.
     
  9. FiNT

    FiNT Member

    Messages:
    12
    Likes Received:
    3
    Joined:
    Jan 9, 2017
    Thanks good idea, but i tried it and I get error for args[8] since its ';' at the end and not ',' proabably would be easier to just use ',' at the end.
     
  10. Squalalah

    Squalalah Well-Known Member

    Messages:
    105
    Likes Received:
    29
    Joined:
    Jan 16, 2017
    replace

    Code:
    string[] args = veh.Split(',');
    by

    Code:
    string[] args = str.Split(',');
    i forgot to change sry, TRIM delete the char declared as argument in the specified string.
     
    FiNT likes this.
  11. FiNT

    FiNT Member

    Messages:
    12
    Likes Received:
    3
    Joined:
    Jan 9, 2017
    thanks, I've edited my post.
     
  12. Squalalah

    Squalalah Well-Known Member

    Messages:
    105
    Likes Received:
    29
    Joined:
    Jan 16, 2017
    If you want to prevent the script from crashing you can add this right before vehicles.add

    Code:
    If(args.Count != 9) continue;
    The loop (foreach) will go to the next line without adding the bugged line

    (Of course, if there are 9 args but one of them is a string instead of int (for example) it will crash, so you can check every args if you want)
     
  13. root

    root Well-Known Member

    Messages:
    244
    Likes Received:
    88
    Joined:
    Aug 15, 2016
    You should also really put this into a try {} catch {}. If someone puts an invalid line in the file, it could potentially crash (the server).
     
  14. kubop

    kubop Active Member

    Messages:
    87
    Likes Received:
    4
    Joined:
    Sep 25, 2016
    I'd like to inform you that this won't work for some cars, as you're using player.vehicle.displayName which doesn't need to be exactly vehicle model name as well.

    For example car model Windsor2 have displayName = Windsor Drop and it won't spawn this car.

    I'd suggest storing vehicles in hashes.
     

Share This Page