NPC 問題: NPC攻擊NPC偵測FUNCTION的問題

Home Home
引用 | 編輯 op47
2012-08-13 18:09
樓主
推文 x0
以下是我NPC 偵測 NPC 的 FUNCTION, 但一執行, 伺服器就當掉了:
複製程式
public FindClosesEnemy(entid)
{
    new Float:Dist
    new Float:maxdistance=5000.0
    new indexid=0
    new i;
    while ((is_valid_ent(i = find_ent_by_class (i, CLASSNAME))) || (is_valid_ent(i = find_ent_by_class (i, "player"))))
    //for(new i=1;i <= get_maxplayers();i++)
    {
     new Ptdclassname[32]
  pev(i, pev_classname, Ptdclassname, charsmax(Ptdclassname))
  if (equal(Ptdclassname, CLASSNAME) && i != entid)
  {
   if (is_valid_ent(i) && pev(i,pev_health) > 0.0)
   {
    Dist = entity_range(entid, i)
             if(Dist <= maxdistance)
             {
                 maxdistance=Dist
                 indexid=i
                }
            }
        }
        else if (equal(Ptdclassname, "player"))
        {
         if(is_user_alive(i) && is_valid_ent(i) && cs_get_user_team(i) == CS_TEAM_CT && is_user_connected(i))
         {
             Dist = entity_range(entid, i)
             if(Dist <= maxdistance)
             {
                 maxdistance=Dist
                 indexid=i
             }
         }
           
     }
    }
    return indexid
}

請問一下, CODE 在那裏出了問題?

獻花 x0
引用 | 編輯 tw2twtw
2012-08-13 20:11
1樓
  
你的while 沒有設定如何停止,要是條件式一直都符合...
就變成無限執行了,然後當掉~

獻花 x0
引用 | 編輯 op47
2012-08-13 20:22
2樓
  
那麼我應在那裏加一個 break ? 表情

獻花 x0
引用 | 編輯 HsK
2012-08-14 01:25
3樓
  
下面是引用 tw2twtw 於 2012-08-13 20:11 發表的 : 到引言文
你的while 沒有設定如何停止,要是條件式一直都符合...
就變成無限執行了,然後當掉~

只說 while 應該沒問題
(is_valid_ent(i = find_ent_by_class (i, CLASSNAME)))

找不到 classname entity,
find_ent_by_class 會 -1/0

is_valid_ent 便可 block while

獻花 x0
引用 | 編輯 op47
2012-08-14 08:44
4樓
  
WHILE 沒有出問題, 那麼是那裏出了問題 表情

獻花 x0