Quantcast

Jump to content

» «
Photo

[C#] Check if AI is stuck

21 replies to this topic
PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#1

Posted 20 February 2014 - 05:07 PM

Hi again, third post...

 

I am trying to make a mod where there are people following you, which i have managed to do.

 

Problem is, sometimes they f*** up and crash into something, and for some reason they dont even try and reverse back a little.

 

Does anyone know the code to check if the vehicle has the speed 0-3 or something over 15 seconds or so?

 

I'll expirement around more of coarse, so if i finally find an answer, ill post it here.

 

 


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#2

Posted 20 February 2014 - 05:18 PM Edited by LordOfTheBongs, 20 February 2014 - 05:20 PM.

//set your vehicle wherever and when u tell them to start driving, set the current game time to the gameTime integer
//hook the method below whenever u need to check for if the vehicle is stuck, then u can unhook whenever or just null someVehicle when u dont need to use
int gameTime; Vehicle someVehicle;
private void CheckVehicleStuckTick(object sender, EventArgs e)
{
    if (!Game.Exists(someVehicle)) return;
    if (!someVehicle.isDriveable) return;
    if (someVehicle.Speed > 3) { gameTime = Game.GameTime; return; }
 
    if (Game.GameTime - gameTime > 15000)
    {
        gameTime = Game.GameTime;
        //someVehicle is stuck... do something
    }
}

try something like that


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#3

Posted 21 February 2014 - 01:29 AM Edited by PlayPrey, 21 February 2014 - 01:38 AM.

hmm, i can't get it to work, i tried so many diffrent ways, but nothing happens when the ai is standing still for a amount of seconds...

 

I got a feeling that i do something wrong the the gameTime thing. I don't know though.


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#4

Posted 21 February 2014 - 02:24 AM

some debugging progress.. None of the if (car.speed > 3) works no matter how i try them.

 

Any alternative?


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#5

Posted 21 February 2014 - 05:01 AM Edited by LordOfTheBongs, 21 February 2014 - 06:52 AM.

paste your entire script


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#6

Posted 21 February 2014 - 01:15 PM

using GTA;
using System;
using System.Drawing;
using System.Windows.Forms;


namespace Stalker
{

    public class main  : Script
    {
        int stuckTimer;
        GTA.Timer stuckTimert;
        Vehicle suvStalker01;
        Random rnd = new Random();
        String suv01;
        Boolean stalker0 = true;
        Boolean stalker0p = false;
         public main()  
     
        {
            this.KeyDown += new GTA.KeyEventHandler(this.KeyCheck_Down);
            

        }
        private void KeyCheck_Down(object sender, GTA.KeyEventArgs e)
        {
            if (stalker0 == true)
            {
                BindKey(Keys.N, new KeyPressDelegate(spawnVehicle01));
            }
        }
         public void spawnVehicle01()
        {
            int stalker01carpicker = rnd.Next(1, 91);
            if (stalker01carpicker.Equals(1)) suv01 = "ADMIRAL";
            if (stalker01carpicker.Equals(2)) suv01 = "BANSHEE";
            if (stalker01carpicker.Equals(3)) suv01 = "BENSON";
            if (stalker01carpicker.Equals(4)) suv01 = "BIFF";
            if (stalker01carpicker.Equals(5)) suv01 = "BLISTA";
            if (stalker01carpicker.Equals(6)) suv01 = "BOBCAT";
            if (stalker01carpicker.Equals(7)) suv01 = "BOXVILLE";
            if (stalker01carpicker.Equals(8)) suv01 = "BUCCANEER";
            if (stalker01carpicker.Equals(9)) suv01 = "BURRITO";
            if (stalker01carpicker.Equals(10)) suv01 = "BURRITO2";
            if (stalker01carpicker.Equals(11)) suv01 = "BUS";
            if (stalker01carpicker.Equals(12)) suv01 = "CAVALCADE";
            if (stalker01carpicker.Equals(13)) suv01 = "CHAVOS";
            if (stalker01carpicker.Equals(14)) suv01 = "COGNOSCENTI";
            if (stalker01carpicker.Equals(15)) suv01 = "COMET";
            if (stalker01carpicker.Equals(16)) suv01 = "COQUETTE";
            if (stalker01carpicker.Equals(17)) suv01 = "DF8";
            if (stalker01carpicker.Equals(18)) suv01 = "DILETTANTE";
            if (stalker01carpicker.Equals(19)) suv01 = "DUKES";
            if (stalker01carpicker.Equals(20)) suv01 = "E109";
            if (stalker01carpicker.Equals(21)) suv01 = "EMPEROR";
            if (stalker01carpicker.Equals(22)) suv01 = "EMPEROR2";
            if (stalker01carpicker.Equals(23)) suv01 = "ESPERANTO";
            if (stalker01carpicker.Equals(24)) suv01 = "FACTION";
            if (stalker01carpicker.Equals(25)) suv01 = "FELTZER";
            if (stalker01carpicker.Equals(26)) suv01 = "FEROCI";
            if (stalker01carpicker.Equals(27)) suv01 = "FEROCI2";
            if (stalker01carpicker.Equals(28)) suv01 = "FLATBED";
            if (stalker01carpicker.Equals(29)) suv01 = "FORTUNE";
            if (stalker01carpicker.Equals(30)) suv01 = "FUTO";
            if (stalker01carpicker.Equals(31)) suv01 = "FXT";
            if (stalker01carpicker.Equals(32)) suv01 = "HABANERO";
            if (stalker01carpicker.Equals(33)) suv01 = "HAKUMAI";
            if (stalker01carpicker.Equals(34)) suv01 = "HUNTLEY";
            if (stalker01carpicker.Equals(35)) suv01 = "INFERNUS";
            if (stalker01carpicker.Equals(36)) suv01 = "INGOT";
            if (stalker01carpicker.Equals(37)) suv01 = "INTRUDER";
            if (stalker01carpicker.Equals(38)) suv01 = "LANDSTALKER";
            if (stalker01carpicker.Equals(39)) suv01 = "LOKUS";
            if (stalker01carpicker.Equals(40)) suv01 = "MANANA";
            if (stalker01carpicker.Equals(41)) suv01 = "MARBELLA";
            if (stalker01carpicker.Equals(42)) suv01 = "MERIT";
            if (stalker01carpicker.Equals(43)) suv01 = "MINIVAN";
            if (stalker01carpicker.Equals(44)) suv01 = "MOONBEAM";
            if (stalker01carpicker.Equals(45)) suv01 = "MRTASTY";
            if (stalker01carpicker.Equals(46)) suv01 = "MULE";
            if (stalker01carpicker.Equals(47)) suv01 = "STOCKADE";
            if (stalker01carpicker.Equals(48)) suv01 = "ORACLE";
            if (stalker01carpicker.Equals(49)) suv01 = "PACKER";
            if (stalker01carpicker.Equals(50)) suv01 = "PATRIOT";
            if (stalker01carpicker.Equals(51)) suv01 = "PERENNIAL";
            if (stalker01carpicker.Equals(52)) suv01 = "PERENNIAL2";
            if (stalker01carpicker.Equals(53)) suv01 = "PEYOTE";
            if (stalker01carpicker.Equals(54)) suv01 = "PHANTOM";
            if (stalker01carpicker.Equals(55)) suv01 = "PINNACLE";
            if (stalker01carpicker.Equals(56)) suv01 = "PMP600";
            if (stalker01carpicker.Equals(57)) suv01 = "PONY";
            if (stalker01carpicker.Equals(58)) suv01 = "PREMIER";
            if (stalker01carpicker.Equals(59)) suv01 = "PRES";
            if (stalker01carpicker.Equals(60)) suv01 = "PRIMO";
            if (stalker01carpicker.Equals(61)) suv01 = "RANCHER";
            if (stalker01carpicker.Equals(62)) suv01 = "REBLA";
            if (stalker01carpicker.Equals(63)) suv01 = "RIPLEY";
            if (stalker01carpicker.Equals(64)) suv01 = "ROMERO";
            if (stalker01carpicker.Equals(65)) suv01 = "ROM";
            if (stalker01carpicker.Equals(66)) suv01 = "RUINER";
            if (stalker01carpicker.Equals(67)) suv01 = "SABRE";
            if (stalker01carpicker.Equals(68)) suv01 = "SABRE2";
            if (stalker01carpicker.Equals(69)) suv01 = "SABREGT";
            if (stalker01carpicker.Equals(70)) suv01 = "SCHAFTER";
            if (stalker01carpicker.Equals(71)) suv01 = "SENTINEL";
            if (stalker01carpicker.Equals(72)) suv01 = "SOLAIR";
            if (stalker01carpicker.Equals(73)) suv01 = "SPEEDO";
            if (stalker01carpicker.Equals(74)) suv01 = "STALION";
            if (stalker01carpicker.Equals(75)) suv01 = "STRATUM";
            if (stalker01carpicker.Equals(76)) suv01 = "STRETCH";
            if (stalker01carpicker.Equals(77)) suv01 = "SULTAN";
            if (stalker01carpicker.Equals(78)) suv01 = "SULTANRS";
            if (stalker01carpicker.Equals(79)) suv01 = "SUPERGT";
            if (stalker01carpicker.Equals(80)) suv01 = "TRASH";
            if (stalker01carpicker.Equals(81)) suv01 = "TURISMO";
            if (stalker01carpicker.Equals(82)) suv01 = "URANUS";
            if (stalker01carpicker.Equals(83)) suv01 = "VIGERO";
            if (stalker01carpicker.Equals(84)) suv01 = "VIGERO2";
            if (stalker01carpicker.Equals(85)) suv01 = "VINCENT";
            if (stalker01carpicker.Equals(86)) suv01 = "VIRGO";
            if (stalker01carpicker.Equals(87)) suv01 = "VOODOO";
            if (stalker01carpicker.Equals(88)) suv01 = "WASHINGTON";
            if (stalker01carpicker.Equals(89)) suv01 = "WILLARD";
            if (stalker01carpicker.Equals(90)) suv01 = "YANKEE";

            if (stalker0 == true)
            {
                Game.DisplayText("Debugging, Your stalker is driving a " + suv01 + ", and this is your first stalker.", 2600);
                Interval = 250;
                suvStalker01 = GTA.World.CreateVehicle(suv01, World.GetNextPositionOnStreet(Player.Character.Position.Around(150.0F)));
                suvDriver01 = GTA.World.CreatePed("M_Y_GMAF_LO_02", Player.Character.Position.Around(5.0F));
                if (Exists(suvStalker01) && Exists(suvDriver01))
                {
                    suvDriver01.WarpIntoVehicle(suvStalker01, VehicleSeat.Driver);
                }
                if (Exists(suvDriver01))
                {
                    Blip veh01 = suvStalker01.AttachBlip();
                    veh01.Color = BlipColor.LightYellow;
                    veh01.Name = "First Vehicle";

                }
                stuckTimer = Game.GameTime;

                if (Player.Character.isInVehicle())
                {
                    if (Player.Character.CurrentVehicle.Equals(suvStalker01))
                    {
                        suvStalker01.Color = GTA.ColorIndex.TaxiYellow2;
                    }
                }
                stalker0 = false;
                stalker1 = true;

                this.Tick += new EventHandler(this.mashHorn);
            }
             private void mashHorn(object sender, EventArgs e)
            {
                if (World.CurrentDayTime.Equals(40)) Game.DisplayText("Now time 40 :D");
                if (Player.Character.Position.DistanceTo(suvDriver01.Position) < 20.0f && stalker0p == false)
                {
                    suvStalker01.Speed = 0.0f;
                    suvStalker01.EngineRunning = false;
                }
                if (Player.Character.isInVehicle())
                {
                    if (Player.Character.CurrentVehicle.Equals(suvStalker01))
                    {
                        stalker0p = true;
                        suvStalker01.Color = GTA.ColorIndex.TaxiYellow2;
                        suvStalker01.FeatureColor1 = GTA.ColorIndex.TaxiYellow2;
                    }
                }
              
                if (Player.Character.Position.DistanceTo(suvDriver01.Position) > 23.0f) suvStalker01.EngineRunning = true;
                if (Player.Character.Position.DistanceTo(suvDriver01.Position) > 27.0f) suvDriver01.Task.DriveTo(Player, 7, true);
                if (suvStalker01.isUpsideDown && Player.Character.Position.DistanceTo(suvDriver01.Position) > 50.0f)
                {
                    World.GetNextPositionOnPavement(Player.Character.Position.Around(75.0f));

                    if (suvDriver01.CurrentVehicle == null)
                    {
                        suvDriver01.NoLongerNeeded();
                        suvDriver01.Task.Die();
                    }
                    if (Player.Character.Position.DistanceTo(suvDriver01.Position) > 110.0f) suvDriver01.Task.DriveTo(Player, 30, true);
                    if (Player.Character.Position.DistanceTo(suvDriver01.Position) > 200.0f)
                    {
                        
                        suvDriver01.Task.DriveTo(Player, 100, false);
                    }
                    if (suvDriver01.isAlive == true)
                    {

                       if (suvStalker01.Speed > 3.0f)
                       {
                           Game.DisplayText("Speed is below 3");
                           stuckTimert.Interval = 1000;
                           stuckTimert.Start();
                           if (stuckTimert.ElapsedTime == 15)
                           {
                               Game.DisplayText("Timer Test");

                           }
                       }
                       if (suvStalker01.Speed < 3.0f)
                        {
                            stuckTimert.Stop();
                        }

                    }
                    

                   
                    
                }

            }
           

This is the last one i tried.

 

Its a messy code, please remember im a newbie to this.


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#7

Posted 21 February 2014 - 03:25 PM Edited by LordOfTheBongs, 22 February 2014 - 10:05 AM.

this script has errors and the formatting is pretty bad, are u doing this on notepad??

 

you made everything pretty confusing to be honest and ill just say u cant even check if the vehicle is stuck unless it is upside down and greater than 50f from player

 

your code isnt going to work like that

 

this is just wrong

if (suvStalker01.Speed > 3.0f)
                       {
                           Game.DisplayText("Speed is below 3");

your checking if speed is greater than 3 and telling yourself it is below 3, that is just wrong among other things in the script. A good way to write code is to first write what u want a script to do making methods first and then after writing the code for inside the method. That way u keep stuff organized... example...

 

namespace SpawnTail
{
    using System;
    using GTA;

    public class Main : Script
    {
        private Ped driver;
        private Vehicle car;
        private int gameTimeStuck, gameTimeDrive, soundHornTime;

        private string GetVehicleModel()
        {
            int stalker01carpicker = new Random().Next(1, 91);
            switch (stalker01carpicker)
            {
                case 1: return "ADMIRAL";
                case 2: return "BANSHEE";
                case 3: return "BENSON";
                case 4: return "BIFF";
                case 5: return "BLISTA";
                case 6: return "BOBCAT";
                case 7: return "BOXVILLE";
                case 8: return "BUCCANEER";
                case 9: return "BURRITO";
                case 10: return "BURRITO2";
                case 11: return "BUS";
                case 12: return "CAVALCADE";
                case 13: return "CHAVOS";
                case 14: return "COGNOSCENTI";
                case 15: return "COMET";
                case 16: return "COQUETTE";
                case 17: return "DF8";
                case 18: return "DILETTANTE";
                case 19: return "DUKES";
                case 20: return "E109";
                case 21: return "EMPEROR";
                case 22: return "EMPEROR2";
                case 23: return "ESPERANTO";
                case 24: return "FACTION";
                case 25: return "FELTZER";
                case 26: return "FEROCI";
                case 27: return "FEROCI2";
                case 28: return "FLATBED";
                case 29: return "FORTUNE";
                case 30: return "FUTO";
                case 31: return "FXT";
                case 32: return "HABANERO";
                case 33: return "HAKUMAI";
                case 34: return "HUNTLEY";
                case 35: return "INFERNUS";
                case 36: return "INGOT";
                case 37: return "INTRUDER";
                case 38: return "LANDSTALKER";
                case 39: return "LOKUS";
                case 40: return "MANANA";
                case 41: return "MARBELLA";
                case 42: return "MERIT";
                case 43: return "MINIVAN";
                case 44: return "MOONBEAM";
                case 45: return "MRTASTY";
                case 46: return "MULE";
                case 47: return "STOCKADE";
                case 48: return "ORACLE";
                case 49: return "PACKER";
                case 50: return "PATRIOT";
                case 51: return "PERENNIAL";
                case 52: return "PERENNIAL2";
                case 53: return "PEYOTE";
                case 54: return "PHANTOM";
                case 55: return "PINNACLE";
                case 56: return "PMP600";
                case 57: return "PONY";
                case 58: return "PREMIER";
                case 59: return "PRES";
                case 60: return "PRIMO";
                case 61: return "RANCHER";
                case 62: return "REBLA";
                case 63: return "RIPLEY";
                case 64: return "ROMERO";
                case 65: return "ROM";
                case 66: return "RUINER";
                case 67: return "SABRE";
                case 68: return "SABRE2";
                case 69: return "SABREGT";
                case 70: return "SCHAFTER";
                case 71: return "SENTINEL";
                case 72: return "SOLAIR";
                case 73: return "SPEEDO";
                case 74: return "STALION";
                case 75: return "STRATUM";
                case 76: return "STRETCH";
                case 77: return "SULTAN";
                case 78: return "SULTANRS";
                case 79: return "SUPERGT";
                case 80: return "TRASH";
                case 81: return "TURISMO";
                case 82: return "URANUS";
                case 83: return "VIGERO";
                case 84: return "VIGERO2";
                case 85: return "VINCENT";
                case 86: return "VIRGO";
                case 87: return "VOODOO";
                case 88: return "WASHINGTON";
                case 89: return "WILLARD";
                case 90: return "YANKEE";
            }

            return null;
        }

        private float GetSpeed()
        {
            float distance = Game.LocalPlayer.Character.Position.DistanceTo(car.Position);
            return distance * 0.6f;
        }
        
        private float GetHeading()
        {
            Vehicle closestVehicle = World.GetClosestVehicle(car.Position, 15f);
            return Game.Exists(closestVehicle) ? closestVehicle.Heading : Helper.DirectionToHeading(Vector3.Normalize(Game.LocalPlayer.Character.Position - car.Position));
        }

        private bool GetObeyLaw()
        {
            return Game.LocalPlayer.Character.Position.DistanceTo(car.Position) < 8f;
        }

        private bool GetDriveOnWrongSide()
        {
            return Game.LocalPlayer.Character.Position.DistanceTo(car.Position) < 15f;
        }

        public Main()
        {
            Tick += MainTick;
        }

        private void MainTick(object sender, EventArgs e)//to be organized... only use methods inside tick, your code is super messy
        {
            SpawnCar();

            SpawnDriver();

            UpdateDriverDestination(500);
            
            CheckIfShouldMashHorn();

            CheckIfStuckEveryFifteenSeconds();

            Cleanup();
        }//i wrote and completed this method before i even wrote anything else

        private void SpawnCar()
        {
            if (Game.Exists(car) || Game.Exists(driver) || !Game.LocalPlayer.Character.isInVehicle()) return;//exits method if it sees u already spawned a car, driver from previous spawn hasnt been cleaned up yet or ur not even driving

            car = World.CreateVehicle(GetVehicleModel(), World.GetNextPositionOnStreet(Game.LocalPlayer.Character.Position.Around(75f)));
            car.Heading = GetHeading();
            car.isRequiredForMission = true;
            gameTimeStuck = Game.GameTime;
            Blip blip = car.AttachBlip();
            blip.Icon = BlipIcon.Person_Unknown;
            blip.Name = "Stalker";
        }

        private void SpawnDriver()
        {
            if (!Game.Exists(car) || Game.Exists(driver) || !Game.LocalPlayer.Character.isInVehicle()) return;//exits method if it sees u already spawned a ped and set to driver, if u never spawned the car or if player not driving

            driver = car.CreatePedOnSeat(VehicleSeat.Driver, "M_Y_GMAF_LO_02");
            driver.BecomeMissionCharacter();
        }

        private void UpdateDriverDestination(int interval)
        {
            if (!Game.Exists(driver) || !Game.Exists(car) || !driver.isInVehicle(car) || car.GetPedOnSeat(VehicleSeat.Driver) != driver) return;//script wont even tell driver anything if one doesnt exist, the car doesnt exist, he isnt in the car, or he isnt driving the car

            if (Game.GameTime - gameTimeDrive > interval)
            {
                gameTimeDrive = Game.GameTime;//reset
                driver.Task.DriveTo(Game.LocalPlayer.Character, GetSpeed(), GetObeyLaw(), GetDriveOnWrongSide());
            }
        }
        
        private void CheckIfShouldMashHorn()
        {
            if (!Game.Exists(driver) || !driver.isAliveAndWell || !Game.Exists(car) || !driver.isInVehicle(car) || car.GetPedOnSeat(VehicleSeat.Driver) != driver) return;//script wont even tell driver anything if one doesnt exist, the car doesnt exist, he isnt in the car, or he isnt driving the car

            if (car.Position.DistanceTo(Game.LocalPlayer.Character.Position) < 10f)
            {
                if (Game.GameTime - soundHornTime > 5000)
                {
                    soundHornTime = Game.GameTime;
                    car.SoundHorn(5000);
                }
            }
        }

        private void CheckIfStuckEveryFifteenSeconds()
        {
            if (!Game.Exists(driver) || !Game.Exists(car)) return;//script wont even check if stuck unless both exist

            if (car.Speed > 3f || car.Position.DistanceTo(Game.LocalPlayer.Character.Position) < 7.5f) { gameTimeStuck = Game.GameTime; return; }//resets timer and exits method because driver is going fast enough

            if (Game.GameTime - gameTimeStuck > 15000)
            {
                gameTimeStuck = Game.GameTime;//reset

                Game.DisplayText("DRIVER HAS BEEN GOING LESS THAN OR EQUAL TO 3F FOR LAST 15 SECONDS AND HE IS NOT NEARBY..." + Environment.NewLine +
                                 "CLEANING...NEW DRIVER WILL SPAWN", 6000);

                Cleanup(true);
            }
        }

        private void Cleanup(bool forceCleanup = false)
        {
            if (Game.Exists(driver))
            {
                if (forceCleanup || !driver.isAliveAndWell || !driver.isInVehicle() || !Game.Exists(car) || !Game.LocalPlayer.Character.isInVehicle())
                {
                    driver.NoLongerNeeded();
                    driver = null;
                }
            }

            if (Game.Exists(car))
            {
                if (forceCleanup || !car.isDriveable || !Game.Exists(driver) || !Game.LocalPlayer.Character.isInVehicle() || car.Position.DistanceTo(Game.LocalPlayer.Character.Position) > 150f)
                {
                    car.NoLongerNeeded();
                    car = null;
                }
            }
        }
    }
}

 

So try to write only methods first in the tick so u stay organized and then write the code for each method


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#8

Posted 22 February 2014 - 06:31 AM

updated my post, i didnt rewrite your script but wrote my own to give u ideas on how to stay organized. Organization is pretty important or u wont be able to debug anything


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#9

Posted 22 February 2014 - 06:26 PM Edited by PlayPrey, 22 February 2014 - 06:29 PM.

Sry. But i felt like what i did was pretty orginized..  Thanks for the script idea, ill look into it when im in the mood for it. Thanks again.

 

This is being done in Visual Studio Express 2013.


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#10

Posted 22 February 2014 - 06:49 PM Edited by LordOfTheBongs, 22 February 2014 - 06:58 PM.

Sry. But i felt like what i did was pretty orginized..  Thanks for the script idea, ill look into it when im in the mood for it. Thanks again.

 

This is being done in Visual Studio Express 2013.

lol... no... just save ur code as is and look at in 6 months and tell me if u still think it is organized


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#11

Posted 22 February 2014 - 07:40 PM

 

Sry. But i felt like what i did was pretty orginized..  Thanks for the script idea, ill look into it when im in the mood for it. Thanks again.

 

This is being done in Visual Studio Express 2013.

lol... no... just save ur code as is and look at in 6 months and tell me if u still think it is organized

 

So what do you suggest? Just delete the mod code, or put it aside, and start all over?


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#12

Posted 22 February 2014 - 07:52 PM Edited by LordOfTheBongs, 22 February 2014 - 07:56 PM.

 

 

Sry. But i felt like what i did was pretty orginized..  Thanks for the script idea, ill look into it when im in the mood for it. Thanks again.

 

This is being done in Visual Studio Express 2013.

lol... no... just save ur code as is and look at in 6 months and tell me if u still think it is organized

 

So what do you suggest? Just delete the mod code, or put it aside, and start all over?

 

 

i told u what i would suggest to stay organized. Write in the tick what u want to do first by making up methods for stuff u want to accomplish. Look at the tick method i made, it pretty much says in plain english what the script does... then just make sure each method can work properly. U see how i return out of the method if stuff isnt right. So think of each method as independant of each other so you dont conflict any functionality

 

if u look at my script all the methods work independant of each other but they all work together. Im just more use to writing stuff for gta so i know how gta works a little better, u will get use to it

 

i would suggest to start over only to rewrite the outline of the script using only methods and making names for the methods that are easy to understand... and then copy paste code from your original script into the methods but with edits of course to make it work better...

 

also read exactly what a keydown event does. What ur doing in the keydown im positive ur not intending to do


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#13

Posted 22 February 2014 - 07:59 PM Edited by LordOfTheBongs, 22 February 2014 - 08:09 PM.

        private void MainTick(object sender, EventArgs e)//to be organized... only use methods inside tick, your code is super messy
        {
            SpawnCar();
 
            SpawnDriver();
 
            UpdateDriverDestination();
 
            CheckIfShouldMashHorn();
 
            CheckIfStuckEveryFifteenSeconds();
 
            Cleanup();
        }
 
        private void SpawnCar()
        {
 
        }

        private void SpawnDriver()
        {
 
        }

        private void UpdateDriverDestination()
        {
 
        }
        
        private void CheckIfShouldMashHorn()
        {
 
        }

        private void CheckIfStuckEveryFifteenSeconds()
        {
 
        }

        private void Cleanup()
        {
 
        }

so write the outline of your script first like above, so u know what u have to do first and so u can make sure each block of code can run when it needs to, u just need to return from each method by checking conditions specific to the method working correctly...

        private void SpawnDriver()
        {
            if (!Game.Exists(car) || Game.Exists(driver) || !Game.LocalPlayer.Character.isInVehicle()) return;//exits method if it sees u already spawned a ped and set to driver, if u never spawned the car or if player not driving

            driver = car.CreatePedOnSeat(VehicleSeat.Driver, "M_Y_GMAF_LO_02");
            driver.BecomeMissionCharacter();
        }

this method is getting called constantly in my script but it only runs exactly when i want it to.... u need to create return conditions for every method u write and then u have no crashes and stuff works and runs exactly when u want and u dont need to mix up all your code (why yours doesnt work, u were trying to check if they were stuck only if upside down and far away from player, which makes no sense)

 

i think u just will get use to programming and develop your own style but this is stuff i do to be organized ;)

 

good luck and just practice


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#14

Posted 22 February 2014 - 08:24 PM Edited by PlayPrey, 22 February 2014 - 08:27 PM.

OK, i copied your script just to test how it worked out, turns out... it crashes the game...

 

But just to get stuff straight. return skips the entire method if the if returns false?

 

 

Scripthook managed to tell me that there are no scripts running before it crashed.. ( the log file, not the game- load complete = crash )


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#15

Posted 22 February 2014 - 09:06 PM Edited by LordOfTheBongs, 22 February 2014 - 09:05 PM.

OK, i copied your script just to test how it worked out, turns out... it crashes the game...

 

But just to get stuff straight. return skips the entire method if the if returns false?

 

 

Scripthook managed to tell me that there are no scripts running before it crashed.. ( the log file, not the game- load complete = crash )

 

no it doesnt i tested and works fine, i guess u copied wrong, i can make a vid to show u the script in action if u like?

 

and yeah... u dont need brackets if the code u want to run after the if is 1 line (ur only using 1 ';')

 

so im saying if one of these things is true then return. When u return from a method... u exit it pretty much


paste the error from the log, it probably tells u what happened


just install this and dont copy from the forum post...

 

https://dl.dropboxus...A/SpawnTail.rar


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#16

Posted 22 February 2014 - 09:07 PM

No need for a video, i belive you...

 

I got the game to load, however the script dosent get detetected, huray for that.

 

I'll report back when i got any troubleshooting resoults.


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#17

Posted 22 February 2014 - 09:09 PM Edited by LordOfTheBongs, 22 February 2014 - 09:10 PM.

No need for a video, i belive you...

 

I got the game to load, however the script dosent get detetected, huray for that.

 

I'll report back when i got any troubleshooting resoults.

just open console and pageup all the way and tell me your scripthookdotnet version, it says at top

 

u should be running 1.7.1.7 beta on eflc or 1.0.7 with scripthook.dll 0.5.1

 

1.0.4 bugs are possible and not guaranteed to work

 

u also need to make sure ur installing this as a cs file


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#18

Posted 22 February 2014 - 09:16 PM

 

No need for a video, i belive you...

 

I got the game to load, however the script dosent get detetected, huray for that.

 

I'll report back when i got any troubleshooting resoults.

just open console and pageup all the way and tell me your scripthookdotnet version, it says at top

 

u should be running 1.7.1.7 beta on eflc or 1.0.7 with scripthook.dll 0.5.1

 

1.0.4 bugs are possible and not guaranteed to work

 

u also need to make sure ur installing this as a cs file

 

There we go, thank you! :) Installing it as a .cs file worked, i compiled the code... :p

 

It works decent, some stuff to fix, like what distance he stops at and make him follow traffic rules, and then add the objective i had planned in secret. :p

 

Thanks a lot for the code, ill try and understand how to use that instead of my way.


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#19

Posted 22 February 2014 - 09:21 PM

 

 

No need for a video, i belive you...

 

I got the game to load, however the script dosent get detetected, huray for that.

 

I'll report back when i got any troubleshooting resoults.

just open console and pageup all the way and tell me your scripthookdotnet version, it says at top

 

u should be running 1.7.1.7 beta on eflc or 1.0.7 with scripthook.dll 0.5.1

 

1.0.4 bugs are possible and not guaranteed to work

 

u also need to make sure ur installing this as a cs file

 

There we go, thank you! :) Installing it as a .cs file worked, i compiled the code... :p

 

It works decent, some stuff to fix, like what distance he stops at and make him follow traffic rules, and then add the objective i had planned in secret. :p

 

Thanks a lot for the code, ill try and understand how to use that instead of my way.

 

 

ok great, yeah i didnt exactly copy yours so yeah u will need to change the way it works but yeah hope it gives u ideas... enjoy ;)


PlayPrey
  • PlayPrey

    string ScriptSkillLevel = "Low-Med";

  • Members
  • Joined: 02 Jul 2012
  • Norway

#20

Posted 22 February 2014 - 09:45 PM

Fun fact: Something goes wrong. :|...

2014-02-22 22:37:51 - STARTING SCRIPTS...
2014-02-22 22:37:51 -  ...successfully started script 'Stalker2.Main'!
2014-02-22 22:43:52 - Error during Tick in script 'Stalker2.Main':
                      System.NullReferenceException: Object reference not set to an instance of an object.
                         at Stalker2.Main.GetHeading()
                         at Stalker2.Main.SpawnCar()
                         at Stalker2.Main.MainTick(Object sender, EventArgs e)
                         at GTA.Script.TryTick()
                         at GTA.Script.DoTick()
                         at GTA.ScriptThread.OnTick()


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#21

Posted 22 February 2014 - 10:14 PM Edited by LordOfTheBongs, 22 February 2014 - 10:23 PM.

 

Fun fact: Something goes wrong. :|...

2014-02-22 22:37:51 - STARTING SCRIPTS...
2014-02-22 22:37:51 -  ...successfully started script 'Stalker2.Main'!
2014-02-22 22:43:52 - Error during Tick in script 'Stalker2.Main':
                      System.NullReferenceException: Object reference not set to an instance of an object.
                         at Stalker2.Main.GetHeading()
                         at Stalker2.Main.SpawnCar()
                         at Stalker2.Main.MainTick(Object sender, EventArgs e)
                         at GTA.Script.TryTick()
                         at GTA.Script.DoTick()
                         at GTA.ScriptThread.OnTick()

 

so check if "car" exists before u get it's heading... i just wrote it quick and tested for 5 minutes... u gotta debug yourself, i'm not trying to write a stalker script... u are ;)

 

btw the only possible way this error happened was because the car spawn failed which tells me your gta is bugging out and u need to reload your game... ur probably running a bunch of mods that might be spawning stuff and interfering with how gta manages spawns... gta tends to breakdown when ur running scripts that spawn peds, vehicles or objects alot ;) You can even try using try catches with spawning and handle it that way or u can check if the ped or vehicle exists before continuing with stuff and exit method if u see spawn fails... j

return Game.Exists(closestVehicle) ? closestVehicle.Heading : Game.Exists(car) ? Helper.DirectionToHeading(Vector3.Normalize(Game.LocalPlayer.Character.Position - car.Position)) : 0f;

Ped ped = World.CreatePed(blabla);
if (!Game.Exists(ped)) return;

 

u need to do that so if a spwan fails u dont try to do stuff with it


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#22

Posted 22 February 2014 - 10:34 PM Edited by LordOfTheBongs, 23 February 2014 - 09:08 PM.

Actually this...

        private float GetHeading()
        {
            if (!Game.Exists(car)) { Game.Console.Print("sh*t IS f*ckED UP... crash imminent if u continue to use car"); return 0f; }

            Vehicle closestVehicle = World.GetClosestVehicle(car.Position, 15f);
            return Game.Exists(closestVehicle) ? closestVehicle.Heading : Helper.DirectionToHeading(Vector3.Normalize(Game.LocalPlayer.Character.Position - car.Position));
        }

both of these should be like this...

        private void SpawnCar()
        {
            if (Game.Exists(car) || Game.Exists(driver) || !Game.LocalPlayer.Character.isInVehicle()) return;//exits method if it sees u already spawned a car, driver from previous spawn hasnt been cleaned up yet or ur not even driving

            car = World.CreateVehicle(GetVehicleModel(), World.GetNextPositionOnStreet(Game.LocalPlayer.Character.Position.Around(75f)));
            if (!Game.Exists(car)) { Game.Console.Print(":*("); return; }
            car.Heading = GetHeading();
            car.isRequiredForMission = true;
            gameTimeStuck = Game.GameTime;
            Blip blip = car.AttachBlip();
            if (!Game.Exists(blip)) { Game.Console.Print("GTA Says... \"ALL OF YOUR BLIPS ARE BELONG TO ME\""); return; }
            blip.Icon = BlipIcon.Person_Unknown;
            blip.Name = "Stalker";
        }

        private void SpawnDriver()
        {
            if (!Game.Exists(car) || Game.Exists(driver) || !Game.LocalPlayer.Character.isInVehicle()) return;//exits method if it sees u already spawned a ped and set to driver, if u never spawned the car or if player not driving

            driver = car.CreatePedOnSeat(VehicleSeat.Driver, "M_Y_GMAF_LO_02");
            if (!Game.Exists(driver)) { Game.Console.Print(":*("); return; }
            driver.BecomeMissionCharacter();
        }




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users