Here's the general overview:
- I'm creating a mod menu.
- I'm still fairly new to C# and the GTAV local natives.
- The game crashes whenever I run the following functions: DELETE_PED, DELETE_ENTITY, and REMOVE_PED_ELEGANTLY.
All I want to do is delete a spawned ped, because they never seem to despawn like naturally-spawning game peds.
What the code does:
- Creates a new pedestrian when a UI button is selected & adds it to a Ped List.
- A tick is set to constantly run through the function(removeDeadEntity()) to check if the ped is both dead and not moving.
- In the removeDeadEntity() function, it loops through the length of the list, checking, like what was previously stated, if the ped is both dead and not moving.
- The only problem is, is the game crashes every time when both conditions are met in the if() statement.
Code: (I removed the code that shouldn't have any affect.)
(If you need to actually run the mod menu, here's the link >> https://mega.nz/#!EzRw3DpY!JBXNCzlhoRJBjRBPQzQQWdZmnbZA1k1SbnElQBDjsg0 (Press "Z" to activate.))
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using GTA;
using GTA.Native;
using NativeUI;
using GTA.Math;
public class MenuExample : Script { // Main Script
private MenuPool _menuPool; // MenuPool
private Player player = Game.Player;
private Ped playerPed = Game.Player.Character;
List<Ped> spawnedPedList = new List<Ped>();
// This is where Menu1 is created
public void Menu1(UIMenu menu) {
// This is where the submenus and/or buttons are created
var playerOptions = _menuPool.AddSubMenu(menu, "Player Options");
// This is where the pedestrian menu opens/is created
public void Menu2(UIMenu menu) {
var submenu1 = _menuPool.AddSubMenu(menu, "Pedestrian Menu");
for (int i = 0; i > 1; i++) { // Enables the submenu
}
var pedMoods = new List<dynamic> {
"Angry ped",
"Happy ped",
"Gang War ped"
};
var spawnedPedMoodList = new UIMenuListItem("Set ped mood:", pedMoods, 0);
var spawnPed = new UIMenuItem("Spawn a person");
int spawnedPedMoodType = 0;
submenu1.AddItem(button1);
submenu1.OnItemSelect += (sender, item, index) => { // Checks if the button is selected
if(item == spawnPed) {
Vector3 position = Game.Player.Character.Position + Game.Player.Character.ForwardVector * 5;
Ped spawnedPed = World.CreatePed(new Model(PedHash.Soucent04AMM), position);
Function.Call(Hash._0xD30C50DF888D58B5, spawnedPed.Handle, true);
spawnedPed.Weapons.Give(WeaponHash.MicroSMG, 9000, true, true);
Function.Call(Hash.SET_PED_COMBAT_ABILITY, spawnedPed, 100); // 100 = attack
spawnedPedList.Add(spawnedPed);
if(spawnedPedMoodType == 0) {
spawnedPed.Task.FightAgainst(Game.Player.Character);
}
else if(spawnedPedMoodType == 1) {
Function.Call(Hash.TASK_WANDER_STANDARD, 10.0f, 10);
}
else if(spawnedPedMoodType == 2) {
spawnedPed.Task.ShootAt(spawnedPed.Position);
}
}
};
submenu1.AddItem(spawnedPedMoodList);
submenu1.AddItem(spawnPed);
submenu1.OnListChange += (sender, item, index) => {
if (item == spawnedPedMoodList) {
if(index == 0) {
spawnedPedMoodType = 0;
}
else if(index == 1) {
spawnedPedMoodType = 1;
}
else if(index == 2) {
spawnedPedMoodType = 2;
}
}
};
} //MENU 2 END
//MY FUNCTIONS
public void removeDeadEntity() {
Vector3 stillPosition = new Vector3(0f, 0f, 0f);
if (spawnedPedList.Count != 0) {
for (int i = 0; i < spawnedPedList.Count; i++) {
if (Function.Call<Vector3>(Hash.GET_ENTITY_VELOCITY, spawnedPedList[i]) == stillPosition && Function.Call<bool>(Hash._IS_PED_DEAD, spawnedPedList[i])) {
Function.Call(Hash.DELETE_PED, spawnedPedList[i]);
}
}
}
}
//MY FUNCTIONS
// Main Base, including the menu name, hotkeys etc
public MenuExample() {
_menuPool = new MenuPool(); // MenuPool
var mainMenu = new UIMenu("~g~MeestaUI Mod Menu", "~b~Happy modding");
_menuPool.Add(mainMenu); // Adds the main menu
Menu1(mainMenu);
Menu2(mainMenu);
_menuPool.RefreshIndex(); // Refreshes the Index
Tick += (o, e) => _menuPool.ProcessMenus(); // Constant tick, processes the menus
Tick += (o, e) => removeDeadEntity();
KeyDown += (o, e) => { // Enables hotkey(s)
if (e.KeyCode == Keys.Z && !_menuPool.IsAnyMenuOpen()) {
mainMenu.Visible = !mainMenu.Visible;
}
};
}
}
Thanks for the help!