当布尔条件为 True 时调用 Unity Mehtod 的问题

problem to call a unity mehtod when a bool condition is True

提问人:donbonbon 提问时间:8/6/2023 更新时间:8/6/2023 访问量:22

问:

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

EndGane 方法应调用 CheckAllPlayerHandsEmpty。但是,当我的项目中所有 playerHands 都是空的时,当我尝试调用 EndGamemethod 时出现此错误:

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

由于 if 语句的语法似乎是正确的......我被锁定在一个愿景中,什么都不会出错......正如我的游戏真人秀所显示的那样,这是错误的,错误是存在的。

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

相关代码:

卡管理器.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);
    }
}

卡控制器 .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 回调

评论

0赞 BugFinder 8/6/2023
checkallplayerhandsempty 的返回类型为 void。因此,它不会帮助您做出布尔值决定

答:

2赞 JeffRSon 8/6/2023 #1

好吧,问题是,这不能用作 if 条件,因为它不返回布尔值。CheckAllPlayerHandsEmpty

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

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;
    }

评论

0赞 donbonbon 8/8/2023
太好了,这就是魔术师。感谢您抽出宝贵时间提供帮助。