Need help with lua? - LunaLua General Help

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
User avatar
Murphmario
Larry Koopa
Larry Koopa
Posts: 2163
Joined: Fri Dec 20, 2013 7:07 pm
Current Project: Project planned; waiting for SMBX2 Beta 4

Re: Need help with lua? - LunaLua General Help

Postby Murphmario » Fri Mar 27, 2020 1:50 pm

Code: Select all

function onTick()
	for k,v in ipairs(NPC.get(210)) do
		v.hp = v:mem(0x148, FIELD_FLOAT)
		Text.print(v.hp, 100, 100)
		if v.hp == 6 then
			triggerEvent(Halfway)
		end
	end
end
Not sure what I'm missing here. Probably something dumb.
Now taking graphics requests:
https://docs.google.com/forms/d/1urWpFq ... #responses

My graphics pack:
http://www.supermariobrosx.org/forums/v ... t=1960/url

Really tempted to just make a general graphics thread for all of my graphics instead of just making individual threads for each of them.

chasey1237
Shy Guy
Shy Guy
Posts: 9
Joined: Sat Nov 03, 2018 4:02 pm
Chat Username: chasey_epic

Re: Need help with lua? - LunaLua General Help

Postby chasey1237 » Mon Mar 30, 2020 11:15 pm

is it possible to lock a character to have a powerup in a level so they start with it and not lose it? if so, tell me how!

Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 8536
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Tue Mar 31, 2020 3:55 am

Murphmario wrote:
Fri Mar 27, 2020 1:50 pm

Code: Select all

function onTick()
	for k,v in ipairs(NPC.get(210)) do
		v.hp = v:mem(0x148, FIELD_FLOAT)
		Text.print(v.hp, 100, 100)
		if v.hp == 6 then
			triggerEvent(Halfway)
		end
	end
end
Not sure what I'm missing here. Probably something dumb.
Not sure what you're trying to do. Rinkas don't have HP and triggering an event that isn't even a string every frame sounds like a silly thing to do.
chasey1237 wrote:
Mon Mar 30, 2020 11:15 pm
is it possible to lock a character to have a powerup in a level so they start with it and not lose it? if so, tell me how!
Just use the Beta 4 powerup filter blocks in the Misc. tab by sprinkling them throughout your level where players can get hit.
CrazyMew37 wrote:
Fri Mar 27, 2020 1:03 pm
Hello! I want to create a stage effect in one of the sections of my stage where the lighting of the level goes down and then goes up again in a repeating manner. Perhaps this could be for both the backgrounds, blocks, and NPCs (If Possible), to make it a bit of a challenge. The Speed of the Light fading away and fading back should be quick, but not too quick. How could I possibly do this? (Also, this is just for one entire section of the level. Not the whole thing or a part of a section.)
Are you looking for something like the editor darkness setting (with light sources) or a semitransparent black overlay over everything?
The ABC of Creation
________________________________________________________
Image

User avatar
DrMekar
Koopa Shell
Koopa Shell
Posts: 649
Joined: Sat Apr 08, 2017 7:16 am
Flair: CUSTOM CHARACTER CREATOR
Current Project: Mario Heroes: The First Chapter Remade (MH1.5)
Chat Username: Doc
Contact:

Re: Need help with lua? - LunaLua General Help

Postby DrMekar » Tue Mar 31, 2020 8:46 am

Could someone help me with this function?

Code: Select all

local autoscroll = require("autoscroll") 
function onEvent(p) 
    if p == "Gore Starts" then 
        autoscroll.scrollRight(4, (.7)) 
    end
end
I'm trying to make the Screen Scroll once the Boss starts Moving.
Demo:
Image

(Also what is the usual Speed of a Yellow Grinder?)

Thanks in Advance
My Youtube Channel: https://www.youtube.com/channel/UC-8fzX ... subscriber

My Episodes & Projects:
ImageImage


If you want to support my Projects, put this in your signature. Thanks :)
Spoiler: show

Code: Select all

[url=https://www.supermariobrosx.org/forums/viewtopic.php?f=36&t=25110[/url]
Great Folks who support/ed my projects/in the past:

Suecra
jaf
Suckerman7 GM

Image

User avatar
Hoeloe
Kamikaze Koopa
Kamikaze Koopa
Posts: 1226
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Current Project: A2XT Episode 2

Re: Need help with lua? - LunaLua General Help

Postby Hoeloe » Tue Mar 31, 2020 9:44 am

To me, it looks like either your event name is wrong, the event isn't being triggered, or your Lua code isn't running at all.

Try hitting F2 when the game is running. That should show you which code files are currently running code, so if your luna.lua file isn't listed, it would mean that your code isn't running. If it is listed, it's likely to do with your events rather than your code. Remember, everything is case sensitive.

User avatar
DrMekar
Koopa Shell
Koopa Shell
Posts: 649
Joined: Sat Apr 08, 2017 7:16 am
Flair: CUSTOM CHARACTER CREATOR
Current Project: Mario Heroes: The First Chapter Remade (MH1.5)
Chat Username: Doc
Contact:

Re: Need help with lua? - LunaLua General Help

Postby DrMekar » Tue Mar 31, 2020 9:56 am

Hoeloe wrote:
Tue Mar 31, 2020 9:44 am
To me, it looks like either your event name is wrong, the event isn't being triggered, or your Lua code isn't running at all.

Try hitting F2 when the game is running. That should show you which code files are currently running code, so if your luna.lua file isn't listed, it would mean that your code isn't running. If it is listed, it's likely to do with your events rather than your code. Remember, everything is case sensitive.
My Lua Code is defenitly running since the Music Change (A Diffrent Code below) works. It also shows the Libary. Is there something I need
to do in the Event? The Event that should trigger the Scrolling currently only Hides the Idle Boss and Spawns the Active Boss.

Image
My Youtube Channel: https://www.youtube.com/channel/UC-8fzX ... subscriber

My Episodes & Projects:
ImageImage


If you want to support my Projects, put this in your signature. Thanks :)
Spoiler: show

Code: Select all

[url=https://www.supermariobrosx.org/forums/viewtopic.php?f=36&t=25110[/url]
Great Folks who support/ed my projects/in the past:

Suecra
jaf
Suckerman7 GM

Image

User avatar
Hoeloe
Kamikaze Koopa
Kamikaze Koopa
Posts: 1226
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Current Project: A2XT Episode 2

Re: Need help with lua? - LunaLua General Help

Postby Hoeloe » Tue Mar 31, 2020 11:37 am

I would double check the name of the event. Also make sure your code doesn't have multiple "onEvent" functions. You can only have one function with a specific name, so if you have more than one, it won't work (though it would also give you a warning in this case).

User avatar
DrMekar
Koopa Shell
Koopa Shell
Posts: 649
Joined: Sat Apr 08, 2017 7:16 am
Flair: CUSTOM CHARACTER CREATOR
Current Project: Mario Heroes: The First Chapter Remade (MH1.5)
Chat Username: Doc
Contact:

Re: Need help with lua? - LunaLua General Help

Postby DrMekar » Tue Mar 31, 2020 11:48 am

Hoeloe wrote:
Tue Mar 31, 2020 11:37 am
I would double check the name of the event. Also make sure your code doesn't have multiple "onEvent" functions. You can only have one function with a specific name, so if you have more than one, it won't work (though it would also give you a warning in this case).
It has a second on Event Function, though it refers to two diffrent Events.

Image
My Youtube Channel: https://www.youtube.com/channel/UC-8fzX ... subscriber

My Episodes & Projects:
ImageImage


If you want to support my Projects, put this in your signature. Thanks :)
Spoiler: show

Code: Select all

[url=https://www.supermariobrosx.org/forums/viewtopic.php?f=36&t=25110[/url]
Great Folks who support/ed my projects/in the past:

Suecra
jaf
Suckerman7 GM

Image

Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 8536
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Tue Mar 31, 2020 12:04 pm

DrMekar wrote:
Tue Mar 31, 2020 11:48 am
Hoeloe wrote:
Tue Mar 31, 2020 11:37 am
I would double check the name of the event. Also make sure your code doesn't have multiple "onEvent" functions. You can only have one function with a specific name, so if you have more than one, it won't work (though it would also give you a warning in this case).
It has a second on Event Function, though it refers to two diffrent Events.

Image
Merge the two. To re-iterate on what Hoeloe said: You cannot have two functions or variables with the same name. The one defined further down in the code will always REPLACE the previously defined one.
The ABC of Creation
________________________________________________________
Image

User avatar
DrMekar
Koopa Shell
Koopa Shell
Posts: 649
Joined: Sat Apr 08, 2017 7:16 am
Flair: CUSTOM CHARACTER CREATOR
Current Project: Mario Heroes: The First Chapter Remade (MH1.5)
Chat Username: Doc
Contact:

Re: Need help with lua? - LunaLua General Help

Postby DrMekar » Tue Mar 31, 2020 12:24 pm

Enjl wrote:
Tue Mar 31, 2020 12:04 pm
DrMekar wrote:
Tue Mar 31, 2020 11:48 am
Hoeloe wrote:
Tue Mar 31, 2020 11:37 am
I would double check the name of the event. Also make sure your code doesn't have multiple "onEvent" functions. You can only have one function with a specific name, so if you have more than one, it won't work (though it would also give you a warning in this case).
It has a second on Event Function, though it refers to two diffrent Events.

Image
Merge the two. To re-iterate on what Hoeloe said: You cannot have two functions or variables with the same name. The one defined further down in the code will always REPLACE the previously defined one.
Oh, I got it to work with merging. Sorry for not writing yet. Thanks for the help :)
My Youtube Channel: https://www.youtube.com/channel/UC-8fzX ... subscriber

My Episodes & Projects:
ImageImage


If you want to support my Projects, put this in your signature. Thanks :)
Spoiler: show

Code: Select all

[url=https://www.supermariobrosx.org/forums/viewtopic.php?f=36&t=25110[/url]
Great Folks who support/ed my projects/in the past:

Suecra
jaf
Suckerman7 GM

Image

User avatar
Hoeloe
Kamikaze Koopa
Kamikaze Koopa
Posts: 1226
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Current Project: A2XT Episode 2

Re: Need help with lua? - LunaLua General Help

Postby Hoeloe » Tue Mar 31, 2020 4:36 pm

DrMekar wrote:
Tue Mar 31, 2020 12:24 pm

Oh, I got it to work with merging. Sorry for not writing yet. Thanks for the help :)
I should note: If you're asking for help like this, mentioning you had a warning showing up would have got to this point a lot faster. Don't leave our error messages or warnings, and don't just ignore them. Try reading them.

User avatar
DrMekar
Koopa Shell
Koopa Shell
Posts: 649
Joined: Sat Apr 08, 2017 7:16 am
Flair: CUSTOM CHARACTER CREATOR
Current Project: Mario Heroes: The First Chapter Remade (MH1.5)
Chat Username: Doc
Contact:

Re: Need help with lua? - LunaLua General Help

Postby DrMekar » Tue Mar 31, 2020 5:24 pm

Hoeloe wrote:
Tue Mar 31, 2020 4:36 pm
DrMekar wrote:
Tue Mar 31, 2020 12:24 pm

Oh, I got it to work with merging. Sorry for not writing yet. Thanks for the help :)
I should note: If you're asking for help like this, mentioning you had a warning showing up would have got to this point a lot faster. Don't leave our error messages or warnings, and don't just ignore them. Try reading them.
Noted for the Future 😉.
My Youtube Channel: https://www.youtube.com/channel/UC-8fzX ... subscriber

My Episodes & Projects:
ImageImage


If you want to support my Projects, put this in your signature. Thanks :)
Spoiler: show

Code: Select all

[url=https://www.supermariobrosx.org/forums/viewtopic.php?f=36&t=25110[/url]
Great Folks who support/ed my projects/in the past:

Suecra
jaf
Suckerman7 GM

Image

User avatar
Ninji2701
Bob-omb
Bob-omb
Posts: 37
Joined: Fri Mar 27, 2020 10:12 pm
Current Project: Mario's Quest

Life counter displays character name

Postby Ninji2701 » Wed Apr 15, 2020 10:19 pm

Code: Select all

function onStart()
    if player.character==CHARACTER_MARIO then
        local playerName=Graphics.loadImage("name-1.png")
    else
        if player.character==CHARACTER_LUIGI then
            local playerName=Graphics.loadImage("name-2.png")
        else
            local playerName=Graphics.loadImage("name-3.png")
        end
    end
    Graphics.sprites.hardcoded["33-3"].img=playerName
end
name-1.png
Image
name-2.png
Image
name-3.png
Image
NOTE: I don't need 2-player compatibility as my episode doesn't actively support that. Also, Peach & Link are banned characters in this episode.

Added in 5 minutes 45 seconds:
I should note that when I run this, it simply displays the 1UP text as normal, and does not display any error messages.

Added in 49 minutes 26 seconds:
I'm now using a different method to replace the 1UP graphic, and am getting this error:
Image
Here is my new code:

Code: Select all

function onStart()
    if player.character==CHARACTER_MARIO then
        local playerName=Graphics.loadImage("name-1.png")
    else
        if player.character==CHARACTER_LUIGI then
            local playerName=Graphics.loadImage("name-2.png")
        else
            local playerName=Graphics.loadImage("name-3.png")
        end
    end
    Graphics.drawImageWP(playerName,0,0)
end
Obviously the (0,0) position for the image is only a placeholder, but I need to figure out why this is an error. By the way, the name files are the same as the ones from my previous post.
<p>Does this support HTML markup?</p>
<!--It probably doesn't-->

Mʏ Pʀᴏᴊᴇᴄᴛ
Image

Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 8536
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Thu Apr 16, 2020 12:47 am

Ninji2701 wrote:
Wed Apr 15, 2020 11:14 pm

Code: Select all

function onStart()
    if player.character==CHARACTER_MARIO then
        local playerName=Graphics.loadImage("name-1.png")
    else
        if player.character==CHARACTER_LUIGI then
            local playerName=Graphics.loadImage("name-2.png")
        else
            local playerName=Graphics.loadImage("name-3.png")
        end
    end
    Graphics.sprites.hardcoded["33-3"].img=playerName
end
Im on mobile so no links but:
1. Google what the local keyword does in lua
2. Go back to replacing thehardcoded image or stop loading your image anew in every frame
The ABC of Creation
________________________________________________________
Image

User avatar
Ninji2701
Bob-omb
Bob-omb
Posts: 37
Joined: Fri Mar 27, 2020 10:12 pm
Current Project: Mario's Quest

Re: Need help with lua? - LunaLua General Help

Postby Ninji2701 » Thu Apr 16, 2020 7:20 am

Enjl wrote:
Thu Apr 16, 2020 12:47 am
1. Google what the local keyword does in lua
Fixed that, but still get the same error.
Enjl wrote:
Thu Apr 16, 2020 12:47 am
2. Go back to replacing thehardcoded image or stop loading your image anew in every frame
This can't be the case for a few reasons.
  1. I want the image to change based on what player you are playing as.I now realize that you meant replacing the hardcoded image in the lua script.
  2. When replacing the hardcoded image, the player name overlaps with the lives counter.
  3. This function is called from onStart().
  4. The error message appears before the first frame is shown.
Added in 22 minutes 45 seconds:
I JUST FIGURED OUT THE PROBLEM! The issue actually was that I wasn't defining layer priority for the image.
<p>Does this support HTML markup?</p>
<!--It probably doesn't-->

Mʏ Pʀᴏᴊᴇᴄᴛ
Image

Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 8536
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Thu Apr 16, 2020 7:44 am

Can you show your script now that the scoping issue is fixed?
The ABC of Creation
________________________________________________________
Image

User avatar
Ninji2701
Bob-omb
Bob-omb
Posts: 37
Joined: Fri Mar 27, 2020 10:12 pm
Current Project: Mario's Quest

Re: Need help with lua? - LunaLua General Help

Postby Ninji2701 » Thu Apr 16, 2020 12:45 pm

Code: Select all

function onStart()
    if player.character==CHARACTER_MARIO then
        playerName=Graphics.loadImageResolved("name-1.png")
    else
        if player.character==CHARACTER_LUIGI then
            playerName=Graphics.loadImageResolved("name-2.png")
        else
            playerName=Graphics.loadImageResolved("name-3.png")
        end
    end
end
function onDraw()
    Graphics.drawImageWP(playerName,0,0,6)
end
<p>Does this support HTML markup?</p>
<!--It probably doesn't-->

Mʏ Pʀᴏᴊᴇᴄᴛ
Image

Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 8536
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Thu Apr 16, 2020 12:52 pm

Ah, just noticed you figured it out in the previous post. That's good. Some more suggestions for this code:
1) I recommend defining the playerName variable outside of functions at the top of the code to restrict it to the code's scope. Currently, it has no scope limitation, so other scripts could potentially access it. You can just do this at the top of the code:

Code: Select all

local playerName
2) Worth noting is that this doesn't account for player character changes. Here's a little modification that accounts for character switches mid-level:

Code: Select all

local playerName = {
	[CHARACTER_MARIO] = Graphics.loadImageResolved("name-1.png"),
	[CHARACTER_LUIGI] = Graphics.loadImageResolved("name-2.png"),
	[CHARACTER_TOAD] = Graphics.loadImageResolved("name-3.png"),
}

function onDraw()
	if (playerName[player.character] ~= nil then
		Graphics.drawImageWP(playerName[player.character],0,0,6)
	end
end
This causes the onDraw code to draw the image corresponding to the player's current character, by using a lookup table (looks up the image based on the player character index as the key).
The ABC of Creation
________________________________________________________
Image

User avatar
Ninji2701
Bob-omb
Bob-omb
Posts: 37
Joined: Fri Mar 27, 2020 10:12 pm
Current Project: Mario's Quest

Re: Need help with lua? - LunaLua General Help

Postby Ninji2701 » Thu Apr 16, 2020 6:46 pm

Code: Select all

local worldPlayerDisable={}
--config
local allowedPlayers={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}--List of characters allowed in the episode. Should usually match the flags set in the 'Disable characters' field of the your world map.
--/config
local playerMask={}
for i=1,#allowedPlayers do
    playerMask[allowedPlayers[i]]=true
end
function testPlayer()
    for playerID,playerInfo in ipairs(Player.get())do
        if((not playerMask[playerInfo.character])or player.character==player2.character)then
            if playerInfo.rightKeyPressed then
                while((not playerMask[playerInfo.character])or player.character==player2.character)do
                    playerInfo.character=playerInfo.character%allowedPlayers[#allowedPlayers]+1
                end
            else
                while((not playerMask[playerInfo.character])or player.character==player2.character)do
                    playerInfo.character=(playerInfo.character-2)%allowedPlayers[#allowedPlayers]+1
                end
            end
        end
    end
end
function worldPlayerDisable.onInitAPI()
    registerEvent(worldPlayerDisable,"onInputUpdate","testPlayer")
end
return worldPlayerDisable
For some reason the testPlayer() function is never run.
<p>Does this support HTML markup?</p>
<!--It probably doesn't-->

Mʏ Pʀᴏᴊᴇᴄᴛ
Image

User avatar
Hoeloe
Kamikaze Koopa
Kamikaze Koopa
Posts: 1226
Joined: Sat Oct 03, 2015 6:18 pm
Flair: The Codehaus Girl
Current Project: A2XT Episode 2

Re: Need help with lua? - LunaLua General Help

Postby Hoeloe » Thu Apr 16, 2020 9:02 pm

When registering events, you supply a library table ("worldPlayerDisable", here), and a function name ("testPlayer"). The code will then try to run "library.function", i.e. "worldPlayerDisable.testPlayer", in your case. Your function needs to be a member function of your library table.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 6 guests