当布尔条件为 True 时调用统一方法时出现问题

问题描述 投票:0回答:1

在我的 Unity 四重奏纸牌游戏中,当触发必要条件时调用 EndGame 方法时遇到问题。

EndGane 方法应该调用 CheckAllPlayerHandsEmpty。但是,当我的项目中所有玩家手都是空的时,当我尝试调用 EndGame 方法时,我会收到此错误:

Assets\Scripts\Managers\CardManager.cs(172,13): error CS0029: Cannot implicitly convert type 'void' to 'bool'

我注意到只有在 DistributeCards() 末尾引入这行代码时才会出现此问题

if (CheckAllPlayerHandsEmpty())
    {
        playerManager.GetComponent<MainGameManager>().EndGame(Players);
    }

我认为这个 if 语句是有问题的代码,因为:

  1. 如果我用 [ICODE]CheckAllPlayerHandsEmpt();[/ICODE] 替换上面的 if 语句,CheckAllPlayerHandsEmpty 会正确运行并显示正确的日志。
  2. 我的项目目前按预期运行,即所有变量都根据需要进行引用和通信。
  3. 被调用的方法不是问题,EndGame 方法会以其他方式运行,并且当调用另一个方法时,相同的错误仍然存在。

因为 if 语句的语法似乎是正确的......我被锁定在一个不会有任何错误的愿景中......正如我的游戏现实所示,这是错误的,那里有错误。

任何关于如何克服这个问题的想法都将受到高度赞赏。

相关代码:

CardManager.cs:

public class CardManager : MonoBehaviour
{
    public GameObject cardPrefab;
    public Transform deck;

    private List<Card> deckCards;
    public List<Player> Players = new List<Player>();
    public Transform undistributedCardsParent; // Reference to the parent transform for undistributed cards

    public PlayerManager playerManager; // Reference to the PlayerManager


    public bool IsCardDistributionComplete
    {
        get { return isCardDistributionComplete; }
    }

    // Method to initialize cards with loaded data to the Deck
    public void InitializeCards(List<CardData> cardDataList)
    {
        // Convert CardData to Card objects and store them in the deckCards list
        deckCards = new List<Card>();
        foreach (CardData cardData in cardDataList)
        {
            Card card = new Card();
            //other attributes
        }

        // Cards are initialized, you can proceed with further logic here
        ShuffleDeck();
        DistributeCards();
    }

    private void ShuffleDeck()
    {
        
    }

    public void DistributeCards()
    {   
        
        int cardsToDistribute = 4; 
        
        //distribution logic here

        // Instantiate undistributed cards on the deck
        for (int i = 0; i < deckCards.Count; i++)
        {
            Card card = deckCards[i];
            Vector3 positionOffset = new Vector3(i * 1.2f, i * 1.2f, -i * 1f); // Adjust the position offset for each card
        }

        isCardDistributionComplete = true;

        // After distributing cards, check if all player hands are empty and trigger game end if needed
        CheckAllPlayerHandsEmpty();
        
        //here I tried both version, with Line n.1 and without, tje error appeared in both cases
        /*j1. playerManager.GetComponent<EndGameTest>().EndingGame(Players);
        if (CheckAllPlayerHandsEmpty())
        {
            playerManager.GetComponent<EndGameTest>().EndingGame(Players);
        }*/    
    }

    // Add this method to check if all players' hands are empty
    public void CheckAllPlayerHandsEmpty()
    {
        bool allHandsEmpty = true;

        foreach (Player player in Players)
        {
            if (!player.IsHandEmpty())
            {
                allHandsEmpty = false;
                break;
            }
        }

        if (allHandsEmpty)
        {
            // All player hands are empty, trigger game end logic
            Debug.Log("All player hands are empty. Game should end.");
        }
    }

    //other methods
}

EndGameTest.cs:

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class EndGameTest : MonoBehaviour
{
    public Image endScreenBackground;
    // Start is called before the first frame update
    public void EndingGame(List<Player> players)
    {
        Debug.Log("Game has ended!");

        endScreenBackground.gameObject.SetActive(true); // Activate the background
    }

}

如果相关:

卡.cs:

using System;
using UnityEngine;

[System.Serializable]
public class Card : IComparable<Card>
{
    public string rank;
    public string cardName;
    public string hint;
    public string level;
    public Sprite illustration;
    public GameObject gameObject;

    public int CompareTo(Card other)
    {
        // Sort by rank alphabetically
        return String.Compare(rank, other.rank, StringComparison.Ordinal);
    }
}

CardController.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

public class CardController : MonoBehaviour
{
    public TMP_Text cardRankText;
    public TMP_Text cardNameText;
    public TMP_Text cardHintText;
    public TMP_Text cardLevelText;
    public Image cardIllustrationImage;

    public void SetCardData(Card card)
    {
        cardRankText.text = "Rank: " + card.rank;
        cardNameText.text = "Name: " + card.cardName;
        cardHintText.text = "Hint: " + card.hint;
        cardLevelText.text = "Level: " + card.level;
        cardIllustrationImage.sprite = card.illustration;
    }
}
c# unity-game-engine callback
1个回答
0
投票

嗯,问题是,

CheckAllPlayerHandsEmpty
不能用作if条件,因为它不返回布尔值。

而不是

public void CheckAllPlayerHandsEmpty()
,它应该是
public bool CheckAllPlayerHandsEmpty()
并在最终语句中返回 allHandsEmpty。

public bool CheckAllPlayerHandsEmpty()
    {
        bool allHandsEmpty = true;

        foreach (Player player in Players)
        {
            if (!player.IsHandEmpty())
            {
                allHandsEmpty = false;
                break;
            }
        }

        if (allHandsEmpty)
        {
            // All player hands are empty, trigger game end logic
            Debug.Log("All player hands are empty. Game should end.");
        }

        return allHandsEmpty;
    }
© www.soinside.com 2019 - 2024. All rights reserved.