#Ammo Bug

40 messages · Page 1 of 1 (latest)

agile steeple
#

Hello everyone, have you encountered such a problem (I asked it before, but it still hasn't been solved):

To sum it up in one sentence: Ammo and MagCapacity mess up when players swap or pick up each other's weapons.(For example by killing a player to get his weapon)

A little more detail: the number of bullets and the upper limit of bullets (capacity) of the weapon will decrease, as if a certain process will be executed twice, which is very strange.

I implore those who are interested to test your own game to see if there are any problems similar to mine. If your game's reload system works correctly, please let me know, thank you!🫡

keen glade
#

I'll say my ammo setup seems to work just fine, and as far as I know I did everything per the lesson. Sorry yours isn't working.

agile steeple
agile steeple
#

My problem is very similar to what he described. I don't know if everyone has a solution. His description is so simple that I can't understand it.

lean finch
#

@agile steeple are u using a separate input for switching weapons?

#

Or is it same as equip key?

agile steeple
lean finch
#

oh u are not on the weapon swap lecture.
is it happening on both server and client?

agile steeple
lean finch
#

oh so its not being replicated

#

can u compare ur code with github code

#

specifically the elim function

#

the multicast elim

#

weapon's OnRep_State Change function

#

and weapon's dropped function

agile steeple
# lean finch and weapon's dropped function

Yes, but it seems like I can't get help from the teacher's code, although some other students say their bullet system works fine (I don't know if they tested this situation)

lean finch
#

bullet system should work fine, have u compared the code line by line against code on github for the lecture?

#

I think there is a faulty if check somewhere with reverse condition

agile steeple
lean finch
#

alright good luck

agile steeple
lean finch
#

If u are going to develop games, u should expect alot of bugs Xd

sharp elbow
#

请问这个bug你有解决了吗,我今天也遇到了🤣

#

@agile steeple

agile steeple
# sharp elbow <@986123770915881020>

还没有🥲 我先去做其他功能了,我目前只是让掉落或者捡起的函数里让子弹值变满,这样无论谁捡起一把枪都是满弹夹的。。。不过有可能修复这个问题的方法就在掉落和捡起里,可能需要多一次更新?我打算后面再来看看这个问题,这个问题确实是阴魂不散,但是仍然有很多人表示他们并没有这个问题。

sharp elbow
#

我遇到的问题更复杂,捡起枪之后子弹可能变成负数然后又马上回复,大部分是变成-1,有一次还变成了-90

#

我是完全按照老师的教程做的,目前还没加新功能,理论上大家都应该有这个bug才对

sharp elbow
#

🤣🥲

pallid zephyr
# agile steeple I found many discussions under the lecture, only his description is similar to m...

Have you been able to find a fix for this? I have the same problem with all weapons. In one of my cases, when a client picks up a shotgun, the ammo value is set to 0 and it will not auto reload. But in the server, I can see the client plays the reloading montage and after some time the AmmoHUD updates correctly but when firing the weapon once, the ammo goes down to -1. Don't even know anymore what is the problem even though I have clamped the values in SpendRound between 0 and MaxCapacity.

calm charm
#

Okay, so I looked into this. I answered it on the Udemy item but I'll post the solution here.

There is actually a bug! Sequence should only be incremented for the client. But since SpendRound is called on both client and server, it increments when the server fires the weapon! So we must also check for local control when not the server. Here's the solution:

void AWeapon::SpendRound()
{
    Ammo = FMath::Clamp(Ammo - 1, 0, MagCapacity);
    SetHUDAmmo();
    if (HasAuthority())
    {
        ClientUpdateAmmo(Ammo);
    }
    else if (BlasterOwnerCharacter && BlasterOwnerCharacter->IsLocallyControlled())
    {
        ++Sequence;
    }
}

void AWeapon::ClientUpdateAmmo_Implementation(int32 ServerAmmo)
{
    if (HasAuthority()) return;

    Ammo = ServerAmmo;
    --Sequence;
    Ammo -= Sequence;
    SetHUDAmmo();
}
agile steeple
# calm charm Okay, so I looked into this. I answered it on the Udemy item but I'll post the s...

Hello teacher! thank you for your reply.
In fact, the first discussion below this lecture has already given the same answer as you, but interestingly😂 🥲 , this seems to only solve half of the problem (that is, double the ammo deduction), other than that, I And many students have the other half of the problem:
Just pick up someone else's gun (it has emptied the magazine at least once, note that it is someone else's), the HUD will be confused, and you can't even reload and fire (usually it stays at 0 and the R key does not work). We would appreciate it if you could take the time to help take a look at this issue!🫡
I did some testing and found that this seems to be related to "OnEquipped()" and "OnDropped()", but I haven't thought of a solution yet.

deep zealot
deep zealot
#

I have a solution.
Let me talk about the reason first: When the character controlled by the client or server fires, all machines with this character will fire, and the SpendRound() function will be executed, and then the weapon bullets of all machines with this character will be fired. Reduced, so it is normal for you to pick it up directly if you don't reload at this time. Here comes the key point, when you reload the bullet, all machines will play the Reload montage, and then execute the FinishReloading() function because of the animation notification, but the UpdateAmmoValues(); function can only be run on the server, resulting in EquippedWeapon-> AddAmmo(ReloadAmount); can only be run on the server, and then use RPC to synchronize Ammo on other clients. But the ClientAddAmmo(AmmoToAdd) function called in the AddAmmo function is a Client-RPC, and only the client with the Actor ownership will run when it is called on the server, so when a non-locally controlled character in a client reloads bullets, it is Will not update the ammo on his weapon, so a simple ClientAddAmmo(AmmoToAdd) change to Multicast-RPC will do the trick.

#

我有解决办法了。
我先说下原因:当客户端或者服务端上的控制的角色开火的时候,所有有这角色的机器上都会开火,并且执行SpendRound()函数,然后有这个角色上的所有机器的武器子弹都会减少,所以这时候你如果不换弹的话直接捡起来是正常的。关键点来了,当你重新加载子弹,所有机器都播放Reload的蒙太奇,然后都因为动画通知执行了FinishReloading()函数,但是UpdateAmmoValues();函数只能在服务器上运行,导致其中的EquippedWeapon->AddAmmo(ReloadAmount);也只能在服务器上运行,然后通过RPC来同步其他客户端上的Ammo。但是AddAmmo函数里调用的ClientAddAmmo(AmmoToAdd)函数是Client-RPC,在服务端上调用只有Actor所有权的客户端才会运行,所以当一个客户端里的一个非本地控制的角色重新加载子弹的时候是不会更新子弹在他的武器上,所以只要简单的ClientAddAmmo(AmmoToAdd)更改为Multicast-RPC就可以了。

#

UFUNCTION(Client,Reliable)
void ClientAddAmmo(int32 AmmoToAdd);
Change:
UFUNCTION(NetMulticast,Reliable)
void MulticastAddAmmo(int32 AmmoToAdd);

agile steeple
#

Hello everyone, After some tests, I think this question can be settled (not 100% guaranteed haha).

If you have a bug related to bullets after picking up a weapon, please check this thread, I think this bug is divided into two parts:

Ⅰ. Spend Round
Ⅱ.ClientAddAmmo

For Ⅰ:
#1070137024876650496 message
For Ⅱ:
#1070137024876650496 message

And it seems that some people have a BUG when switching weapons. Since my own game does not use secondary weapons, I don't know the specific situation. Meags seems to discuss this issue:
#🔫|multiplayer-shooter message
https://discord.com/channels/807733033192390676/1073639538292961390
But for the problem that the bullet is 0 after picking up someone else's gun and the reload is invalid, I didn't take Meags' method, and I can solve it by using the above Ⅱ👆 .

Thanks to the teacher and all the students who helped!🫡

pallid zephyr