OnEvent wrapper for easy usage

This is the place for discussion and support for LunaLua and related modifications and libraries.
Forum rules
Before you make a topic/post, consider the following:
-Is there a topic for this already?
-Is your post on topic/appropriate?
-Are you posting in the right forum/following the forum rules?
User avatar
Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 6560
Joined: Mon Jan 20, 2014 12:58 pm
Flair: -chan ^^

OnEvent wrapper for easy usage

Postby Enjl » Sat Oct 13, 2018 4:14 am

Tired of being confused at the structure of onEvent/onEventDirect? Do you always get lost in a heap of if/elseif statements that seem odd?
Maybe not. Maybe yes. Here's a cleaner way to make entirely seperate per-event onEvent branches anyway.

In every case of the different examples, all that needs to be done is to create a new function and reference it in the table with the key as the name of the event that should call it. Multiple events can, of course, call to the same function, by binding the function to 2 event names seperately.
It comes in 3 flavours:

OnEvent and I don't care about which event was fired:

Code: Select all

--write your code for onEvent into functions like this

local function myFirstEventFunc()
    -- do stuff
end

--reference functions to have them be called at the right time

local eventFuncs = {
    ["Level-Start"] = myFirstEventFunc,
    ["mycustomevent"] = myFirstEventFunc
}

--onEvent doesn't need to be touched

function onEvent(eventname)
    if eventFuncs[eventname] then eventFuncs[eventname]()
end

OnEvent and I do care about what was fired:

Code: Select all

--write your code for onEvent into functions like this

local function myFirstEventFunc(eventname)
    -- do stuff
end

--reference functions to have them be called at the right time

local eventFuncs = {
    ["Level-Start"] = myFirstEventFunc,
    ["mycustomevent"] = myFirstEventFunc
}

--onEvent doesn't need to be touched

function onEvent(eventname)
    if eventFuncs[eventname] then eventFuncs[eventname](eventname)
end
I want to be able to cancel events so I'm using onEventDirect:

Code: Select all

--write your code for onEvent into functions like this

local function myFirstEventFunc(eventobj, eventname)
    -- do stuff
end

--reference functions to have them be called at the right time

local eventFuncs = {
    ["Level-Start"] = myFirstEventFunc,
    ["mycustomevent"] = myFirstEventFunc
}

--onEvent doesn't need to be touched

function onEventDirect(eventobj, eventname)
    if eventFuncs[eventname] then eventFuncs[eventname](eventobj, eventname)
end
Might be helpful for those of you who previously got confused at the notion of onEvent("killboss1") not working. This is kind of the next best thing. That specific concept is kind of represented in the eventFuncs table with lines like ["mycustomevent"] = myFirstEventFunc.
ImageImageImageImage
ImageImageImageImageImageImage
Muh favourite quotes:
Spoiler: show
TheKingOfBoos: enji I'm only scared of you cuz you're always right, and that's creepy
SnoruntPyro: if you don't like kirby you have no life
ToonQuill: do you have any hentai girl sfx
Pichu: "And Jesus said unto his apostles, 'ew so much clash 0/10'."
MNIE52: ohoho master king sitting in his golden chair! Dude, you think you're president? i will not leave, if i want - i'll leave.
MNIE52: i will not kill you, why you so rude. are you have nothing to do, boy?
Sanct: oh my god i made the perfect amount of stairs i am jesus christ.
Spoiler: show
Image

User avatar
ThePieSkyHigh
Blooper
Blooper
Posts: 199
Joined: Fri Jun 15, 2018 3:03 pm
Flair: "Nothing is impossible!" But I do nothing all day.
Current Project: Jump up super star!
Location: Mr. Foofy Land
Chat Username: ThePieSkyHigh
Contact:

Re: OnEvent wrapper for easy usage

Postby ThePieSkyHigh » Fri Oct 19, 2018 1:53 pm

This will help a lot of people.

I don't use a lot of events and layers yet, but I'll get to it someday.

Somebody is gonna quote me saying: "Please make more productive posts."

:/
--Might not be on too much because of internet issues, I'll try to read up everything I missed including PMs, posts, so don't back down from sending any to me-- ;)

Image
Go check out my collection of levels: viewtopic.php?f=89&t=22422

You may call me "Pie" if you'd like.

If you want a logo for your episode, go PM AndrewPixel, He made a great logo for me and he will for you!

Working on:
Spoiler: show
Image

A game with a classic selection of world styles, each having 5 levels to play!
Demo is out now! viewtopic.php?f=90&t=22538&p=336507#p336507


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 3 guests