Hi guys I see this is a really common problem that people are facing.
I was wondering if this would work. I’m not at my computer so I can’t test it .
Inside the game mode class there is an event called post login. The description states
“Event Post Login”, this will fire when a player joins the game and was assigned a Player Controller.
So my theory is this could we not create a custom event which we call using the post login event.
Inside this event We get all instances of the player controllers class that are in the game/connected to the server and add them to an array.
Then assign each element in the array an id then pass this id back into the player controller as player number. Eg if controller 1 is added at index 0 in array = we take that array index and assign it the id for player 1
Controller 2 is at index 1 in array = we assign it id for player 2 and so on .
Can any one try this or have any theories why this wouldn’t work.??
Yes, you can use it. But i wouldn’t use “GetAllActorsOfClass”. You get a PostLogin call (GameMode only exists on the Server) every time
a new player joins. And you get the Controller. So better add the player to your own PlayerController array at this point.
There are events in the PlayerController (i think it was “EndPlay” or so) which are called when the Actor is destroyed.
You could use this to tell the GameMode to remove the PlayerController again.
Also you could directly give the Player this ID. Make a INT Value in the GameMode that you increase if a Player joins (assign it).
You just need to think about player leaving the game with a number between the lowest and the highest.
What keeps you from making a team selection screen when the Player joins?
We’ll what I’m trying to do is when a player joins they are given a number between 1 and 5. This is there player number. There will also be 5 spawn points . So player 1 would spawn at spawn point 1, player 2 at spawn point 2 etc.
Then I also need to assign each player into a team let’s say red or blue.
Once each team has 5 players the game will start. So it’s basically 5vs5 max 10 players.
But not sure the best way of going about getting the player numbers assigned to the player controllers and then the team.
I thought I coul use the controller id as an identifier but then each time the game starts this number changes. So it’s useless as an identifier .
God it’s so annoying , why is something that should be easy to do so hard to do lol.
I tried the whole night to get it working. The closest I got was using the random range int node starting from 1-5 then when the user presses the play button on the widget it randomly assigned this number to a variable in the custom player controller called player number.
This works half the time but obviously is flawed because sometimes it picks the same random number so you end up with two players with the same player number.
I tried using the post login event in game mode . This uses the default player controller so I tried casting it to my custom controller then just using the set variable node to set the variable of player number . But the problem with this is it sets every players controller player number variable to the same thing .
I tried using arrays and loops but not entirely sure if they we’re done properly.
Here is what I do on Event Begin Play inside of my character BP, I realize that this is not the best way, I just needed something quick & dirty to get the job done. Something you could do is randomize the array before looping, that would solve the problem of random indexes.
Thanks but you’re blueprint is confusing me . I understand you’re basically getting all the actors putting them into an array then for each element you’re using a loop and setting the name. But for value you have the variable player name so when you execute this blueprint wouldn’t it change all elements in the loop to the same value , which is the problem I had ?? I don’t see any where in that blueprint screen shot where you change the value of player name??
I just don’t get it though. In theory to make the player number all we are doing is takin the variable and adding 1 to it.
Example playerNumber it’s default value is 0
I press the button it takes the default value and adds 1 to it
So playerNumber is now 1
I press the button and playerNumber is now 2
If I was to make a onkey pressed event like The j key
Then in the event I drag out the variable and click set variable from that variable I drag out an int+int node and set the value to 1 from the value I drag out and do a print screen node.
Then compile and run every time I presse the j key it would print out the value as 1 then 2 then 3
So why is it tha I do the exact same logic except instead of pressing j key I’m simply saying everytime the widget button is pressed add 1 to the variable . It’s the exact same thing so it should work .
Yet in reality when I press the widget button it does this
Press widget button , it gets the default value of playerNumber which to start is 0 then it follows the logic I want which is it takes this number and adds 1 to it the result is playerNumber now equals 1 .
Howether the problem is when you press the widget button again
In theory it should go , get playerNumber which should now be 1 , then add 1 to it and the result would be 2
But instead it reverts back to the default value so on the second press
It actually goes , gets playerNumber = 0 , add 1 so result again is 1
We now have 2 players both with the player number 1.
I’m testing it on multiplayer. It doesn’t matter who presses the widget the outcome is the same. I havnt tried clicking the button twice on the same player as after the player clicks the button hides itself. So I guess I can try that and see if the outcome is the same.
I can’t see that being the issue though as when I tried just assigning random numbers this worked.
The problem seems to be the variable resets to default value rather than persisting inbetween button presses so I’m Wondering if maybe it is a replication issue rather than a logic issue
Yes but what I’m saying is what is each name set to? I don’t see any names and only one value is connected or is that an array of names as we’ll ?? Or actually on close look are you just converting the name string into an integer and this integer is what you are setting as the name?
I don’t think I could use you’re system though as mine is called on a button press where as youres is on event begin play. Which would cause trouble . Let’s say a new player joined you would have to call the whole event all over again . But thanks
well on further playing around it seems that the logic is correct. When i click on the button it is getting the variable and adding one to it.
So if i click 3 times the result comes out as 3 which is what i wanted.
It seems the trouble is that i cant click on the button on one client and then click the button on another client and add it together.
The variable is not global. So each client has its own version of the variable .
Eg i click 4 times on client 1 the player number would read as 4
on client 2 the player number would still read as 0
i click twice on client 2 the player number would read as 2
client 1 would still read as 4
So unless there is some way to add a global variable in blueprints i don’t see how we can get this to work.
You cant use level blueprint or game mode as you cannot cast to it or access its variables, and even if you can which i think they might be sorting out in version 4.9 . Would this variable be global and persist for each of the clients or would the problem be the same??.
Not only can you assign the player number to each player/player controller but you can also spawn them into either a random spawn location or a prefixed one , and also you can set the maximum number of players that you would like to allow to spawn, and either set them to a fixed team or random team which is another thing i see a lot of people asking about in forums, and once i have the player number assigned and its unique to each player controller it now means that i can do so many different things just by checking that number value against other stuff.
yeah that’s sort of how i did it in the end, the reason it took me ages to realize game state could work is because there was conflicting information on the forums. As i was researching for ideas on how to overcome this problem a lot of threads were saying that you cannot cast to the game state, and that there was no way way make global variables. Which i have now found out is not true or maybe epic have changed how it works in the newer versions of the engine. And i used a game instance rather than the actual game state. not sure if that’s the same thing im still very new to unreal lol.