Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!   (85,296 visits to this link)

    2. News

    1. GTA Online

      1. Find Lobbies & Players
      2. Guides & Strategies
      3. Vehicles
      4. Content Creator
      5. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption 2

    2. Red Dead Redemption

    3. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

minhkhoi0975

[C#] The followers don't attack the enemies.

Recommended Posts

minhkhoi0975

Hi, I have a problem with my C# script. I wrote a script for GTA IV that makes peds on the street become my followers. However, when I am in combat with the cops or other enemies, the followers try to run away instead of helping me fight against them. Are there any ways to make my followers attack the enemies ?

Here is my source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GTA;

namespace HoboHelper
{
public class HoboHelperScript:Script
{
Group PlayerGroup;

public HoboHelperScript()
{
PlayerGroup = Player.Group;
this.BindKey(Keys.G, new KeyPressDelegate(GetHobo));
this.Tick += new EventHandler(HoboHelper_Ticks);
}

public void HoboHelper_Ticks(object sender, EventArgs e)
{
Ped[] Pedgroup = World.GetPeds(Player.Character.Position, 3.0F);
if (Pedgroup == null) return;

foreach (Ped p in Pedgroup)
{
if (!Exists(p) || p == null) return;
if (p.RelationshipGroup == RelationshipGroup.Player) continue;

//Help a hobo.
if (PedHasModelInGroup(p, HoboModel) == true)
{
if (Player.Money < 100) return;
GiveMessage("Press G to help the tramp ($100).", 2000);
}

//Hire a criminal.
else if (PedHasModelInGroup(p, CriminalModel) == true)
{
if (Player.Money < 500) return;
GiveMessage("Press G to hire the criminal ($500).", 2000);
}
}
}

private void GetHobo()
{
Ped[] Pedgroup = World.GetPeds(Player.Character.Position, 3.0F);
if (Pedgroup == null) return;

foreach (Ped p in Pedgroup)
{
if (!Exists(p) || p == null) return;
if (p.RelationshipGroup == RelationshipGroup.Player) continue;

//Help a hobo.
if (PedHasModelInGroup(p, HoboModel) == true || p.PedType == PedType.Bum)
{
if (Player.Money < 100) return;

p.WillDoDrivebys = true;
p.AlwaysDiesOnLowHealth = true;
p.PriorityTargetForEnemies = true;
p.DuckWhenAimedAtByGroupMember = true;
p.SetPathfinding(true, true, true);

GetRandomWeapon1(p);

p.RelationshipGroup = RelationshipGroup.Player;
p.ChangeRelationship(RelationshipGroup.Player, Relationship.Companion);
PlayerGroup.AddMember(p);

Player.Money -= 100;
GiveMessage("Thank you for giving me money. Let me do something for you.", 5000);
}

//Hire a criminal.
else if (PedHasModelInGroup(p, CriminalModel) == true || p.PedType == PedType.Criminal || p.PedType == PedType.Dealer)
{
if (Player.Money < 500) return;

p.WillDoDrivebys = true;
p.AlwaysDiesOnLowHealth = true;
p.PriorityTargetForEnemies = true;
p.DuckWhenAimedAtByGroupMember = true;
p.SetPathfinding(true, true, true);

GetRandomWeapon2(p);

p.RelationshipGroup = RelationshipGroup.Player;
p.ChangeRelationship(RelationshipGroup.Player, Relationship.Companion);
PlayerGroup.AddMember(p);

Player.Money -= 500;
GiveMessage("Let's have some fun.", 5000);
}
}
}


private void GetRandomWeapon1(Ped p)
{

Random rd = new Random();
int WeaponChoice = rd.Next(11);

switch (WeaponChoice)
{
case 0: //The weapon the player is carrying.
Weapon weap = Player.Character.Weapons.CurrentType;
if (weap == Weapon.Unarmed) //In case the player is unarmed.
{
p.Weapons.Select(Weapon.Melee_Knife);
}
else
{
p.Weapons.FromType(weap).Ammo = 9999;
p.Weapons.Select(weap);
}
break;

case 1:
case 2: //Glock pistol.
p.Weapons.Glock.Ammo = 9999;
p.Weapons.Select(Weapon.Handgun_Glock);
break;

case 3:
case 4:
case 5:
case 6: //Baseball bat.
p.Weapons.Select(Weapon.Melee_BaseballBat);
break;

case 7:
case 8:
case 9:
case 10: //Knife.
p.Weapons.Select(Weapon.Melee_Knife);
break;
}
}
private void GetRandomWeapon2(Ped p)
{
Random rd = new Random();
int WeaponChoice = rd.Next(6);

switch (WeaponChoice)
{
case 0: //Glock
p.Weapons.Glock.Ammo = 9999;
p.Weapons.Select(Weapon.Handgun_Glock);
break;

case 1: //Desert Eagle
p.Weapons.DesertEagle.Ammo = 9999;
p.Weapons.Select(Weapon.Handgun_DesertEagle);
break;

case 2: //Uzi
p.Weapons.Uzi.Ammo = 9999;
p.Weapons.Select(Weapon.SMG_Uzi);
break;

case 3: //MP5
p.Weapons.MP5.Ammo = 9999;
p.Weapons.Select(Weapon.SMG_MP5);
break;

case 4: //AK47
p.Weapons.AssaultRifle_AK47.Ammo = 9999;
p.Weapons.Select(Weapon.Rifle_AK47);
break;

case 5: //The weapon the player is carrying
Weapon weap = Player.Character.Weapons.CurrentType;
if (weap == Weapon.Unarmed) //In case the player is unarmed.
{
p.Weapons.Select(Weapon.Melee_Knife);
}
else
{
p.Weapons.FromType(weap).Ammo = 9999;
p.Weapons.Select(weap);
}
break;
}
}

private void GiveMessage(string text, int time)
{
GTA.Native.Function.Call("PRINT_STRING_WITH_LITERAL_STRING_NOW", "STRING", text, time, 1);
}

private bool PedHasModelInGroup(Ped p, Model[] modelgroup)
{
for (int i=0;i<=modelgroup.Length-1;i++)
{
if (p.Model == modelgroup) return true;
}
return false;
}

Model[] HoboModel = { "M_M_CRACKHEAD", "M_M_GENBUM_01", "M_M_TRAMPBLACK" };
Model[] CriminalModel = { "M_Y_BRONX_01", "M_Y_DEALER", "M_Y_DRUG_01", "M_Y_RUNNER", "M_Y_SHADY_02" };
}
}

 

Edited by minhkhoi0975

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.