【问题标题】:Execute methods following the order of a list in C#在 C# 中按照列表的顺序执行方法
【发布时间】:2018-07-20 13:52:15
【问题描述】:

我有一个统一项目,想使用预定义的列表一个接一个地调用方法。

在这个测试场景中,我想执行 startTrialWithFixedValue1() 两次, startTrialWithFixedValue2() 两次, startTrialWithRandomValue() 最后一次。每次用户进行输入时,我都想执行列表中的下一个方法。我的代码有两个主要问题:

(1) 每当我使用带有 return 的 if 语句时,变量 countTrial 变得无法访问。调试日志总是显示 0:1 并且一直执行第一个方法。

(2)如果我排除了return语句,所有的方法都会同时启动。

[编辑] 指定我的问题:如何按照给定列表的顺序执行方法。

脚本 A:

public int countTrial;
public int maxTrials = 16;
public Spawner spawner;

List<int> trialMarkers = new List<int>() {1, 1, 2, 2, 3};

public void chooseNextTrial()
{
    for (int countTrial = 0; countTrial < maxTrials; countTrial++)
    {
        Debug.Log(countTrial + ": " + trialMarkers[countTrial]);

        if (trialMarkers[countTrial] == 1)
        {
            spawner.startTrialWithFixedValue1();
            return;
        }
        if (trialMarkers[countTrial] == 2)
        {
            spawner.startTrialWithFixedValue2();
            return;
        }
        else
        {
            spawner.startTrialWithRandomValue();
            return;
        }
    }

脚本 B:

public void handleUserInput()
{
        if (Input.GetButtonDown("Fire3"))
        {
            deleteAllChildren();
            chooseTrial.chooseNextTrial();
        }
    }
}

希望你们能帮助我!

[EDIT2] 正如程序员所建议的那样:删除该函数中的 return 语句,然后再次运行它。复制并粘贴 Debug.Log(countTrial + ": " + trialMarkers[countTrial]); 的结果结果总是:

5:3
5:3
5:3
...

【问题讨论】:

  • 标题具有误导性,列表中没有存储任何方法。我也说不出你的问题是什么
  • 请仔细描述您的根本问题。不要重复这个问题。
  • 如前所述,您不是在调用列表中的方法,需要一些时间来了解您的 for 循环实际在做什么,以及您的代码实际在做什么。你在问题中解释它的方式似乎你都不明白。
  • 感谢大家的反馈,我编辑了第一篇文章以说明我的问题。
  • @Programmer,正确。它肯定会起作用,只是可能会引起程序员的混乱,特别是如果他(她)在另一种方法中使用该变量。很抱歉造成混乱!

标签: c# list unity3d


【解决方案1】:

您需要删除 For 循环并更手动地处理 Method 的执行

public int countTrial = 0;
public int maxTrials = 16;
public Spawner spawner;

List<int> trialMarkers = new List<int>() {1, 1, 2, 2, 3};

    public void chooseNextTrial()
    {

            Debug.Log(countTrial + ": " + trialMarkers[countTrial]);
            if (trialMarkers[countTrial] == 1)
            {
                spawner.startTrialWithFixedValue1();
            }
            if (trialMarkers[countTrial] == 2)
            {
                spawner.startTrialWithFixedValue2();
            }
            else
            {
                spawner.startTrialWithRandomValue();
            }
      }

然后在处理用户输入的第二个脚本(脚本 B)中

public void handleUserInput()
{
        if (Input.GetButtonDown("Fire3"))
        {
            deleteAllChildren();
            chooseTrial.chooseNextTrial();
            chooseTrial.countTrial++;
        }
}

结束行:您不应该使用 Fop 循环,因为您希望序列从玩家输入动作开始并像这样处理它。还添加这样的方法:

public void NextCounter()
{
     if(countTrial<trialMarkers.Count)
         countTrial++

}

在您的脚本 A 中,然后在脚本 B 中这样调用它,以避免您的计数器超出列表计数。

chooseTrial.NextCounter();

在 SCript B 中使用 countTrial++

【讨论】:

    【解决方案2】:

    当您在方法中返回时,您将跳出循环。

     for (int countTrial = 0; countTrial < (maxTrials <= trialMarkers.Count ? maxTrails : trialMarkers.Count) ; countTrial++)
     {
         Debug.Log(countTrial + ": " + trialMarkers[countTrial]);
    
         if (trialMarkers[countTrial] == 1)
         {
             spawner.startTrialWithFixedValue1();
         }
         if (trialMarkers[countTrial] == 2)
         {
             spawner.startTrialWithFixedValue2();
         }
         else
         {
             spawner.startTrialWithRandomValue();
         }
     }
    

    将按照您要求的方式执行。另请注意,我更改了您的迭代器,以便您不会在循环中收到对象引用错误。

    【讨论】:

    • 在问题中,OP 已经说过他从代码中删除了return....
    【解决方案3】:

    如何做一些类似于你在标题中描述的事情(这给人的印象是你有一个“功能列表”)。这个例子很容易扩展:

     private Action[] _functions;
    
     public void MainEntryPoint()
     {
         _functions = new Action[] { StartTrialWithFixedValue1, StartTrialWithFixedValue2, StartTrialWithRandomValue };
         List<int> trialMarkers = new List<int>() { 1, 1, 2, 2, 3 };
         DoThings(trialMarkers);
     }
    
     public void DoThings(IEnumerable<int> indexesOfFuctions)
     {
         foreach (var index in indexesOfFuctions)
         {
             _functions[index-1]();
         }
     }
    
     private void StartTrialWithFixedValue1()
     {
         Trace.WriteLine("StartTrialWithFixedValue1");
     }
    
     private void StartTrialWithFixedValue2()
     {
         Trace.WriteLine("StartTrialWithFixedValue2");
     }
    
     private void StartTrialWithRandomValue()
     {
         Trace.WriteLine("StartTrialWithRandomValue");
     }
    

    我创建了一个“函数数组”(实际上是函数的委托),然后我使用您的指令列表来执行它们。

    【讨论】:

      猜你喜欢
      • 2017-10-04
      • 2021-03-28
      • 1970-01-01
      • 2019-07-16
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多