C++ RPC not being called via BluePrint

Hi guys,

I just recently switched to C++ and for some reason my RPC is not being called, mind you aside from the template controls there is nothing else implemented as of yet (no input, etc)

I used the blue print right click event under character blueprint to set casting my Fireball which is of type ASpellSystem, here is a few screenies:

Thank you in advance,

EDIT: I just set it as server reliable, I’m new to this so didnt think I needed multicast, and if I do, how do I go about setting it up?

So this RPC Cast function is a wrapper that determines which “private” function to cast, for example in our scenario it is an instant fireball. But before it casts that spell it is supposed to print that debug line and it is not. The code compiles and the RPC it self does not give me any errors. But the following line is not even printing:

  GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, TEXT("Casting Spell under server"));

Here is my spawn function that gets called, but like I said I need to see that the RPC is actually being called before I even start debugging the spawnactor.

Thank you,

What behavior exactly are you expecting? What’s your scenario? Are “cast” functions multicast (if you want other clients to see the behavior)? Does ServerCastSpell actually get called or is the ROLE not correct? Can you just describe exactly what you’re intending and what is and isn’t working?

So “Casting our fireball”, the BP print string message, is firing though? Have you tried setting a breakpoint in the function on the C++ side?

Also, since I’m going to bed and won’t be able to help you further until tomorrow:
There are a lot of things wrong with your code when it comes to networking your game. UnrealEngine takes a Server Authoritative approach to networking. That means that the server has to determine what happens, and then tells the clients. So say you want to have a player cast a spell. They press Right Mouse, then that calls a Server RPC (a reliable one) requesting that the server will cast their spell. The Server then needs to determine if that character can cast a spell, and if so, it needs to let everyone know, including the owning client, the player is casting a spell. This is what the multicast would be for. Client calls ServerCastSpell, Server calls MulticastCastSpell, everyone sees cast spell.

This is a huge oversimplification though. MulticastCastSpell may just fire off some effects on every client, and the Server may spawn a fireball Actor that is replicated (should probably use the ProjectileMovementComponent component).

Sidebar: “Reliable” vs “Unreliable” basically means: does this absolutely have to make it to the server? Most of the time for gameplay, you want Reliable, but Unreliable will work 99% of the time anyway, so that wasn’t/isn’t your issue.

Anyway, your next problem is how you cast your spell. You have an actor called SpellSystem that is individually responsible for the casting of spells, and that actor apparently has to find out who is casting the spell and then cast it. GetPlayerCharacter will not work here. I’m assuming you copied this logic from a singleplayer demo. Basically, the server will use GetPlayerCharacter and always request the 0th character in the game. So basically, if your code was working, every time ANYONE casts a spell, the 0th character would cast the spell.

Furthermore, right now CastSpell both fires off the RPC and then does the logic. This would mean that the client would cast their spell but then also tell the server to cast it. This is okay, but this doesn’t really conform to the logic I described for Server Authoritative. Basically, the client should only call the RPC, and then the server should handle all the logic. If you want more immediate feedback for the client who is firing the spell, that’s something for later in the project.

Finally, I can’t tell why your code isn’t firing immediately, but make sure you build the DebugGame Editor before you use breakpoints. That way you can get a clearer picture of what’s happening.

Notes:

Only the Server can spawn actors that everyone will see, and those actors must be replicated.

You can do a lot of this in blueprint initially. It helps with understanding how the networking should happen, imo. If you are experienced with C++, then continue on this path. Otherwise, I would watch the Networking with Blueprints tutorial unreal engine has, and prototype this that way first.

Thank you for this reply, what do you recommend I use instead of GetPlayerCharacter? because when i was doing this in blueprint it was a concern when using it in multiplayer. I will do some more testing in the morning but I changed my function to a regular non rpc method and it still does not call.

And yes the printstring after castspell executes.

Thank you,

EDIT: Could it have to do with not calling the correct player?

I had to call it from within a player character. I was attempting to debug it by creating a fireball object in blueprint and having it call it self. Instead I had to add it to a TArray from my MyPlayerCharacter and then call the CastSpell method from that index.

The GC is deleting that fireball from within the array, but the method finally gets called, so marking this resolved.