So im making a shooter where there are two teams and those teams are red and blue.
i made an integer variable called teams and the value 1 = red and 2 = blue and 0 = no team.
However when i go to print string in the event tick it seems that it changes the variable but still has a default value so changes between them rapidly. i need to be able to make it so you can’t shoot team mates so (ive done this by checking if the other actor has the same value in the variable as the shooter)
Just wondering how i can fix this ive tried looking around and searching google but can’t seem to find out why. if anyone might know why that would help a lot.
Hey there @Meks1738! Welcome to the community!
So off the top of my head I was thinking your may require a Rep-Notify and make sure the variables are being set across the board. That said I’d definitely have to see the BPs and ask a couple more questions before I diagnose that for certain.
Is it swapping rapidly back and forth on both client and server in sync or out of sync?
Thanks for the response! im not too sure what rep-notify is i have seen it around but ive got a couple images and a video to show whats going on. (you can see the material change from 0 which is the normal to 1 and 2 red and blue)
Alright, I think it’s a combination of 2 factors here. Multicasting something that could be handled via RepNotify if the variable is meant to be monitored constantly and if other players can join late etc. or a Server only call if it’s going to change just the once/or only when you call it. Also doing the logic for the team color changing in event tick with a delay. There’s a ton to go over with this so I’ll explain what’s going on in this original post, and then I’ll have to make a longer winded post to give you some options to fix it.
Issue 1: Event Tick
So this is more best practices than anything but it’s part of your problem as well. Avoid putting anything on event tick that doesn’t need to change every single tick. Their teams shouldn’t be changing that often, the fastest game I can think of for team changing would be like tag or hide and seek, but even then an event causes it to occur, so it’s usually best to have the logic to trigger it once.
This leads into the question: “Wait then what if a player joins late? How do they know who’s on who’s team?”, well I’m glad you’ve asked, other me. The wonderful world of RepNotify is here to save the day! So your variable likely is set to replicate if you access it from elsewhere right? There’s another option there. There’s RepNotify, the shortest way to explain it is if you need a variable that has to be permanently and constantly updated on the server and all clients with relevancy, then you want a RepNotify. I’ll leave some resources, but I’ll be able to explain it’s use case a bit better when I make it in BP and show it to you then.
One of the best videos to explain the difference would be this video by our community member Bry.
It’s part of a 4 part series so I still recommend them all but you seem to have the basics of replication down so this may just be a refresher.
Issue 2: Delay Node
The Delay node in your event tick does something a little whacky, and it’s because delay is independent of everything else. Just imagine the game still continuing in the meantime of the 0.2sec delay, which for some use cases is totally fine and safe to do. For event tick however this means that every tick is firing your graphs after every tick, but they aren’t synced. So if you change teams to red, while blue but anything changed in that 0.2 seconds, you may have your graph work the wrong data after the delay. We won’t need the delay once we rewrite this.
Player controller is not replicated, try not to use this when you can avoid it in multiplayer situations. This will save hours of your life in the future.
There’s a couple more issues, but the core of it is what I mentioned before, and most of these issues won’t be a problem once this is reworked. Stay tuned in the next couple hours, I’ll write up a little thing for you to try out that might get you sorted!
This is a simplified version of how you would handle this. The Rep-Notify here basically makes sure all players get the team variable automatically, then are told to update the colors whenever there is a team change, you can then call this whenever a team change occurs. This decouples your color setting logic from your team logic (mostly) and allows you to only ever focus on messing with the teams.
That said, this is a simple, extendable way to pull that off, but be warned, this is not quite best practices. We still don’t check to make sure it’s valid, we don’t make sure teams can be changed, we have no change team redundancy checking, and I didn’t make the widgets. I tried to keep it as close to your setup as possible so it wouldn’t be hard for you to integrate it. Let me know if you have any questions!
Thanks man! this was real informative. and yea i could probably do with a bit of a brush up on replication anyway its still a bit weird at times. and i didn’t realise the delay node with the event tick worked like that. thanks for your help again!
No problem! I also think your implementation of Get Player Controller is not bad here since it mostly deals with UI and that’s all client side anyway, but just be careful as it’s a slippery slope from there. If you’d like a little cheat-sheet that I keep on hand for replication stuff, here’s a PDF that’s a bit dated but still insanely useful from Cedric Neukirchen.
Disclaimer: Epic Games is not liable for anything you access on any other domain. Epic Games is not affiliated with any external sites linked below. Sites can be changed on a whim, and this link may not be the same forever. That said, Cedric is awesome and I use this PDF once a week.