Log In  

@sulai

Follow
Follow

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.

P#43901 2017-09-03 18:33 ( Edited 2017-09-03 22:33)

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

Cart [#43864#] | Code | 2017-09-02 | License: CC4-BY-NC-SA | Embed

P#43862 2017-09-02 13:08 ( Edited 2017-09-02 17:22)

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

Cart [#58904#] | Code | 2018-11-10 | License: CC4-BY-NC-SA | Embed
5

Hint to get started: produce energy and connect the energy producer to quarries. Once powered, they start mining minerals.

More hints (spoiler):

  • you can build "transport" tiles to transport about everything: energy, coal and oil
  • prefer hydro plants over solar panels, they are cheaper. connect the hydro plants to your quarries with transport
  • building transportation is cheap. you can use them as a switch by removing a transport tile. It's possible to re-route energy that way.
  • activate the core by delivering coal and oil to it
  • once activated, it functions as the core of a purifier cluster. All purifiers adjacent to it work together.

Development details (spoiler):


I liked the setting of Makiki's game, so I invested 2 weeks into it to add features, tweak the game and learn about Pico-8 and Lua along the way.

Feature list:

  • random map generator (had to remove Makiki's hand drawn map, sadly)
  • producers of energy, coal and oil need to be connected to their producers
    • to get the path finding/validation to work took me ages. My very first experience with Lua though.
  • transport tiles to transport any goods over a larger distance
  • core works as a purifier cluster core.
    • I think this one is a nice idea, as it replaces late-game grinding with late-game "I need to rewire a few things". On the other hand, It's perfectly possible to win without activating and using the core, and I think it's both fun in their own ways.
  • lots of tweaks and balancing on the ui, controls and game elements
  • lots of memory and cpu optimization. I still ended up to reduce map size to prevent out of memory errors in the late game. Which actually results in a good game length. It finishes just before you need to start grinding. Thus the title "Colony on a tiny planet. ;)
P#43859 2017-09-02 10:55 ( Edited 2018-11-10 00:56)

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.

P#43725 2017-08-29 07:30 ( Edited 2017-09-07 03:59)

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

P#43676 2017-08-27 08:46 ( Edited 2017-08-27 14:51)

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
P#43654 2017-08-26 16:03 ( Edited 2017-08-27 15:20)

-- 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 }
P#43636 2017-08-26 07:12 ( Edited 2017-12-07 14:13)

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-07-19 17:02 | 0.107s | 4194k | Q:39