Page 22 of 27

Re: Need help with lua? - LunaLua General Help

Posted: Fri Jan 14, 2022 9:33 am
by Marioman2007
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

Re: Need help with lua? - LunaLua General Help

Posted: Fri Jan 14, 2022 3:59 pm
by Hoeloe
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

Re: Need help with lua? - LunaLua General Help

Posted: Fri Jan 14, 2022 5:02 pm
by Goldenemerl64
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.

Re: Need help with lua? - LunaLua General Help

Posted: Fri Jan 14, 2022 8:56 pm
by Marioman2007
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

Re: Need help with lua? - LunaLua General Help

Posted: Sat Jan 15, 2022 12:46 am
by Goldenemerl64
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???

Re: Need help with lua? - LunaLua General Help

Posted: Sat Jan 15, 2022 7:15 am
by Marioman2007
You have two onEvents

Re: Need help with lua? - LunaLua General Help

Posted: Sat Jan 15, 2022 3:32 pm
by Goldenemerl64
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?

Re: Need help with lua? - LunaLua General Help

Posted: Sun Jan 16, 2022 6:01 pm
by Dragon0307
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.

Re: Need help with lua? - LunaLua General Help

Posted: Sun Jan 16, 2022 8:37 pm
by Hoeloe
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.

Re: Need help with lua? - LunaLua General Help

Posted: Tue Jan 18, 2022 6:39 am
by Goldenemerl64
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

Re: Need help with lua? - LunaLua General Help

Posted: Wed Jan 19, 2022 8:46 am
by Hoeloe
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.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Jan 19, 2022 5:45 pm
by Goldenemerl64
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.

Re: Need help with lua? - LunaLua General Help

Posted: Fri Jan 21, 2022 6:31 pm
by ChunkyChimp
Is there a tag to draw an image an make it transparent when Graphics.draw - ing it? Like transparency = 90% or something else?

Re: Need help with lua? - LunaLua General Help

Posted: Fri Jan 21, 2022 10:00 pm
by Marioman2007
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

Re: Need help with lua? - LunaLua General Help

Posted: Sat Jan 22, 2022 9:43 am
by Dragon0307
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"

Re: Need help with lua? - LunaLua General Help

Posted: Sat Jan 22, 2022 10:03 pm
by Goldenemerl64
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?

Re: Need help with lua? - LunaLua General Help

Posted: Sun Jan 23, 2022 7:22 am
by deice
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.

Re: Need help with lua? - LunaLua General Help

Posted: Mon Jan 24, 2022 7:40 am
by Goldenemerl64
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))

Re: Need help with lua? - LunaLua General Help

Posted: Mon May 16, 2022 3:07 am
by Iggizorn
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?

Re: Need help with lua? - LunaLua General Help

Posted: Mon May 16, 2022 11:43 am
by deice
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