Need help with lua? - LunaLua General Help

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
Marioman2007
Ripper II
Ripper II
Posts: 337
Joined: Tue Aug 25, 2020 3:19 am
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Fri Jan 14, 2022 9:33 am

uhh what?
To activate an event, you need

Code: Select all

triggerEvent("youEventNameHere")
So like

Code: Select all

if number == 1 then
    triggerevent("yourEventNameHere")
end
Also onEvent doesnt work like that
It's like

Code: Select all

function onEvent("eventName")
    if eventName == "yourEventNameHere" then
        -- do stuff
    end
end

Hoeloe
Foo
Foo
Posts: 1420
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Pronouns: she/her

Re: Need help with lua? - LunaLua General Help

Postby Hoeloe » Fri Jan 14, 2022 3:59 pm

marioman2007 wrote:
Fri Jan 14, 2022 9:33 am
It's like

Code: Select all

function onEvent("eventName")
    if eventName == "yourEventNameHere" then
        -- do stuff
    end
end
This is incorrect. You should not use quotes in function arguments. The correct code is:

Code: Select all

function onEvent(eventName)
    if eventName == "yourEventNameHere" then
        -- do stuff
    end
end

Goldenemerl64
Rex
Rex
Posts: 37
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Fri Jan 14, 2022 5:02 pm

Sorry to bother you all again, but...

Code: Select all

function onEvent(eventName)
    if eventName == "Attack Choose" then 
    local randomNumber1 = math.random(1, 3)
    end
end

    function onEvent(eventName)
    if number == 1 then
    triggerEvent("Attack 1")
    end
end

    function onEvent(eventName)
    if number == 2 then
    triggerEvent("Attack 2")
    end
end

    function onEvent(eventName)
    if number == 3 then
    triggerEvent("Attack 3")
    end
end
Is something not working correctly?

I'm trying to get a boss to pick a random move.

Marioman2007
Ripper II
Ripper II
Posts: 337
Joined: Tue Aug 25, 2020 3:19 am
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Fri Jan 14, 2022 8:56 pm

Hoeloe wrote:
Fri Jan 14, 2022 3:59 pm
This is incorrect. You should not use quotes in function arguments. The correct code is:
oh my bad

Goldenemerl64 wrote:
Fri Jan 14, 2022 5:02 pm
Spoiler: show

Code: Select all

function onEvent(eventName)
    if eventName == "Attack Choose" then 
    local randomNumber1 = math.random(1, 3)
    end
end

    function onEvent(eventName)
    if number == 1 then
    triggerEvent("Attack 1")
    end
end

    function onEvent(eventName)
    if number == 2 then
    triggerEvent("Attack 2")
    end
end

    function onEvent(eventName)
    if number == 3 then
    triggerEvent("Attack 3")
    end
end
Spoiler: show

Code: Select all

function onEvent(eventName)
    if eventName == "Attack Choose" then 
        local randomNumber1 = math.random(1, 3)
    end

    if number == 1 then
        triggerEvent("Attack 1")
    end

    if number == 2 then
        triggerEvent("Attack 2")
    end

    if number == 3 then
        triggerEvent("Attack 3")
    end
end

Goldenemerl64
Rex
Rex
Posts: 37
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Sat Jan 15, 2022 12:46 am

marioman2007 wrote:
Fri Jan 14, 2022 8:56 pm
Hoeloe wrote:
Fri Jan 14, 2022 3:59 pm
This is incorrect. You should not use quotes in function arguments. The correct code is:
oh my bad

Goldenemerl64 wrote:
Fri Jan 14, 2022 5:02 pm
Spoiler: show

Code: Select all

function onEvent(eventName)
    if eventName == "Attack Choose" then 
    local randomNumber1 = math.random(1, 3)
    end
end

    function onEvent(eventName)
    if number == 1 then
    triggerEvent("Attack 1")
    end
end

    function onEvent(eventName)
    if number == 2 then
    triggerEvent("Attack 2")
    end
end

    function onEvent(eventName)
    if number == 3 then
    triggerEvent("Attack 3")
    end
end
Spoiler: show

Code: Select all

function onEvent(eventName)
    if eventName == "Attack Choose" then 
        local randomNumber1 = math.random(1, 3)
    end

    if number == 1 then
        triggerEvent("Attack 1")
    end

    if number == 2 then
        triggerEvent("Attack 2")
    end

    if number == 3 then
        triggerEvent("Attack 3")
    end
end
Well thank you. I Tried it and then there is this:

0t - Warning: Overwritten handler 'onEvent' in worlds/Super Mario Adventure Plus/Rocky Mountains/luna.lua
stack traceback:
main.lua:718: in function '__newindex'
worlds/Super Mario Adventure Plus/Rocky Mountains/luna.lua:29: in function 'codeFile'
main.lua:743: in function 'loadCodeFile'
main.lua:893: in function <main.lua:793>
[C]: in function '__xpcall'
main.lua:793: in function <main.lua:792>

what else is wrong with the code???

Marioman2007
Ripper II
Ripper II
Posts: 337
Joined: Tue Aug 25, 2020 3:19 am
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Sat Jan 15, 2022 7:15 am

You have two onEvents

Goldenemerl64
Rex
Rex
Posts: 37
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Sat Jan 15, 2022 3:32 pm

Got the thing working, but now when it activates it crashes.

Code: Select all

    if eventName == "Attack Choose" then 
       randomNumber1 = math.random(1, 3)
    end

    if randomNumber1 == 1 then
        triggerEvent("Attack 1")
    end

    if randomNumber1 == 2 then
        triggerEvent("Attack 2")
    end

    if randomNumber1 == 3 then
        triggerEvent("Attack 3")
    end
end
Any way to fix this?

Dragon0307
Tweeter
Tweeter
Posts: 174
Joined: Sun Dec 08, 2019 8:21 am
Flair: Faux Affably Evil
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Dragon0307 » Sun Jan 16, 2022 6:01 pm

Goldenemerl64 wrote:
Sat Jan 15, 2022 3:32 pm
Got the thing working, but now when it activates it crashes.

Code: Select all

    if eventName == "Attack Choose" then 
       randomNumber1 = math.random(1, 3)
    end

    if randomNumber1 == 1 then
        triggerEvent("Attack 1")
    end

    if randomNumber1 == 2 then
        triggerEvent("Attack 2")
    end

    if randomNumber1 == 3 then
        triggerEvent("Attack 3")
    end
end
Any way to fix this?
You should upload a screenshot with the Snipping Tool so that people can see what the issue is.

This probably won't help since it does effectively the same thing, but I probably would have written the script like this:
Spoiler: show

Code: Select all

    
    if eventName == "Attack Choose" then 
       randomNumber1 = math.random(1, 3)
    elseif randomNumber1 == 1 then
        triggerEvent("Attack 1")
    elseif randomNumber1 == 2 then
        triggerEvent("Attack 2")
    elseif randomNumber1 == 3 then
        triggerEvent("Attack 3")
    end
end
It's worth seeing if this implementation would be better or worse than what you already have. I obviously can't try it for myself since I don't have access to your full script and level. The issue may have something to do with elsewhere in your script.

Hoeloe
Foo
Foo
Posts: 1420
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Pronouns: she/her

Re: Need help with lua? - LunaLua General Help

Postby Hoeloe » Sun Jan 16, 2022 8:37 pm

Just as an aside, you should probably be using RNG.random rather than math.random. RNG.random is more reliable and in most cases faster than math.random.

Goldenemerl64
Rex
Rex
Posts: 37
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Tue Jan 18, 2022 6:39 am

Dragon0307 wrote:
Sun Jan 16, 2022 6:01 pm
Goldenemerl64 wrote:
Sat Jan 15, 2022 3:32 pm
Got the thing working, but now when it activates it crashes.

Code: Select all

    if eventName == "Attack Choose" then 
       randomNumber1 = math.random(1, 3)
    end

    if randomNumber1 == 1 then
        triggerEvent("Attack 1")
    end

    if randomNumber1 == 2 then
        triggerEvent("Attack 2")
    end

    if randomNumber1 == 3 then
        triggerEvent("Attack 3")
    end
end
Any way to fix this?
You should upload a screenshot with the Snipping Tool so that people can see what the issue is.

This probably won't help since it does effectively the same thing, but I probably would have written the script like this:
Spoiler: show

Code: Select all

    
    if eventName == "Attack Choose" then 
       randomNumber1 = math.random(1, 3)
    elseif randomNumber1 == 1 then
        triggerEvent("Attack 1")
    elseif randomNumber1 == 2 then
        triggerEvent("Attack 2")
    elseif randomNumber1 == 3 then
        triggerEvent("Attack 3")
    end
end
It's worth seeing if this implementation would be better or worse than what you already have. I obviously can't try it for myself since I don't have access to your full script and level. The issue may have something to do with elsewhere in your script.

perhaps you're right I might as well show the full script

Code: Select all

local autoscroll = require("autoscroll") 
function onLoadSection0() 
    autoscroll.scrollRight(2.0) 
end

function onLoadSection2() 
    autoscroll.scrollRight(2.0)
end

function onTick()
    if player.deathTimer > 0 then return end
    if player:mem(0x148, FIELD_WORD) > 0
    and player:mem(0x14C, FIELD_WORD) > 0 then;
        player:kill()
    end
end

function onEvent(eventName)
    if eventName == "Up" then 
    autoscroll.scrollTo(-183968, -203040, 1.0)
    end

    if eventName == "Right" then 
    autoscroll.scrollRight(2.0)
    end

if eventName == "Attack Choose" then 
       randomNumber1 = RNG.random(1, 3)
    end

    if randomNumber1 == 1 then
        triggerEvent("Attack 1")
    end

    if randomNumber1 == 2 then
        triggerEvent("Attack 2")
    end

    if randomNumber1 == 3 then
        triggerEvent("Attack 3")
    end
end
Is there anything wrong with it?
Hoeloe wrote:
Sun Jan 16, 2022 8:37 pm
Just as an aside, you should probably be using RNG.random rather than math.random. RNG.random is more reliable and in most cases faster than math.random.
I tried replacing math.random with RNG.random and nothing is happening. (A message is supposed to appear saying 1, 2, or 3. (just to see if it works)) am I doing it wrong?

Image

Hoeloe
Foo
Foo
Posts: 1420
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Pronouns: she/her

Re: Need help with lua? - LunaLua General Help

Postby Hoeloe » Wed Jan 19, 2022 8:46 am

Ah, if you're trying to select an integer, you want RNG.randomInt, otherwise it's picking any number in the range, including fractional numbers.

Also you can condense that whole attack thing to just this:

Code: Select all

if eventName == "Attack Choose" then
    triggerEvent("Attack "..RNG.randomInt(1,3))
end
.. is the string concatenator, so this creates a string reading either "Attack 1", "Attack 2", or "Attack 3", and then triggers the event. No need to individually test each one.

Goldenemerl64
Rex
Rex
Posts: 37
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Wed Jan 19, 2022 5:45 pm

Hoeloe wrote:
Wed Jan 19, 2022 8:46 am
Ah, if you're trying to select an integer, you want RNG.randomInt, otherwise it's picking any number in the range, including fractional numbers.

Also you can condense that whole attack thing to just this:

Code: Select all

if eventName == "Attack Choose" then
    triggerEvent("Attack "..RNG.randomInt(1,3))
end
.. is the string concatenator, so this creates a string reading either "Attack 1", "Attack 2", or "Attack 3", and then triggers the event. No need to individually test each one.
It worked! Thank you so much!! :D

Added in 3 hours 50 minutes 8 seconds:
Uh how do I add change music files

[level-music-6]
name="SMB3 Battle"
file="music_ogg/smb3-boss.ogg"

How do I change it (the one highlighted in blue) to the one highlighted in red?

Rocky Mountains/15 Boss.ogg

I change it on the music.ini but nothing happens.

ChunkyChimp
Bob-Omb
Bob-Omb
Posts: 22
Joined: Tue Sep 01, 2020 9:02 pm
Flair: I TAKE GFX FOR MY OWN LEVELS

Re: Need help with lua? - LunaLua General Help

Postby ChunkyChimp » Fri Jan 21, 2022 6:31 pm

Is there a tag to draw an image an make it transparent when Graphics.draw - ing it? Like transparency = 90% or something else?

Marioman2007
Ripper II
Ripper II
Posts: 337
Joined: Tue Aug 25, 2020 3:19 am
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Fri Jan 21, 2022 10:00 pm

ChunkyChimp wrote:
Fri Jan 21, 2022 6:31 pm
Is there a tag to draw an image an make it transparent when Graphics.draw - ing it? Like transparency = 90% or something else?
you could set opacity to 0.9
https://docs.codehaus.moe/#/reference/g ... -functions

Dragon0307
Tweeter
Tweeter
Posts: 174
Joined: Sun Dec 08, 2019 8:21 am
Flair: Faux Affably Evil
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Dragon0307 » Sat Jan 22, 2022 9:43 am

Goldenemerl64 wrote:
Wed Jan 19, 2022 9:35 pm
Uh how do I add change music files

[level-music-6]
name="SMB3 Battle"
file="music_ogg/smb3-boss.ogg"

How do I change it (the one highlighted in blue) to the one highlighted in red?

Rocky Mountains/15 Boss.ogg

I change it on the music.ini but nothing happens.
Did you remember to include speech marks? For example:

Code: Select all

[level-music-6]
name="SMB3 Battle"
file="Rocky Mountains/15 Boss.ogg"

Goldenemerl64
Rex
Rex
Posts: 37
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Sat Jan 22, 2022 10:03 pm

Dragon0307 wrote:
Sat Jan 22, 2022 9:43 am
Goldenemerl64 wrote:
Wed Jan 19, 2022 9:35 pm
Uh how do I add change music files

[level-music-6]
name="SMB3 Battle"
file="music_ogg/smb3-boss.ogg"

How do I change it (the one highlighted in blue) to the one highlighted in red?

Rocky Mountains/15 Boss.ogg

I change it on the music.ini but nothing happens.
Did you remember to include speech marks? For example:

Code: Select all

[level-music-6]
name="SMB3 Battle"
file="Rocky Mountains/15 Boss.ogg"
Yeah, but that doesn't work either. does it work when it's in a level file?

deice
Snifit
Snifit
Posts: 218
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Sun Jan 23, 2022 7:22 am

Goldenemerl64 wrote:
Sat Jan 22, 2022 10:03 pm
Yeah, but that doesn't work either. does it work when it's in a level file?
remember that music.ini has to be either in your level folder or episode folder to work. i copied and pasted what dragon sent and the music was replaced just fine.

Goldenemerl64
Rex
Rex
Posts: 37
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Mon Jan 24, 2022 7:40 am

It's in the level folder and It's like that, but It's still playing the original song. Do i remove the unchanged songs?

Code: Select all

[level-music-6]
name="SMB3 Battle"
file="Rocky Mountains/15 Boss.ogg"
Unless i might be inputting the wrong location


Image(It's called "15 Boss" (I know it's weird))

Iggizorn
Shy Guy
Shy Guy
Posts: 6
Joined: Mon May 16, 2022 2:55 am
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Iggizorn » Mon May 16, 2022 3:07 am

Hi Guys! I've been looking for a script with which I can create an "autorun (like super mario run)" level for a long time. Function should be: Mario runs automatically, he runs faster with sprinting, spin jump should work. I can deactivate buttons, but I just can't find the automatic run....-.- Can someone help me there?

deice
Snifit
Snifit
Posts: 218
Joined: Fri Jul 23, 2021 7:35 am

Re: Need help with lua? - LunaLua General Help

Postby deice » Mon May 16, 2022 11:43 am

Iggizorn wrote:
Mon May 16, 2022 3:07 am
Hi Guys! I've been looking for a script with which I can create an "autorun (like super mario run)" level for a long time. Function should be: Mario runs automatically, he runs faster with sprinting, spin jump should work. I can deactivate buttons, but I just can't find the automatic run....-.- Can someone help me there?
if you're deactivating inputs using player.keys, you can force the player to hold certain keys in the same manner. while i haven't played super mario run, i can provide an example script that forces the player to run right but still lets them jump, which i assume is at least close to what you have in mind:

Code: Select all

function onInputUpdate()
	if(not Misc.isPaused()) then
		player.keys.left = false
		player.keys.down = false
		player.keys.altRun = false
		
		player.keys.run = true
		player.keys.right = true
	end
end


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 1 guest