Resolved Assigning String Variables Incrementally

DuncanMac

Member
Joined
Sep 11, 2021
Messages
6
Programming Experience
1-3
Hi All. Second post here, disclaimer not a programmer. Sorry if topic title is confusing, I'll clarify what I'm trying to do which involves hooking into the GTA5 game engine.

I spawn characters in the game and given them a variable name. For example I would call the first character, Companion1.
Companion1 would be a static variable so as to allow me to interact with that character - animate them, make them talk, etc.
Second/next character spawned would be Companion2, but only if Companion1 is not taken. Similarly, the third character spawned would be Companion3, if 1 and 2 names are not taken.
My problem is how to do I test if CompanionN is available to then use it and increment the next spawn accordingly.

Each time a character is spawned, they are given the name CurrentPed. Each subsequent character is also CurrentPed.
In pseudocode simplified (no else if, no switch):

if (Companion1.Exists() == false) { Companion1 = CurrentPed; } //first spawn, initially called CurrentPed
if (Companion2.Exists() == false) { Companion2 = CurrentPed; } //2nd spawn, initially called CurrentPed
if (Companion3.Exists() == false) { Companion3 = CurrentPed; } //3rd spawn, initially called CurrentPed

Here I'm actually testing if the character exists and it doesn't work. No compiler errors, just run time exception handler error (on a keypress).
So sorry for not TLDRing, but how do I test to see if a variable is already assigned. If it is assigned, I increment, if it isn't assigned I take it.
 
Update. Got this to work:
if (Companion1 == null) { Companion1 = CurrentPed; CurrentPed = null; }
if (Companion2 == null) { Companion2 = CurrentPed; CurrentPed = null; }

Apparently it would be better to use a list. In GTA5 scripting this would be list<ped> where ped is the type for characters, sort of like list<string>. Not sure how to use it though.
 
Instead of having 3 variables, Companion1, Companion2, and Companion3, you should have either a List<Ped> or array: Ped []. Then just run through the indexes.
 
Instead of having 3 variables, Companion1, Companion2, and Companion3, you should have either a List<Ped> or array: Ped []. Then just run through the indexes.

Hi and thanks. Yes, that's my understanding. Figuring it out will be another story. Going to look for some tutorials.
 
Ok. So using a list solved the problem. Thanks.
public static List <Ped> Companion = new List<Ped>();

Companion.Add(CurrentPed);
 
Back
Top Bottom