Kieran_S Posted February 14, 2021 Share Posted February 14, 2021 Hi, I have been trying to make a code so that when I all killed all 13 of spawned in peds it will take me onto the next stage of the code. I made this if (Lost1.IsDead && Lost2.IsDead && Lost3.IsDead && Lost4.IsDead && Lost5.IsDead && Lost6.IsDead && Lost7.IsDead && Lost8.IsDead && Lost9.IsDead && Lost10.IsDead && Lost11.IsDead && Lost12.IsDead && Lost13.IsDead) but as you can see it is really long and it does not move me onto the next part. Any help would be great. Thanks. Link to comment Share on other sites More sharing options...
LeeC22 Posted February 14, 2021 Share Posted February 14, 2021 (edited) 4 hours ago, Kieran_S said: Hi, I have been trying to make a code so that when I all killed all 13 of spawned in peds it will take me onto the next stage of the code. I made this if (Lost1.IsDead && Lost2.IsDead && Lost3.IsDead && Lost4.IsDead && Lost5.IsDead && Lost6.IsDead && Lost7.IsDead && Lost8.IsDead && Lost9.IsDead && Lost10.IsDead && Lost11.IsDead && Lost12.IsDead && Lost13.IsDead) but as you can see it is really long and it does not move me onto the next part. Any help would be great. Thanks. My first thought would be, are all those peds still in the game by the time you kill the last one? After a period of time, the game will clear up dead bodies and that might mean that the first ones no longer exist, meaning they can't be dead. In that case, you would also need to check if they still exist first... but of course that makes your if() check even worse. I don't ever do things with collections of peds really but my instinct would be to add them to a List<Ped> when you spawn them and then in each update, just run through that list backwards checking if each ped exists and is still alive. If it fails, remove it from the List, when the list is empty, everyone is dead or doesn't exist anymore. The reason you do it backwards is because you can't modify the start of a List when you're processing it. But if you get your index from myList.Count - 1 to zero, then you can remove the entry at the current index, because it's at the end. Edit: Not forgetting to null the reference you have removed as well, so things are fully cleaned up by the time everyone is dead. Edited February 14, 2021 by LeeC22 Link to comment Share on other sites More sharing options...
Kieran_S Posted February 14, 2021 Author Share Posted February 14, 2021 4 minutes ago, LeeC22 said: My first thought would be, are all those peds still in the game by the time you kill the last one? After a period of time, the game will clear up dead bodies and that might mean that the first ones no longer exist, meaning they can't be dead. In that case, you would also need to check if they still exist first... but of course that makes your if() check even worse. I don't ever do things with collections of peds really but my instinct would be to add them to a List<Ped> when you spawn them and then in each update, just run through that list backwards checking if each ped exists and is still alive. If it fails, remove it from the List, when the list is empty, everyone is dead or doesn't exist anymore. The reason you do it backwards is because you can't modify the start of a List when you're processing it. But if you get your index from myList.Count - 1 to zero, then you can remove the entry at the current index, because it's at the end. I have then all in this List<Ped> Peds = new List<Ped>(); already so all I have to do is go from lost 13 to lost 1 checking if they are alive and exists with an if/else? Link to comment Share on other sites More sharing options...
LeeC22 Posted February 14, 2021 Share Posted February 14, 2021 (edited) 43 minutes ago, Kieran_S said: I have then all in this List<Ped> Peds = new List<Ped>(); already so all I have to do is go from lost 13 to lost 1 checking if they are alive and exists with an if/else? Yep but you don't reference them as Lost1, Lost2 etc... when you're checking, you just reference them as Peds[index] instead. I mean if you never have any of them do anything specific, then you could just create them straight into the list, with Peds.Add(World.CreatePed... ) but if you needed Lost7 to do something unique for instance, then doing that isn't ideal. for (int index = Peds.Count - 1; index >= 0; index--) { if (!Peds[index].Exists() || Peds[index].IsDead) { Peds.RemoveAt(index); } } if (Peds.Count == 0) { // Move onto next stage } Edit: If you are adding the Lost1... Lost13 references to your list, then before you do the Peds.RemoveAt(index), do Peds[index] = null first Edited February 14, 2021 by LeeC22 Link to comment Share on other sites More sharing options...
Kieran_S Posted February 14, 2021 Author Share Posted February 14, 2021 35 minutes ago, LeeC22 said: Yep but you don't reference them as Lost1, Lost2 etc... when you're checking, you just reference them as Peds[index] instead. I mean if you never have any of them do anything specific, then you could just create them straight into the list, with Peds.Add(World.CreatePed... ) but if you needed Lost7 to do something unique for instance, then doing that isn't ideal. for (int index = Peds.Count - 1; index >= 0; index--) { if (!Peds[index].Exists() || Peds[index].IsDead) { Peds.RemoveAt(index); } } if (Peds.Count == 0) { // Move onto next stage } Edit: If you are adding the Lost1... Lost13 references to your list, then before you do the Peds.RemoveAt(index), do Peds[index] = null first OK, Thanks so I just replace my current long if() line with that? I do not what any of the peds doing anything specific. They just fight against the player. Link to comment Share on other sites More sharing options...
LeeC22 Posted February 14, 2021 Share Posted February 14, 2021 (edited) 1 hour ago, Kieran_S said: OK, Thanks so I just replace my current long if() line with that? I do not what any of the peds doing anything specific. They just fight against the player. Yeah, that should work fine. I should add at this point that there is a situation that might cause you problems and that's if you abort any mission part way through after some have died and you have lost any references to peds, meaning you can't delete the dead peds from the map. Only you know the full plan for the mod, so any solutions given have to be considered with how you want the mod to function. There's always a danger of someone providing a solution that might just as easily cause you a problem in another way, so keep that in mind with any code you get. You could create a second List<int> that contains all the Handles for the peds and as each ped dies, you remove its corresponding Handle from the other list and then check if that list is empty, leaving your initial list with all the ped references you can still deal with if required. Edited February 14, 2021 by LeeC22 Link to comment Share on other sites More sharing options...
Kieran_S Posted February 14, 2021 Author Share Posted February 14, 2021 9 minutes ago, LeeC22 said: Yeah, that should work fine. I should add at this point that there is a situation that might cause you problems and that's if you abort any mission part way through after some have died and you have lost any references to peds, meaning you can't delete the dead peds from the map. Only you know the full plan for the mod, so any solutions given have to be considered with how you want the mod to function. There's always a danger of someone providing a solution that might just as easily cause you a problem in another way, so keep that in mind with any code you get. You could create a second List<int> that contains all the Handles for the peds and as each ped dies, you remove its corresponding Handle from the other list and then check if that list is empty, leaving your initial list with all the ped references you can still deal with if required. I put that code in script, however, it did not work and I am still stuck on the same case. Is there any other methods? Link to comment Share on other sites More sharing options...
LeeC22 Posted February 14, 2021 Share Posted February 14, 2021 39 minutes ago, Kieran_S said: I put that code in script, however, it did not work and I am still stuck on the same case. Really? It seems to work fine for me. if (Peds.Count == 0) { UI.ShowSubtitle("All peds are dead"); } else { for (int index = Peds.Count - 1; index >= 0; index--) { if (!Peds[index].Exists() || Peds[index].IsDead) { Peds[index] = null; Peds.RemoveAt(index); } } UI.ShowSubtitle($"Peds Left: {Peds.Count}"); } Kieran_S 1 Link to comment Share on other sites More sharing options...
Kieran_S Posted February 14, 2021 Author Share Posted February 14, 2021 (edited) 24 minutes ago, LeeC22 said: Really? It seems to work fine for me. if (Peds.Count == 0) { UI.ShowSubtitle("All peds are dead"); } else { for (int index = Peds.Count - 1; index >= 0; index--) { if (!Peds[index].Exists() || Peds[index].IsDead) { Peds[index] = null; Peds.RemoveAt(index); } } UI.ShowSubtitle($"Peds Left: {Peds.Count}"); } Hmmm, OK. I'll try it again tomorrow. Maybe there is a bug somewhere else not sure. I'll look into it more tomorrow. I gave it a try and when I have killed all the peds it still says there is 1 left in the corner. I realised that one of the peds where spawning the other side of the map as I missed a - on the vector3. My bad. Thanks for the help Edited February 14, 2021 by Kieran_S Edit LeeC22 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now