I recently tried to implement an enum structure in Lua, which turned out to be very useful. So I thought I might share it here :)
Find the latest version here (it's part of a Pico-8 library) https://github.com/sulai/Lib-Pico8/blob/master/lang.lua
Then enum(...) function generates an enum structure from a list of names.
The generated structure is useful when it comes to readability and object orientation.
As a use case for Pico-8, you can comfortably generate named objects that map to sprite ids. So if you want to associate names to your sprites like "sword", "shield", etc to show that to the user, this is a nice way to do it.
Example usage in a RPG:
tiles = enum( {"grass" ,"water", "rock"} ) -- landscape sprites start at 1 items = enum( {"sword" ,"shield", "bow"}, 16 ) -- item sprites start at 16 colors = enum( {"black", "blue", ... }, 0 ) -- map color palette. actions = enum( {"look", "take", ... } ) -- just an enum. |
You can now use the enums to make your code more readable:
if action==actions.take then mset(x,y,tiles.grass.id) pset(x,y,colors.green.id) end |
- You can resolve to its name like tiles.water.name.
- You can resolve by its id, eg: tiles[3].name
- you can iterate over all enum entries like: for item in items.all() do print(item.name) end
- You can offset the ids by using tiles=enum(names, 10). Useful if your sprites start at index 10.
- You can also map arbitrary ids to objects like furniture=enum({[15]="table", [20]="chair"}).
- You can use the generated objects as a starting point for more complex objects like furniture.table.heavy=true,
or attach functionality like:
actions.look.execute = function(item) print("Looks like a usual "..item.name) end actions.take.execute = function(item) if not item.heavy then add(inventory, item) end end function onactionuse(action, item) action.execute(item) end |
Keep in mind that the usage of enums consumes more tokens than using magic numbers. You trade tokens for readability and elegant code.
I'm just starting a new game from scratch and programming pico-8 feels really nice :)
So the first step is to have a cursor moving on a map. I wanted it to be really smooth and snappy and fast. So this is what I came up with.
Feel free to use it in your projects :)
I'm happy to announce my very first contribution to the Pico-8 community, which is a spin-off of Makiki's game title "A lone colony on a small planet".
Your goal now is to colonize an even smaller planet, but with some difficulties added to it. So this one is a more of a thinking game, while Makikis original is more of fast builder.
Credits:
Story:
"You have been given a mission. You need to purify a small planet, so we will be able to colonize it. It is rich in resources, so it shouldn't pose a big problem for you. We believe in your management skills. Good luck." -- Makiki
Controls:
- O to build.
- X is a multi tool.
- build faster. you can keep it pressed and move around.
- select what to build by using X over an existing building
- get information about a building
- change transport type
- show the map and statistics
Hint to get started: produce energy and connect the energy producer to quarries. Once powered, they start mining minerals.
More hints (spoiler):
Development details (spoiler):
Hi there,
I wrote a script which helps to write lua code in an external editor. The script listens to changes in the .lua file and re-integrates the lua code back into the .p8 cartridge. Each time you save.
So you can happily change lua files and edit assets in Pico-8 and everything gets merged back as soon as you save. Write some code in your editor, switch to Pico-8 and press ctrl-R to reload. It's quick :)
Works great for me in Intelij Idea, where I have syntax highlighting, code analysis and code completion. When trying to call a function, the IDE even shows the function parameters and lua-doc. What more can you ask for?
Some other things the script can do for you
- include library code (#include library)
- remove debug code on release (#define debug, #if debug, #end debug)
- strip comments if you hit the size limit (please consider that comments are helpful for the Pico-8 community ;))
- convert pico-8 specific statements like += to plain .lua. Some IDEs might work better with plain lua.
That's it, hope you like it. Download it here:
https://github.com/sulai/p8lua
Have a look in the python file, there is more detailled und up to date instructions on the features.
PS: one down side though: the script works for Linux only at the moment, since it uses pyinotify for getting notified about file changes. I'm happy to accept pull requests if you want to port it to windows, maybe doing polling on the .lua files.
In case your game gets slow, it's always helpful to identify the parts of code that take up too much time to compute. This is how you can measure execution time:
local start = stat(1) -- .... do some intensive stuff .... printh("this took "..((stat(1)-start)*100).."% of a frame") |
Hope it helps, and thanks to @Felice for pointing out how to accomplish this.
sulai
You can use this function in your program to do some basic memory profiling. Given the limit of 1MB memory for Lua variables, a little profiling will be much needed for many of us ;) It will return the amount of all elements of a table, including the content of sub tables.
It will not show the actual amount of bytes used, would be interesting if there is a way to calculate that? But you can use this as a rough estimation and check if your optimizations show any effect on the table size.
table={} function table.size(t) local size=0 for k,v in pairs(t) do size+=1 if type(v)=="table" then size+=table.size(v) end end return size end |
-- converts anything to string, even nested tables function tostring(any) if type(any)=="function" then return "function" end if any==nil then return "nil" end if type(any)=="string" then return any end if type(any)=="boolean" then if any then return "true" end return "false" end if type(any)=="table" then local str = "{ " for k,v in pairs(any) do str=str..tostring(k).."->"..tostring(v).." " end return str.."}" end if type(any)=="number" then return ""..any end return "unkown" -- should never show end |
Example usage:
> print(""..tostring({true,x=7,{"nest"}})) { 1->true 2->{ 1->nest } x->7 } |