Unreal Engine Main Int [Help]

Hi all, ive created before started working with UE a basic client that connect at a server with a chat server too, but now I need to implement it to my UE4 project.

Where can i find the int main()? to add it.

Also the basic client is written for a CMD project, how can i change it to be compatible with UE?

Thanks in advance

That’s not going to work. First off, all of that functionality is probably already in UE4, and cross-platform, too.

I know from another thread that you’re attempting to make an MMO. What is your actual goal?

You don’t get to live inside main(), because the lifetime of different parts of the Unreal framework is managed by Unreal, not by main().
Your best bet is to create an object that wraps your network code, and spawns a new thread that runs whatever you need to run. Then use thread-safe queues to communicate back and forth between that thread and the main Unreal code.
You could create an object that owns this thread; when you begin play, the object creates the thread; when you end play, the object signals your system to shut down and then waits for the thread to complete.
That way, your code will peacefully coexist with the Unreal framework, and you can write it as a plug-in without changing any core Unreal code. This is great for when you need to upgrade the code base for newer versions!

hm i didnt under stand u sry im new to unreal :3

Forget main().

What are you trying to achieve?

ive created this:

its a simple client program that connect to a server, with chat too.
i want the program to do the same to connect to the server

It sounds to me more like you’re new to C++ programming in general.

Unreal manages the lifetime of UObjects. Typically, you will make your UObject and Actor so it’s easy to manage from the game code.
You should implement an Actor (or other UObject) that starts a thread. The function for that thread should be a (renamed) version of your main() function.

However, the C++ code you show is 100% incompatible with any real-time game that uses a graphical UI, so I think you have a fair amount of things to learn before you can actually get where you want to go.
Things to learn include:

  • asynchronous programming
  • concurrent programming
  • blocking versus non-blocking code
  • event-based and reactive programming
  • GUI-based user interaction
  • component based architecture (delegation versus inheritance)

These are just general patterns that happen in pretty much all actual shipping software systems.
Once you actually understand and use these concepts, merging them into the Unreal Engine will be pretty easy, because the Unreal Engine APIs provide good implementations and places to hook into for all of these things.

1 Like

thx, yes im new to c++ too :stuck_out_tongue: i know java more and :stuck_out_tongue: and thx again

It would probably be a good idea to look at the templates and example projects. Guided by them, try to make a few simple games, like Minesweeper with a series of cubes that change color. Then, try to make a simple multiplayer title.

Also, with all of the macros and simplifications that Epic adds to their API, it actually feels a lot like developing in Java or C#. The engine has quite a few events (virtual functions in C++) that you can override to steal execution for a short period of time. Those are the brief windows where you do your work, returning execution back to UE4. Good examples are BeginPlay(), which is when a player is spawned into the world, Tick(float DeltaTime), which occurs once per frame for each object, and SetupInputComponent(), which is where Player Controllers bind action commands to C++ functions. You can then bind these action command strings, which you added in your SetupInputComponent() override, to actual buttons, axes, or gestures using the editor’s Input panel.

UE4 even has a framework for Remote Procedure Calls (RPCs). It allows you to call a function with some set of parameters on one machine, which causes it to execute on another machine, depending on the RPC type and where it’s called from. A “Server” RPC, when called from the client, runs on the server’s copy of that same object. A “Client” RPC, when called from the server, runs on the client’s copy of that same object. A “Multicast” RPC, when called from the server, runs on all copies of that same object, including the server and all connected clients.

So, to make a chat client, I would add a Server RPC subclass of PlayerController, and a Multicast RPC to my subclass of GameState. The Server RPC would have an FText parameter, containing the message string. Inside the method, it would validate that the message is okay (profanity filters, valid characters, etc.) and, if it is, call MyGameState->MulticastMyChatFunction() with the FText as a parameter. The body of this function would do what’s necessary to paint it on screen, log it, and/or whatever it needs to do.

So… again… check out the examples and get an idea for how UE4 does things. You will be very abstracted from the concepts of locking while(true) loops and thread sleeps… and probably with much better performance, too.

I whole-heartedly disagree with jwatte telling you to learn things first, though. One of the best ways to learn is by having a project that motivates you past your struggles. The risk of learning theory before applying it is that you’ll get bored and stop caring (especially since theory without application isn’t processed as easily by the brain – no context). As I said earlier, start with some simple projects, guided by the examples. (I was a high school science teacher for a little bit.)