Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to spawn actors in multiplayer

I'm working on a multiplayer game and it involves spawning a certain Pickup class that the player can then consume when he/she overlaps it.

I tried spawning these Pickups in my GameMode class, GameState class, but they just won't spawn... How do i manage the spawning and destruction of these Pickups?

Product Version: UE 4.12
more ▼

asked Aug 15 '16 at 12:42 PM in C++ Programming

avatar image

897 12 14 25

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

You will want to spawn actors on the server. Which class spawns them is mostly irrelevant, though the "reason" for these spawns can determine where they should be implemented. Without context on what kind of pickups are spawning, how often, and where they spawn I am limited in the help I can provide, but I will be happy to give you a generic example.

Assume you want to spawn a pickup every 30 seconds. You can (and should) do this in GameMode since it is a global event that changes depending on what type of game is being played, and the spawn event isn't attached to any trigger or stimuli outside of time. Assuming you have a custom GameMode class you can do the following:

 // MyGameMode.h
 #pragma once
 #include MyGameMode.generated.h

 class AMyGameMode : public AGameMode


    void BeginPlay() override;
    void SpawnPickup();
    FTimerHandle TimerHandle_SpawnPickup;

And your cpp file...

 // MyGameMode.cpp
 #include "MyProject.h"
 #include "MyGameMode.h"
 #include "MyPickupClass.h"

 void AMyGameMode::BeginPlay()
    GetWorld()->GetTimerManager().SetTimer(TimerHandle_SpawnPickup, this, &AMyGameMode::SpawnPickup, 30.f /* You should scale this with the global time dilation */, true, 45.f /* Spawn the first pickup after 45 seconds, also scale with global time dilation */);

 void AMyGameMode::SpawnPickup()
     AMyPickupClass = SpawnActor<AMyPickupClass>(AMyPickupClass::StaticClass(), SomeLocation, SomeRotation);

Now, it is important that you set your actor to replicate. You can view a nice overview of replication here.

If you want some client event to trigger a spawn you would need to use UFUNCTION(Server, Reliable, WithValidation) and do the same SpawnPickup call wherever else.

This will cause your pickup to be spawned on the server but replicate to clients. This way when a client walks over a pickup it can do what it needs to due to the fact that both the player character and pickup live on the server (meaning you don't need to worry about client->server pickup messages and interactions).

If you have a more specific question I would be happy to answer.

more ▼

answered Aug 15 '16 at 06:10 PM

avatar image

1.2k 31 8 40

avatar image goncasmage Aug 18 '16 at 11:12 AM

Thanks for this it was really helpful ;) issue solved!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question