Page 1 of 1

Events with lunalua

Posted: Wed Jan 15, 2020 7:04 pm
by KnuckleChuckle
Another quick question on here. Is it possible to activate a lunalua script with events?

Re: Events with lunalua

Posted: Wed Jan 15, 2020 7:19 pm
by FireyPaperMario
It is 100% possible to active events using Lunalua! But can you explain what events you were thinking of?

Re: Events with lunalua

Posted: Wed Jan 15, 2020 8:20 pm
by KnuckleChuckle
So i'm trying to activate a code that lets the player switch characters by the push of a button, and its supposed to activate once you talk to peach, because then talking to her would activate the event.

Re: Events with lunalua

Posted: Wed Jan 15, 2020 9:18 pm
by 8lue Storm
KnuckleChuckle wrote:
Wed Jan 15, 2020 8:20 pm
So i'm trying to activate a code that lets the player switch characters by the push of a button, and its supposed to activate once you talk to peach, because then talking to her would activate the event.
Wait, so you gain the ability of switching characters after talking to Peach or do you switch characters right after talking to Peach?

Re: Events with lunalua

Posted: Wed Jan 15, 2020 9:25 pm
by KnuckleChuckle
You gain the ability

Re: Events with lunalua

Posted: Thu Jan 16, 2020 12:17 am
by Daring Tombstone
Assuming this what you're asking here's an example.

Image

Nevermind I'm a special kind of stupid.

Re: Events with lunalua

Posted: Thu Jan 16, 2020 1:07 am
by Enjl
Daring Tombstone wrote:
Thu Jan 16, 2020 12:17 am
Assuming this what you're asking here's an example.

Image

Pretty simple yeah?
Not quite. The behaviour expected is an unlock, from what I gather. So, instead of putting all the code into onEvent, it would be better to use onEvent to toggle a boolean variable that otherwise gates off the code that runs in other events like onTick (for input checking and logic) and onDraw (if there are any components to draw to the screen).
Image
Setting "canSwitchCharacters" to true in onEvent would be emblematic of the action of "gaining the ability", since it allows the code inside the if statement in other events to execute.

In the spoiler below is a version that permanently sets the variable for the episode, even across levels (MAGLX3 build, PAL build and newer versions only):
Spoiler: show
Image

Re: Events with lunalua

Posted: Thu Jan 16, 2020 7:05 pm
by KnuckleChuckle
hmmm. On event never seems to work for me. I keep getting the error end is expected(to close function at line 1) near <eof>. But when I add end it says <eof> expected near end.

Re: Events with lunalua

Posted: Thu Jan 16, 2020 8:14 pm
by Hoeloe
That suggests you're adding it in completely the wrong place. Copy the format shown above, and if it still doesn't work, post your code here.

Re: Events with lunalua

Posted: Thu Jan 16, 2020 9:11 pm
by KnuckleChuckle
SaveData.canSwitchCharacters = false
function onEvent(eventName)
if eventName == "Transformation" then
SaveData.canSwitchCharacters = true
end
end


function onTick()
if SaveData.canSwitchCharacters then
function onKeyboardPress(keycode)
if keycode == 0x31 then
player:transform(CHARACTER_MARIO)
elseif keycode == 0x32 then
player:transform(CHARACTER_PEACH)
elseif keycode == 0x33 then
player:transform(CHARACTER_TOAD)
end
end
end
end

Re: Events with lunalua

Posted: Thu Jan 16, 2020 9:31 pm
by Daring Tombstone
You can't have a function in a function I don't believe. You would need to end function ontick before starting function onkeyboardpress. But I would see what a lua expert says.

Re: Events with lunalua

Posted: Thu Jan 16, 2020 10:31 pm
by KnuckleChuckle
Image

Re: Events with lunalua

Posted: Fri Jan 17, 2020 12:58 am
by Daring Tombstone
Would you need onTick here? Can't we just do something like.

function onKeyBoardPress(keycode)
if SaveData.canSwitchCharacters == true then
if keycode == 0x31 then
player:transform(CHARACTER_MARIO)
elseif keycode == 0x32 then
player:transform(CHARACTER_PEACH)
end
end
end

Or is this not the right way to do this? I'm a lua noob myself. Also in your script if SaveData.canSwitchCharacters needs a double ='s since it's checking the boolean.

Re: Events with lunalua

Posted: Fri Jan 17, 2020 1:18 am
by Enjl
OnKeyboardPress is inconvenient because it assumes players never change their control layout. Imagine setting the keys to Q and E just to find out someone plays your level with a controller. Whoops!
But if you really wanna do this, then Daring Tombstone's suggestion is right. In my prior post when I suggested onTick for input checking I assumed you wanted to make use of the inputs SMBX already recognizes.

Re: Events with lunalua

Posted: Tue Apr 28, 2020 4:02 pm
by KnuckleChuckle
My concern isn't really about the whole controller thing since i'd assume most would have a keyboard if they were to access the internet. So if onkeyboard press is inconvenient then what would I use?

Re: Events with lunalua

Posted: Tue Apr 28, 2020 4:12 pm
by Enjl
If you want to rely on keyboard and people not happening to use the keys you use, onKeyboardPress.
If you want to instead use SMBX's internal controls, you check for them in onTick with stuff like if player.keys.[down/up/left/right/altRun/altJump/dropItem/jump/run/pause] == KEYS_[PRESSED/DOWN/UP/UNPRESSED] then (where the options in brackets are the available keys and states). (you will probably have to rethink the input method into some form of menu. Sounds a tad more intiutive than pressing a random key either way.)
That's the two options you have.

Re: Events with lunalua

Posted: Tue Apr 28, 2020 11:52 pm
by Hoeloe
Enjl wrote:
Tue Apr 28, 2020 4:12 pm
if player.keys.[down/up/left/right/altRun/altJump/dropItem/jump/run/pause] == KEYS_[PRESSED/DOWN/UP/UNPRESSED] then
I do want to stress here that your options should really be:

if player.keys.jump == KEYS_PRESSED then (for "player just pressed the key")

if player.keys.jump == KEYS_RELEASED then (for "player just released the key")

if player.keys.jump then (for "player is holding the key")

if not player.keys.jump then (for "player is not holding the key")


KEYS_UP and KEYS_DOWN are very niche and shouldn't be used in most circumstances.

Re: Events with lunalua

Posted: Wed Apr 29, 2020 2:51 pm
by KnuckleChuckle
My apologies if I'm inconveniencing any of you. The whole process to get the the character unlock does not seem to be working.
Image


I had the event activate when you talk to peach, however when I test it for myself nothing happens. I think I'm still doing this wrong

Re: Events with lunalua

Posted: Wed Apr 29, 2020 3:11 pm
by Enjl
Looks like you capitalized onKeyboardPress wrong. The "b" needs to be lowercase: https://wohlsoft.ru/pgewiki/LunaLua_events

Re: Events with lunalua

Posted: Wed Apr 29, 2020 3:20 pm
by KnuckleChuckle
dang you're right. It works perfectly now. Thank you!