Need help with lua? - LunaLua General Help

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
User avatar
marioman2007
Ripper II
Ripper II
Posts: 300
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
Just learning Lua and Importing Graphics

SmgLifeSystem.lua - Custom Health and more!

The Super Cool Pack: I support:
Spoiler: show
Image
Image

User avatar
Hoeloe
Foo
Foo
Posts: 1413
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

User avatar
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.

User avatar
marioman2007
Ripper II
Ripper II
Posts: 300
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
Just learning Lua and Importing Graphics

SmgLifeSystem.lua - Custom Health and more!

The Super Cool Pack: I support:
Spoiler: show
Image
Image

User avatar
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???

User avatar
marioman2007
Ripper II
Ripper II
Posts: 300
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
Just learning Lua and Importing Graphics

SmgLifeSystem.lua - Custom Health and more!

The Super Cool Pack: I support:
Spoiler: show
Image
Image

User avatar
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?

User avatar
Dragon0307
Monty Mole
Monty Mole
Posts: 138
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.
If you are still running Windows 7, your security is at stake. And no, Antivirus is not enough. Switch to Linux!™

This ForumPost® is the intellectual property (IP) of Kamekverse LTD. All rights reserved.
UNAUTHORIZED USAGE IS VIABLE FOR CRIMINAL CHARGES ON THE GROUNDS OF COPYRIGHT INFRINGEMENT
ForumPost® is a registered patent of Kamekverse LTD.
Spoiler: show
Knuckles is the bad guy

User avatar
Hoeloe
Foo
Foo
Posts: 1413
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.

User avatar
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

User avatar
Hoeloe
Foo
Foo
Posts: 1413
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.

User avatar
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: 20
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?

User avatar
marioman2007
Ripper II
Ripper II
Posts: 300
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
Just learning Lua and Importing Graphics

SmgLifeSystem.lua - Custom Health and more!

The Super Cool Pack: I support:
Spoiler: show
Image
Image

User avatar
Dragon0307
Monty Mole
Monty Mole
Posts: 138
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"
If you are still running Windows 7, your security is at stake. And no, Antivirus is not enough. Switch to Linux!™

This ForumPost® is the intellectual property (IP) of Kamekverse LTD. All rights reserved.
UNAUTHORIZED USAGE IS VIABLE FOR CRIMINAL CHARGES ON THE GROUNDS OF COPYRIGHT INFRINGEMENT
ForumPost® is a registered patent of Kamekverse LTD.
Spoiler: show
Knuckles is the bad guy

User avatar
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?

User avatar
deice
Dolphin
Dolphin
Posts: 93
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.

User avatar
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))


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 1 guest