andytudhope / what

A better way to make memes on ao
5 stars 2 forks source link

Load more complex functions #1

Open andytudhope opened 7 months ago

andytudhope commented 7 months ago

As described in the TODO here, this approach does not load anything more complex than Handlers.utils.whatever.

How do I load actual functions? It may just be a case of structuring the message to the process slightly differently? Or it may require deeper changes to the lua script...

twilson63 commented 7 months ago

Sure you can just define a function and it will load into the Global Scope

function MyGreeting(name)
  return "Hello " .. name
end

But you may want to organize better, Lua has modules which are files with a simple scope

You can create a module locally like

mod.lua

local mod = { _version = "0.1" }

function mod.greeting(name)
  return "Hello " .. name
end

return mod

Then in your load file main.lua

local mod = require('.mod')

function hello()
  mod.greeting('rakis')
end
Screenshot 2024-03-09 at 10 26 22 AM
twilson63 commented 7 months ago

You can also implement metatables - here is an example

https://github.com/permaweb/aos/blob/main/blueprints/credUtils.lua