Download

Please help: C++ Networking, Lobby and Game Room systems

Hello fellow developers,
I’m very new to Unreal and trying to understand it’s parts in very limited amount of time.
At the moment I work on building Room System and sort of Lobby system for clients to find games online and join them, so far I have broken down Shooter Game example and think that I sort of get the flow of such system:


GameInstance::HostGame() -> GameSession::HostSession() -> IOnlineSubsystem::CreateSession()

this is something I’m trying to build and right now I’m experiencing a lot of small problems with this system - From lack of documentation and general Unreal knowledge.

I guess my question is: Is there any good read/watch tutorial online on how to build something like that. Or at least useful tips that you guys can give me for future.

Thank you very much in advance!

What i did was i created a bp version to see what was all needed for lobby/game room, then i just rewrote it in c++.

i released the bp version under the name IronCorgi if you want something to start from, i believe there are better examples though.

Disclaimer: Writing this from memory so will probably be off a bit since I don’t know what I’m doing either.

You won’t find many helpful resources online since no complete explanation exists. You have to make sure you include the right dependencies first off; for public: OnlineSubsystem, OnlineSubsystemUtils, OnlineSubsystemNull, for dynamic: OnlineSubsystemNull. Substitute Null with any other subsystem if that’s what you want to use; like Steam. In your DefaultEngine.ini you will need something like:

[OnlineSubsystem]
DefaultPlatformService=Null

Then in your gamemode or such whenever you want to get a runtime pointer for the online subsystem:

IOnlineSubsystem* OSInst = IOnlineSubsystem::Get();

Then use that to access one of the many sub features. To host, find, join you need the sessions sub feature:

IOnlineSessionPtr SessionInst = OSInst->GetSessionInterface();

You can then use the appropriate methods to create, find, or join a game (CreateSession(…), FindSessions(…), JoinSession(…)). For most of the functions the player ID can be 0 since the engine doesn’t do anything with it at the moment. The delegates you can just set all at once in a function like BeginPlay() - just copy how ShooterGame does it.

MaxL thanks for info! I will try it tonight when I get my hands on the build.

Well I feel kinda stupid asking that but I get this warning:


LogOnline:Warning: NULL: Can't start an online game for session (Game) that hasn't been created

Also trying to repeat ShooterGame’s GetGameSession()



ABWGameSession* UBWGameInstance::GetGameSession() const
{
	UWorld* const World = GetWorld();
	if (World)
	{
		AGameMode* const Game = World->GetAuthGameMode();
		if (Game)
		{
			return Cast<ABWGameSession>(Game->GameSession);
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("Game Instance - GetGameSession - Game* is NULL"));
			return nullptr;
		}
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Game Instance - GetGameSession - World* is NULL"));
		return nullptr;
	}
	return nullptr;
}


always returns me a NULL pointer.

Can you guys help?

What i would do first, is change one of your warning messages so you can see which return is firing.

Wow, apparently i cant read. ignore me

It feels that I’m missing something small but really important. And that drives me crazy :slight_smile:

Thank you guys for trying to help.

I think you’re problem is that you didn’t correctly overload GetGameSessionClass. Try this:



TSubclassOf<AGameSession> ABWGameMode::GetGameSessionClass() const
{
	return ABWGameSession::StaticClass();
}


For some reason you have to set your game session class in this strange manner even though everything else is as simple as setting a variable in game mode’s constructor.
Anyways you need to derive your own game mode and overload GetGameSessionClass like I did above. Then when you call:

GameSession* PreCastGameSession = Cast<ABWGameMode>(GetWorld()->GetAuthGameMode())->GameSession;
ABWGameSession* CastGameSession = Cast<ABWGameSession>(PreCastGameSession);

It should work.

Thank you! I will surely try this out today.
All i’m doing is trying to repeat ShooterGame example from Unreal. but most likely you’re right on that I’m over complicating it.

To be honest for the time being I’ve considered forgetting the online subsystem. I tried to analyse ShooterGame and at least get OnlineSubsystemNull running. I can create sessions, but when I try to find them everything goes awry. There’s no proper documentation either so I rather not age 20 years trying a million things to make it work. At the moment if I want to dabble in online subsystems I’ll probably stick to BP since it seems more straightforward - setting delegates is a billion times easier thanks to the Success/Failed? nodes, hopefully they’ll simplify it for C++ as there’s just too many steps to remember.

Alternatively you can just use the ServerTravel and ClientTravel functions in C++. This works on local/LAN/Internet. Only problem is there’s no master server support as well as the many other nice features you get from the online subsystems.

Edit:

I’m hoping someone could tell me why setting a delegate wasn’t made as simple as MySessionPtr->CreateSession(0, “SessionName”, this, &MyClass::CallThisWhenCreated, &MyClass::CallThisWhenFailed). The advantage here is that I don’t need to remember 4 or so extra steps for Unreal’s custom delegate system.

MaxL Thank man! That truly helped!

Hey there, i just wanted to tell you guys that i plan to get a basic Host Search Join Setup running in c++ and make a small tutorial plus documentatiom about it. I already understood most of the session things and we all know that we need docs about it. It is pretty simple in BP with the nodes but since it is that simple, you can’t really extend it (there is an advanced session nodes Plugin though).

Till now i had no time, but since i need this for my own game, i will try to get this up over the few days/weeks.

I will open up a thread then to gather questions and help, for the parts where i may get stucked (i hope i won’t).

So stay tuned, you are not alone x)

Sounds great eXi (Y). I look forward :).

eXi thank you, such thing in C++ would be extremely helpful.
MaxL - Man I cant thank you enough, you were 100% right. I did not override GetGameSessionClass() in GameMode and that is what been driving everything bad. I got it to whatnot work yesterday. You should have seen my happy dance :slight_smile:

Haha no worries. I was stuck on the same things as you about a week ago.

FYI i started the Thread about gathering information and code pieces to get basic C++ Session handling done:

eXi, you’re amazing :slight_smile: thanks! seems to be very helpful

MaxL I’ve got a question for you:



GameSession* PreCastGameSession = Cast<ABWGameMode>(GetWorld()->GetAuthGameMode())->GameSession;
ABWGameSession* CastGameSession = Cast<ABWGameSession>(PreCastGameSession);


when I try to chase current game session with using this function on the server(master client) it works just fine. But when I try to use this logic on client it breaks the code with “trying to access NULL” on the first line.
Can you please help me? do you know what is the difference between server and client logic for that function?

Thanks in advance!

I can. “GetAuthGameMode()” does not work on Clients, because only the Server has an instance of the GameMode Class. There simply is no spawned GameMode for clients.
Very important to know! (:

eXi yet again thanks a lot!