x

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"

Somthing thats only on the server?

If I want to have a timer that is only run on the server. Is the correct way of doing it to, create a timer in the GameMode class and give the value updated / replicated in GameState? So Players and Server always have the same value?

Or is it a other way of defining something that's only existent server or client side?

Product Version: Not Selected
Tags:
more ▼

asked May 03 '14 at 03:53 PM in C++ Programming

avatar image

WCode
498 43 44 69

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

You can simply start the timer on the server using a server function to start the process!

Since the server function is the one calling the timer, it will only ever run on the server.

To avoid telling the timer to start multiple times, with included internet lag making the results go off a bit, (calling the timer multiple times will restart it), make sure to only call the server function while on the Server (HasAuthority())

See below!

~~~

.h

 UFUNCTION()
 void TheActualTimerFunction();
 
 UFUNCTION(Reliable, Server, WithValidation)
 void SERVER_StartTimerFunction();

.CPP

 void AVictoryGamePlayerController::SERVER_StartTimerFunction_Validate()
 {
   return true;
 }
 void AVictoryGamePlayerController::SERVER_StartTimerFunction_Implementation()
 {
    //start the timer
    GetWorldTimerManager().SetTimer(this, 
         &AVictoryGamePlayerController::TheActualTimerFunction, 
             0.03, true); //looping
 }


 //somewheres in your code
     if(HasAuthority())
     {
       SERVER_StartTimerFunction();
     }

Even Simpler

The above is if you are over-precautious about making sure only the server ever runs the function

if you have confidence you will always check HasAuthority()

all you need is this:

 //somewheres in your code
         if(HasAuthority())
         {
            //start the timer
        GetWorldTimerManager().SetTimer(this, 
             &AVictoryGamePlayerController::TheActualTimerFunction, 
                 0.03, true); //looping
         }


The above timer call will only run on the server

more ▼

answered May 03 '14 at 04:04 PM

avatar image

Rama
10.6k 445 343 1083

avatar image WCode May 03 '14 at 06:54 PM

Thank you for that great explination.

(comments are locked)
10|2000 characters needed characters left
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