hp update problem
ok so ive been trying to get this to work for 5 hours
this is the script that syncs the health, evey player has it attatched to them
and this is the thing that updates the health
what happens is: if i attack the other player with the sword, its health updates on my screen but not on their screen
if i attack myself with the sword, my hp updates on both screens, as it should. whats the problem
14 Replies
Hi @MasterOX 水. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The issue you're experiencing seems to be related to the ownership of the
RealtimeView
component. In Normcore, the ownership of a RealtimeView
determines which client has the authority to make changes to the data. When you attack another player, you're clearing the ownership of their RealtimeView
and then trying to update their health. However, since you've just cleared the ownership, no client has the authority to make changes, which is likely why the health update isn't being reflected on the other player's screen.
Instead of clearing the ownership, you should request ownership before making changes. Here's how you can modify your code:
This way, you're requesting ownership of the RealtimeView
before making changes to the health. Once you have ownership, you should be able to update the health and have it reflected on all clients.
Remember, when you're done making changes, it's a good practice to clear the ownership if you no longer need it. You can do this by calling realtimeView.ClearOwnership()
.
For more information on ownership in Normcore, you can refer to the RealtimeView reference (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeView) and the RealtimeView Programming Interface (https://normcore.io/documentation/realtime/realtimeview#programming-interface) in the documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
it didnt work...
@Normcore Assistant that gives the same result, i added some debugging logs
and ownership is being granted, but again if i attack the other player with the sword, its health updates on my screen but not on their screen. but if i attack myself with the sword, my hp updates on both screens, as it should
Let me check the knowledge base for you. One moment! :books:
----------------------
The issue you're experiencing might be due to the fact that the
RealtimeView
ownership request is not instantaneous. It's a network operation that might take some time to complete. Therefore, when you check realtimeView.isOwnedLocallySelf
immediately after realtimeView.RequestOwnership()
, it might still return false
.
To handle this, you can use the DidRequestOwnership
and DidReceiveOwnership
events provided by the RealtimeView
class. Here's how you can modify your code:
In this code, DidRequestOwnership
will be called when ownership is requested, and DidReceiveOwnership
will be called when ownership is granted. This way, you're ensuring that the health update only happens after ownership has been granted.
Remember to remove these event handlers when they're no longer needed to prevent memory leaks. You can do this by calling realtimeView.didRequestOwnership -= DidRequestOwnership;
and realtimeView.didReceiveOwnership -= DidReceiveOwnership;
.
For more information on ownership in Normcore, you can refer to the RealtimeView reference (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeView) and the RealtimeView Programming Interface (https://normcore.io/documentation/realtime/realtimeview#programming-interface) in the documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
@Normcore Assistant
'RealtimeView' does not contain a definition for 'didRequestOwnership' and no accessible extension method 'didRequestOwnership' accepting a first argument of type 'RealtimeView' could be found (are you missing a using directive or an assembly reference?)
'RealtimeView' does not contain a definition for 'didReceiveOwnership' and no accessible extension method 'didReceiveOwnership' accepting a first argument of type 'RealtimeView' could be found (are you missing a using directive or an assembly reference?)
Let me check the knowledge base for you. One moment! :books:
----------------------
I apologize for the confusion. It seems I made a mistake. The
RealtimeView
class in Normcore does not provide didRequestOwnership
and didReceiveOwnership
events.
However, the RealtimeView
class does provide an ownerIDSelfDidChange
event which is dispatched when the ownerIDSelf
value changes. You can use this event to check if the ownership has been granted to the local client. Here's how you can modify your code:
In this code, OwnerIDSelfDidChange
will be called when the ownerIDSelf
value changes. If the RealtimeView
is owned by the local client, the health update will be performed.
Remember to remove this event handler when it's no longer needed to prevent memory leaks. You can do this by calling realtimeView.ownerIDSelfDidChange -= OwnerIDSelfDidChange;
.
For more information on ownership in Normcore, you can refer to the RealtimeView reference (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeView) and the RealtimeView Programming Interface (https://normcore.io/documentation/realtime/realtimeview#programming-interface) in the documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
@Normcore Assistant still says ownership granted... but health doesnt update on both screens. i dont think ownership is the problem
Let me check the knowledge base for you. One moment! :books:
----------------------
I see, it seems like the issue might not be related to ownership. Let's try a different approach.
In Normcore, the
RealtimeComponent
class provides a RequestOwnership()
method that you can use to request ownership of the model. This might be useful in your case since you're trying to modify the health value which is part of the playerHealthSync
model.
Here's how you can modify your code:
In this code, you're requesting ownership of the playerHealthSync
model before making changes to the health. Once you have ownership, you should be able to update the health and have it reflected on all clients.
Remember, when you're done making changes, it's a good practice to clear the ownership if you no longer need it. You can do this by calling playerHealthSync.ClearOwnership()
.
For more information on ownership in Normcore, you can refer to the RealtimeModel reference (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeModel) in the documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
no, still not fixed. can anyone help me please?
ownership is being granted but it only works if i'm attacking myself
ty! looking into it now
oh. it makes sense now