Resolved How to toggle between two states

DuncanMac

Member
Joined
Sep 11, 2021
Messages
6
Programming Experience
1-3
I'm a non-programmer and absolute beginner using C#. I'm using C#.NET with VS 2019 to modify the video game GTA5 for scholastic purposes (albeit without the violence and swearing). I'm progressing well but would like some help with a new challenge. I have successfully written code below, that allows me on one key press to change the player in the game to another character - so this character becomes the player. For example I press L.

Pressing a second key binding, for example O, will allow me to go back to the original player. I currently have this working with two similar functions, each assigned a key press.

But if I press the same key twice it will freeze the game. I believe this happens because the game thinks there isn't any player.

I need help to prevent the error through a conditional statement, I suppose. Ideally it would be great if I could toggle from player to the other character and back again with only one key press? My code which works using what are called native functions of the RAGE game engine. Please ignore the last 2 booleans they are not relevant. In passing, the GTA5 community is quite dead with respect to coding which is why I'm here.

In summary I'm asking for help as to how to initialize the Teacher as player, and then if i hit the key again it goes to Student. Next key press would be back to Teacher.
My knowledge is very limited, my day job is a teacher. This project is to help my students learn through gaming as the pandemic has been very difficult for education.

if (e.KeyCode == Keys.L) //Switch from Player Teacher at start to new Player Student
{
Teacher = Game.Player.Character;
Function.Call(Hash.CHANGE_PLAYER_PED, Game.Player, Student, true, true); //Change Player Ped means change the Player to the student
}

if (e.KeyCode == Keys.O) //Switch from Student current player to Original Player Teacher
{
Student = Game.Player.Character;
Function.Call(Hash.CHANGE_PLAYER_PED, Game.Player, Teacher, true, true); //Change Player Ped here means change the Player back to the teacher
}
 
Last edited:
Think about what you're actually asking for and that will give you a pretty good idea of what code to write. If the user presses the L key, set the player to Teacher if it is not already. Notice that that sentence has the word "if" in it twice? What does that tell you about the code you need? Perhaps that it requires two if statements? You already have the first one here:
C#:
if (e.KeyCode == Keys.L)
Now you need code for the second one. You need to test whether the player is already Teacher and change it to Teacher if it's not. I can't provide specific code because I don't know what it would be in that context but either you do know or you can find out. My guess would be that you need to call Function.Call with a different first argument to get the player identifier rather than set it, then compare that identifier to something.
 
Thanks but I solved it. The problem with an issue like this one was that it would freeze the game at run time, and its a long load - so trial and error was really not an efficient way to go.

You know it's really challenging to do what I'm doing because it involves both C# and a very poorly documented set of custom libraries providing a hook into a game engine not designed to be modified - but I'm doing the best I can for my students and it is rewarding to progress.

if (Game.Player.Character == Teacher) Function.Call(Hash.CHANGE_PLAYER_PED, Game.Player, Student, true, true); //we start with Teacher as player, switch player to Student

else if (Game.Player.Character == Student) Function.Call(Hash.CHANGE_PLAYER_PED, Game.Player, Teacher, true, true); //if player is student, we switch back to the teacher

Your idea to get the player id is a good one, but I couldn't find a function to do that. What is missing in the code above is I had to declare the Teacher variable.

public static Ped Teacher { get; set; } //Ped means pedestrian or NPC entity in GTA5.

I'll post the complete code below if anyone from the GTA5 community stumbles upon this.

Actual Code C# - GTA5 RAGE Engine:
 if (e.KeyCode == Keys.O)
            {
                if (Game.Player.Character == Player1)
                {
                    Function.Call(Hash.CHANGE_PLAYER_PED, Game.Player, CurrentPed, true, true); // Player 1 is already declared
                    PedGroup PlayerGroup = Game.Player.Character.PedGroup;  //make them buddies
                    Function.Call(Hash.SET_PED_AS_GROUP_MEMBER, Player1, PlayerGroup);
                }
                else if (Game.Player.Character == CurrentPed)
                {   Function.Call(Hash.CHANGE_PLAYER_PED, Game.Player, Player1, true, true);
                    PedGroup PlayerGroup = Game.Player.Character.PedGroup; //make them buddies
                    Function.Call(Hash.SET_PED_AS_GROUP_MEMBER, CurrentPed, PlayerGroup);
                }
           }
 
Last edited:

Latest posts

Back
Top Bottom