2013-09-24 9 views
5

私は皆さんのために新しい質問をしていますが、ルア内でenumartionsを実行できるかどうか疑問に思っています。これが正しい名前かどうかはわかりません。 PAWNを使った例(C言語を知っているなら意味をなさない)。Lua内の列挙型ですか?

#define MAX_SPIDERS 1000 

new spawnedSpiders; 

enum _spiderData { 
    spiderX, 
    spiderY, 
    bool:spiderDead 
} 

new SpiderData[MAX_SPIDERS][_spiderData]; 

stock SpawnSpider(x, y) 
{ 
    spawnedSpiders++; 
    new thisId = spawnedSpiders; 
    SpiderData[thisId][spiderX] = x; 
    SpiderData[thisId][spiderY] = y; 
    SpiderData[thisId][spiderDead] = false; 
    return thisId; 
} 

だから、これは私がこれまでに得たものである...しかし、私はLuaの中でこれを実行する方法がわからない、それはPAWNで次のようになります。

local spawnedSpiders = {x, y, dead} 
local spawnCount = 0 

function spider.spawn(tilex, tiley) 
    spawnCount = spawnCount + 1 
    local thisId = spawnCount 
    spawnedSpiders[thisId].x = tilex 
    spawnedSpiders[thisId].y = tiley 
    spawnedSpiders[thisId].dead = false 
    return thisId 
end 

しかし、明らかにエラーが発生します。これを行う正しい方法はわかりますか?ありがとう!

+0

。 PAWの例をLUAに変換する方法は? – Akhneyzar

答えて

4

何か?

local spawnedSpiders = {} 
local spawnCount = 0 

function spawn_spider(tilex, tiley) 
    spawnCount = spawnCount + 1 
    spawnedSpiders[spawnCount] = { 
     x = tilex, 
     y = tiley, 
     dead = false, 
    } 
    return spawnCount 
end 

EDIT:ゆうハオは私より速かった:)タイトルに与えられた問題を解決しない。この会話は、「どのようにLUAで列挙を行うには?」

+0

ああ、大丈夫です!それは私がとにかく探していたものです:3 – Bicentric

+1

'spawnCount'は'#spawnedSpiders'と置き換えることができます – hjpotter92

+0

@ hjpotter92簡素化のために、Yu Haoの答えのように。 *ロット*のスパイダーがある場合、長さ演算子 '#'が 'O(log n)'なので、やや効率が悪くなります。しかし、私がそれをしなかった本当の理由は、@ Bicentricのコードに近づきたいということです(別のコンセプトの導入を避けてください)。 – catwell

4

私がPAWNのことは知らないが、私は、これはあなたが何を意味するかだと思う:

local spawnedSpiders = {} 

function spawn(tilex, tiley) 
    local spiderData = {x = tilex, y = tiley, dead = false} 
    spawnedSpiders[#spawnedSpiders + 1] = spiderData 
    return #spawnedSpiders 
end 

はそれをテストを与える:

spawn("first", "hello") 
spawn("second", "world") 

print(spawnedSpiders[1].x, spawnedSpiders[1].y) 

出力:first helloこのような

+0

ありがとうございました!これはまさに私が探していたものです^ _ ^私はそれを感謝します、ありがとう、男! – Bicentric