Search code examples
lualua-table

How to translate this PAWN example to LUA?


I have a new question for you all.

I am wondering if you're able to do enumerations within Lua (I am not sure if this is the correct name for it).

The best way I can explain this is if I show you an example using PAWN (if you know a C type language it will make sense).

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

So that's what it would look like in PAWN, however I don't know how to do this in Lua... This is what I got so far.

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

But obviously it gives an error, do any of you know the proper way of doing this? Thanks!


Solution

  • Something like this?

    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: Yu Hao was faster than me :)