简体   繁体   中英

Lua objects not just tables?

I am very new to Lua and am trying to write a short script using the Aseprite API .
The API uses the Image object to represent images.

My understanding of OOP in Lua is that everything is simply a table. There are no classes or objects, just functions that return tables.

I thought, then that I would be able to view all the attributes of an Image object just iterating through it using pairs(table) .

-- `image` is and Image object
for attribute, value in pairs(image) do
   app.alert(k..": "..v) -- creates an alert window in the Aseprite app
end

I would expect this to show me each attribute and method name available for use. Instead, I just get an error:

lua:2: bad argument #1 to 'for iterator' (table expected, got ImageObj)

How is it possible that this is not just a table? What is this? How might I print all attributes and method names available to me in this 'object'?

From the Lua Reference Manual, like on page 1-2

There are eight basic types in Lua: nil, boolean, number, string, function, userdata , thread, and table.

The type userdata is provided to allow arbitrary C data to be stored in Lua variables. A userdata value represents a block of raw memory. There are two kinds of userdata: full userdata, which is an object with a block of memory managed by Lua, and light userdata, which is simply a C pointer value. Userdata has no predefined operations in Lua, except assignment and identity test. By using metatables, the programmer can define operations for full userdata values (see §2.4). Userdata values cannot be created or modified in Lua, only through the C API. This guarantees the integrity of data owned by the host program and C libraries.

So no, there is not just tables.

How might I print all attributes and method names available to me in this 'object'?

image is a ImageObj , but Image should be a table that provides functions that you can use on ImageObjs.

Try for k,v in pairs(Image) do print(k,v) end

Or simply refer to the API documentation.

https://github.com/aseprite/api/blob/master/api/image.md#image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM