Quantcast

Jump to content

» «
Photo

Custom GTA Bodyguard script.. please help.

No replies to this topic
XboxHaxorZ
  • XboxHaxorZ

    Player Hater

  • Members
  • Joined: 14 Oct 2007

#1

Posted 12 November 2016 - 03:50 PM

I'm using the .NET script hook, C++/CLI. In need of some advice as to why the bodyguards run back and forth between the player's vehicle and a randomly chosen vehicle. Why won't they stay inside of the chosen vehicle? If there is only one guard with you it works fine, but as soon as you spawn more than 1 in messes up.

 

// PS_BodyGuards.h
// Description: Simple Bodyguard script...
// Coded in C++/CLI (Managed C++)

#pragma once

using namespace System;
using namespace System::Windows::Forms;
using namespace GTA;

namespace PS_BodyGuards {
    public ref class BodyGuards : Script {
    private:
        Group ^group;
        Model PedModel = Model::FromString("M_Y_BOUNCER_02");
        Ped ^target;
        Ped ^looker;

        bool bJustLeftVehicle;
        bool bVehicleFound;

    public:
        BodyGuards() {
            group = Player->Group;
            target = nullptr;
            looker = nullptr;

            bJustLeftVehicle = true;
            bVehicleFound = false;

            Interval = 250;
            this->KeyDown += gcnew GTA::KeyEventHandler(this, &BodyGuards::BodyGuards_KeyDown);
            this->Tick += gcnew EventHandler(this, &BodyGuards::BodyGuards_OnTick);
        }

    private:
        void BodyGuards_KeyDown(Object^ sender, GTA::KeyEventArgs^ e) {
            switch (e->Key) {
                
            case Keys::Insert:
                if (group->MemberCount < 3) { // limit to 3 friends, so they all fit in a 4-door vehicle
                    Ped^ p = World::CreatePed(PedModel, Player->Character->Position.Around(3.0f), RelationshipGroup::Player);
                    AddToGroup(p);
                    // Set the looker
                    if (group->MemberCount == 1 && looker == nullptr)
                        looker = p;
                }
                break;

            case Keys::Delete:
                for each(Ped ^member in group) {
                    member->Weapons->RemoveAll();
                    member->Die();
                    member->NoLongerNeeded();
                }
                group->RemoveAllMembers();
                if (looker != nullptr)
                    looker = nullptr;
                break;

            case Keys::T:
                for each(Ped ^member in group) {
                    if (Exists(member)) {
                        target = Player->GetTargetedPed();
                        if (Exists(target)) {
                            member->Task->ClearAll();
                            member->Task->AlwaysKeepTask = true;
                            member->Task->FightAgainst(target);
                        }
                    }
                }
                break;

            }
        }

        /*
         * Function that controls, the dudes.
         */
        void BodyGuards_OnTick(Object^ sender, EventArgs ^e) {
            if (Player->Character->isInVehicle())
                bJustLeftVehicle = false;
            else
                bJustLeftVehicle = true;

            // TODO : Fix this!
            // handles the vehicle searching member
            if (Exists(looker) && looker != nullptr && group->MemberCount > 1 && group->MemberCount < 3 && !bVehicleFound) {
                if (!bJustLeftVehicle && Player->Character->isInVehicle() && !Player->Character->CurrentVehicle->isSeatFree(VehicleSeat::AnyPassengerSeat)) {
                    array<Vehicle^> ^vehArrRef = World::GetVehicles(looker->Position, 15.0f);
                    for each(Vehicle ^veh in vehArrRef) {
                        if (Exists(veh) && veh != nullptr && Exists(Player->Character->CurrentVehicle)) {
                            if (veh != Player->Character->CurrentVehicle) {
                                looker->Task->EnterVehicle(veh, VehicleSeat::Driver);
                                bVehicleFound = true;
                                break;
                            }
                        }
                        else
                            bVehicleFound = false;
                    }
                }
            }
            
            // TODO: Fix this!
            // handles all other group members
            for each(Ped ^member in group) {
                if (Exists(member) && Exists(looker)) {
                    if (Player->Character->isInVehicle() && bVehicleFound) {
                        if (member != looker && Exists(looker->CurrentVehicle) && looker->CurrentVehicle->isSeatFree(VehicleSeat::AnyPassengerSeat) && !bJustLeftVehicle) {
                            member->Task->ClearAll();
                            member->Task->EnterVehicle(looker->CurrentVehicle, VehicleSeat::AnyPassengerSeat);
                        }
                    }
                }
            }

            // My Targetting Mess
            if (Exists(target) && target != nullptr) {
                if (!target->isAlive) {
                    for each(Ped ^member in group) {
                        member->Task->AlwaysKeepTask = false;
                        member->Task->ClearAll();
                    }
                    target = nullptr;
                }
            }
        }

        void AddToGroup(Ped ^p) {
            if (!Exists(p)) return;  // won't cause an exception this way

            // Setup normal npc crap that I want
            p->CurrentRoom = Player->Character->CurrentRoom;
            p->WillDoDrivebys = true;
            p->PriorityTargetForEnemies = true;
            p->DuckWhenAimedAtByGroupMember = false;
            p->AlwaysDiesOnLowHealth = false;
            p->SetPathfinding(true, true, true);

            // Setup what kind of guns they have
            if (Game::CurrentEpisode == GameEpisode::TLAD) {
                p->Weapons->FromType(Weapon::TLAD_Automatic9mm)->Ammo = 30000;
                p->Weapons->FromType(Weapon::TLAD_AssaultShotgun)->Ammo = 30000;
            }
            else {
                p->Weapons->FromType(Weapon::Rifle_AK47)->Ammo = 30000;
                p->Weapons->FromType(Weapon::Handgun_DesertEagle)->Ammo = 30000;
            }
            p->Weapons->Select(Weapon::Misc_AnyWeapon);

            // Setup the group to player
            p->RelationshipGroup = RelationshipGroup::Player;
            p->ChangeRelationship(RelationshipGroup::Player, Relationship::Companion);
            p->CantBeDamagedByRelationshipGroup(RelationshipGroup::Player, true);

            group->AddMember(p);
            Game::DisplayText(group->MemberCount.ToString() + " members in gang");
        }
    };
}

 

Any help at all leading to a solution to this problem will be greatly appreciated. Thanks in advance.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users