Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate this PAWN example to LUA?

Tags:

lua

lua-table

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!

like image 876
Bicentric Avatar asked Sep 24 '13 11:09

Bicentric


2 Answers

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 :)

like image 154
catwell Avatar answered Nov 16 '22 04:11

catwell


I don't know about PAWN, but I think this is what you mean:

local spawnedSpiders = {}

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

Give it a test:

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

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

Output: first hello

like image 29
Yu Hao Avatar answered Nov 16 '22 03:11

Yu Hao