vrchat-community / UdonSharp

A compiler for compiling C# to Udon assembly
https://udonsharp.docs.vrchat.com
MIT License
463 stars 50 forks source link

the value of VRCPlayerApi type become null after substitution on OnOwnershipTransferred #31

Closed arumogina closed 2 years ago

arumogina commented 2 years ago

Describe the bug in detail: When OnOwnershipTransferred(VRCPlayerAPi owner_player) is called,"owner_player" is inputed to "player" variable. then, "onwer_player.playerId" is inputed to "player_id" variable. (player & player_id variable is member variable of the class that call the OnOwnershipTransferred(). ) But, In LateUpdate() , "player" variable show null. player_id is not null.

May be my code is wrong simply.

Provide steps/code to reproduce the bug:

[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)] public class JoinToGame : UdonSharpBehaviour { [SerializeField] PlayerStatusCollision psc; [SerializeField] TextMeshProUGUI dtext;

public override void Interact(){
    dtext.text = "OnInteract()" + "\n" + dtext.text;
    if(!psc.PlayerIsLocalPlayer()) psc.SetOwnerWithInteract();        
}

}

[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)] public class PlayerStatusCollision : UdonSharpBehaviour { public int player_id;
public VRCPlayerApi player; [SerializeField] TextMeshProUGUI dtext;

public bool PlayerIsLocalPlayer(){
    if(player == null) return false;
    if(player.playerId == Networking.LocalPlayer.playerId) return true;
    return false;
}

public void SetOwnerWithInteract(){      
    if(Networking.IsOwner(Networking.LocalPlayer,gameObject)){            
        dtext.text = "SetOwnerWithInteract-owner is local player" + "\n" + dtext.text;
        SendCustomNetworkEvent(NetworkEventTarget.All,nameof(SetPlayerWithOwner));
    }else{
        dtext.text = "SetOwnerWithInteract-owner is not local player" + "\n" + dtext.text;
        Networking.SetOwner(Networking.LocalPlayer,gameObject);            
    }
}

public void SetPlayerWithOwner(){
    dtext.text = "SetOwnerWithMaster" +"\n" + dtext.text;
    player = Networking.GetOwner(gameObject);     
    player_id = player.playerId;                                                                          
}

public override void OnOwnershipTransferred(VRCPlayerApi owner_player){
    dtext.text = "OnOwnershipTransferred-owner_player.playerId" + owner_player.playerId.ToString() + "\n" + dtext.text;
    player = owner_player;        
    player_id = owner_player.playerId;
    dtext.text = "OnOwnershipTransferred-player.playerId-" + player.playerId.ToString() + "\n" + dtext.text;
}           

float dtime = 0;
void LateUpdate()    
{                
    dtime += Time.deltaTime;
    if(dtime > 5 && player == null)
    {
        dtext.text = "LateUpdate()-player is null" + "\n" + dtext.text;
        if(player_id != null){
            dtext.text = "LateUpdate()-player_id is not null" + "\n" + dtext.text;
            player = VRCPlayerApi.GetPlayerById(player_id);
        }else{
            dtext.text = "LateUpdate()-player_id is null" + "\n" + dtext.text;
        }
    }
    if(Utilities.IsValid(player)){        
        if(dtime > 5) dtext.text = "LateUpdate()-IF-"+ player.playerId.ToString() + "\n" + dtext.text;
        Vector3 pos = player.GetBonePosition(HumanBodyBones.Spine);             
        gameObject.transform.position = pos;
        gameObject.transform.rotation = player.GetBoneRotation(HumanBodyBones.Hips);
    }else{
        if(dtime > 5) dtext.text = "LateUpdate()-else-"+ "\n" + dtext.text;
    }

    if(dtime > 5) dtime = 0;

}

}

Expected behavior: What was the excepted result?

Additional Information: Provide any additional information here.

MerlinVR commented 2 years ago

Known Udon bug, change the name of the player field to something that's not player for now. https://vrchat.canny.io/vrchat-udon-closed-alpha-bugs/p/runevent-will-clear-incorrect-event-parameter-names