C# Help Request - assigning Ped identifiers after spawn
-
Ok, so I can spawn a ped, assign the name "CurrentPed" and then do whatever I want to script to make that ped do tasks, animate, speak, etc. Each subsequent ped will become "CurrentPed". What I would like to do, is assign an identifier for the first seven peds I spawn. For example, Companion1, Companion2,....Companion7.
So, the process would be: spawn first ped, Companion1 name will be free so use it.
Spawn second ped, test to see if Companion1 is free, in this case it isn't, so use Companion2 etc.
Testing to see if CompanionN exists doesn't work for me.
In psedo code, I have something like this.
if (Companion1.Exists() == false) { Companion1 = CurrentPed; }
//first spawn, initially called CurrentPed
and then use a series of if or else ifs.
if (Companion2.Exists() == false) { Companion2 = CurrentPed; }
//second spawn, initially called CurrentPedcurrent code:
public static Ped CurrentPed { get; set; }
//all other peds, Companion1 etc would also be static so as to be used for scriptingPed PedLara3 = World.CreatePed("Lara3", Game.Player.Character.Position + Game.Player.Character.ForwardVector * 3.0f, Game.Player.Character.Heading + 90);
Function.Call(Hash.SET_PED_COMPONENT_VARIATION, PedLara3, 0, 0, 1, 2);
.......
CurrentPed = PedLara3;
{ EquipCurrentPed(); }
-
If Companion1-7 haven't been set yet, their default value will be null. Check for that in your if statements.
I would much prefer using a List<Ped> instead though. Add the latest ped to the List until the Count == 7.
-
@Jitnaught Thanks. I'm sure this isn't the most elegant solution but it does work.
I set the variables to null in the declarations, then in the code:if (Companion1 == null) { Companion1 = CurrentPed; CurrentPed = null; }
if (Companion2 == null) { Companion2 = CurrentPed; CurrentPed = null; }
and ....I had to set CurrentPed to null with each new spawn, if not any action acted on CurrentPed rather than Companion_n.
I would love to use the list<ped> construct but have no idea how to use it or what assemblies are required. I'm using DN3 and it does not seem to recognize it or I'm just using it wrong. Was not able to find any examples for GTA5. I did find list<string> in non GTA C# examples didn't work either (just as a test, not to replace peds).
I did find references to ADD_PEDS_TO_LIST(); but this is not a native function, at least not in the database or Visual Studio and no code for it in the example i found.
If you have any links/sample code/docs that would greatly appreciated. I will check SHVDN on github.
P.S. My list is not hardcoded, in other words I choose which 7 peds to spawn randomly.
UPDATE: at least i found the missing assembly
using System.Collections.Generic;
So I guess I could use CompanionList.Add(Ped); //but since the peds are spawned at random, how do i assign Companion1, 2, 3, etc?
-
@JohnFromGWN In this short video I spawn 3 peds.
Companion1 is Lara in white
Companion 2 is Ronin in brown. She will aim a gun at the player.
Companion 3 is a Lost Leather gal. She will aim a gun at Ronin/Companion 2.Once spawned I hit a key which ran this:
Companion3.Task.AimAt(Companion2,8000);
Companion2.Task.AimAt(PP, 8000);
//PP is Player
-
Items added to the List are added in order. So the first ped spawned and added to the List would be "Companion1", and the last would be "Companion7". Just instead of saying "Companion1" you would say "Companions[0]" and "Companion7" would be "Companions[6]" (arrays and lists are 0-indexed, they start at 0 instead of 1).
-
@Jitnaught This is very new to me. I've used array Ped [ ] before, like this
Ped[] all_ped = World.GetAllPeds();
foreach (Ped SpawnedPed in all_ped)
// does all Peds, even non-spawned
{
SpawnedPed.Delete();
}
But I've never used List<Ped>. Unfortunately a C# tutorial that spawns peds and adds them to a list doesn't exist outside the GTA5 world and documentation for the latter AFAIK is basically non existent. For example
List<PedHash> list1 = new List<PedHash> { PedHash.Hooker03SFY, PedHash.Autoshop01SMM };
//defeats the purpose of random spawning peds.Crosire also has an example but it is to get closestpeds, not even remotely close.
There are many C# tutorials on how to add items to a list, but the elements are hardcoded or part of loops. The concept of spawning entities is not part of the vocabulary. Another example from MS, once more hard coded.
List<Part> parts = new List<Part>();
// Add parts to the list. parts.Add(new Part() { PartName = "crank arm", PartId = 1234 }); parts.Add(new Part() { PartName = "chain ring", PartId = 1334 }); parts.Add(new Part() { PartName = "regular seat", PartId = 1434 }); parts.Add(new Part() { PartName = "banana seat", PartId = 1444 }); parts.Add(new Part() { PartName = "cassette", PartId = 1534 }); parts.Add(new Part() { PartName = "shift lever", PartId = 1634 });
Need to work at it, not clicking yet.
Thanks for pointing me in the right direction.
-
List<Ped> Companions = new List<Ped>(); if (Companions.Count < 7) //if lower than max companions { CurrentPed = ... create your ped ... Companions.Add(CurrentPed); }
-
@Jitnaught i had tried that as a test, with just 3 peds so without the condition and it compiled but with a runtime exception handler error. So i screwed up somwhere. Will go back and be more careful this time.
THANKS!
-
@Jitnaught Update: still getting run time error on key press. No compiler errors. Frustrating as hell. Probably something stupid. Will keep plugging away. Not sure why Companion is null reference. I'm assuming the Companions should be declared as Ped or is that the issue?
Update: likely some syntax error because it won't run this code either in the function or on the keypress
Companion0.Task.AimAt(PP, 8000);
Thinking syntax error, because this works:
if (Companion0 == null) { Companion0 = CurrentPed; CurrentPed = null; }
Companion0.Task.AimAt(PP, 8000); }
-
@Jitnaught Ok fixed. I'm an idiot. I reread your posts and realized it was not Companion0, it was Companion[0]. In other words not just a suggestion.
Many thanks again.Soundtrack:
-
@Jitnaught A major benefit of the code you provided is that it makes changing characters at run time a breeze. I had originally capped the characters at 7 but that's not necessary unless you want them as companions.
In fact the number of characters you can switch too is theoretically unlimited, limited only by your system and the game engine.
-
Pretty amazing that essentially one line of code, if you remove the constraint and don't count the declaration, can open up so many possibilities.
Companions.Add(CurrentPed);
In this video, with essentially just this one line at the core of the script, I can cycle through unlimited peds - although I've selected 7 to retain as bodyguards/companions - and modify the active ped much quicker than any trainer could. Once active, the clothes, weapons, mood, walking style, etc can be changed. The player remains the player but all spawned characters can be controlled along with the views, effortlessly. Would work well for a movie/machinima.
The ped can be selected in two ways, one by cycling through (will become the CurrentPed) and the other is by Companion[n] which would get tricky unless you give them tshirts with their number on it. JK.
Obviously I'm using my own menu, but that was the easy part - no programming skills required. Going to continue to play with this because it opens so many doors.
Thanks once more for helping a non-programmer.