簡體   English   中英

索引超出范圍。 必須為非負數並且小於集合的大小。 C#

[英]Index was out of range. Must be non-negative and less than the size of the collection. C#

我試圖制作一個腳本來自動,動態地調整推進器,以保持車輛的速度。

萬一你想知道它的游戲^^

無論如何,它總是讓我知道索引超出范圍錯誤。

這是我的代碼,我添加了注釋以使其更清晰

List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();

string ThrusterControl = "Thrusters.Forward";    //the name of my thrusters.
string CruiseControl = "AI.Computer";            //The name of the block running this code.
double setSpeed=0.250;                           //my desired speed (units/tick).  
double maxSpeed=3.000;                           //This script disengages thruster overrides while im are above this speed.
double checkSpeed=0.75;                          //Determines the acceptable speed range.  
double decelerationRate=0.9;                     //Thrust override reduction factor
float speedBoost=500;                            //Gives a boost when im not moving
float minThrust=125;                             //Minimum legal value of thrust override.
double x0,dx; 
double y0,dy; 
double z0,dz; 
double speed; 
float newThrust;

void Main(){

//Determine ship speed. 

GridTerminalSystem.SearchBlocksOfName(CruiseControl,blocks);
double x = Math.Round(blocks[0].GetPosition().GetDim(0),3);
double y = Math.Round(blocks[0].GetPosition().GetDim(1),3);
double z = Math.Round(blocks[0].GetPosition().GetDim(2),3);
dx=x-x0;dy=y-y0;dz=z-z0;x0=x;y0=y;z0=z;
    speed=Math.Round(Math.Sqrt(dx*dx+dy*dy+dz*dz),5);
        blocks[0].SetCustomName(CruiseControl+":"+"\n"+"speed (units/tick) "+speed );

//Increase thrust override if im going too slow.

if(speed < setSpeed * checkSpeed){
GridTerminalSystem.SearchBlocksOfName(ThrusterControl, blocks);
    for(int i=0; i < blocks.Count;){
        blocks[0].GetActionWithName("IncreaseOverride").Apply(blocks[0]);
        i++;}

//Give an extra thrust boost if you're im not moving at all.

if(speed < setSpeed * 0.05){
    newThrust = (float)Math.Round(blocks[0].GetValueFloat("Override") + speedBoost,4);
    for(int i=0; i < blocks.Count;){
        blocks[i].SetValueFloat("Override", newThrust);
        i++;}
    }
}

//Slowly reduces thrust override if im going too fast.

if(speed > setSpeed / checkSpeed){
GridTerminalSystem.SearchBlocksOfName(ThrusterControl, blocks);
newThrust = (float)Math.Round(blocks[0].GetValueFloat("Override") * decelerationRate, 4);
if( newThrust > minThrust){    //Prevents this script from disabling the thruster override.
    for(int i=0; i < blocks.Count;){
        blocks[i].SetValueFloat("Override", newThrust); 
        i++;}
    }

//Reset thruster overrides if moving too fast. Allows inertial dampers to regain control of vehicle.

if(speed > maxSpeed){
    for(int i=0; i < blocks.Count;){
        blocks[i].SetValueFloat("Override", 0);
        i++;}
    }
}

}

有人可以更正我的代碼嗎?

與其讓別人糾正您的代碼,不如確切地了解正在發生的事情,這會很好嗎?

此異常很容易調試。 Visual Studio將在發生的地方停止執行,並突出顯示確切的行。 當該行突出顯示時,您可以將鼠標懸停在變量上,並查看其值以:

  • 檢查集合及其中的項目數
  • 檢查包含導致問題的索引的變量

調試器

然后,index變量將小於0或大於或等於集合中的項目數。

有了這些信息,您也許可以自己解決問題,但是如果沒有,您將可以對SO提出完全不同的問題。

編輯:

如果無法正確調試,則可以隨時進行范圍檢查,即:

public void MyFunction()
{
    for (int i = ...)
    {
        if (i < 0 || i >= blocks.Count)
        {
            System.IO.File.AppendAllText(@"error.log", "Error in MyFunction(), i = " + i + ", blocks.Count = " + blocks.Count);
        }

        blocks[i].SetValue(...);
    }
}

這需要花費更多時間,但至少可以使您了解正在發生的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM