Need help with lua? - LunaLua General Help

Post here for help and support regarding LunaLua and SMBX2's libraries and features.

Moderator: Userbase Moderators

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9890
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Wed Feb 10, 2021 4:46 am

t[player.character] returns the object at that index, so in your case { obj1 = Graphics.loadImage(Misc.resolveFile("modernhud-2.png")) } if the player is mario, and nil in every other scenario.
So you can access obj1 as part of t[player.character] if the player is mario.

Marioman2007
2025 Egg Hunter
2025 Egg Hunter
Posts: 544
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Wed Feb 10, 2021 4:50 am

Enjl wrote:
Wed Feb 10, 2021 4:46 am
nil in every other scenario.
So you can access obj1 as part of t[player.character] if the player is mario.
What are the other scenarios.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9890
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Wed Feb 10, 2021 6:59 am

marioman2007 wrote:
Wed Feb 10, 2021 4:50 am
Enjl wrote:
Wed Feb 10, 2021 4:46 am
nil in every other scenario.
So you can access obj1 as part of t[player.character] if the player is mario.
What are the other scenarios.
If the player is not mario. Because you only have one table entry for when player.character is mario (CHARACTER_MARIO).

Marioman2007
2025 Egg Hunter
2025 Egg Hunter
Posts: 544
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Wed Feb 10, 2021 7:24 am

Still can't understand :(
Last edited by Marioman2007 on Fri Feb 12, 2021 3:56 am, edited 1 time in total.

Marioman2007
2025 Egg Hunter
2025 Egg Hunter
Posts: 544
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: Need help with lua? - LunaLua General Help

Postby Marioman2007 » Thu Feb 11, 2021 5:50 am

Hey! Enjil, this issue is taking lots of pages, so let's take it to here.

BlueStaggo
Koopa
Koopa
Posts: 18
Joined: Sat Feb 20, 2021 8:18 am
Pronouns: he/him
Contact:

Re: Need help with lua? - LunaLua General Help

Postby BlueStaggo » Sat Feb 20, 2021 8:35 am

I'm using SMBX 2.0.0b4 and I want to make it so, when I touch the goal tape (npc-197), everything goes dark, like in Super Mario World. I tried attaching a "Level - End" event to the tape's death and run this code when the event happens:

Code: Select all

function onEvent(eventName)
    if eventName == "Level - End" then
        Darkness.create{
            falloff = Darkness.falloff.HARD,
            shadows = Darkness.shadows.NONE,
            bounds = nil,
            section = -1,
            ambient = 0x000000
        }
    end
end
But it won't make the screen go dark. I've also tried using the onNPCKill event but that didn't work either. How can I make it dark when I finish the level?

Hoeloe
Phanto
Phanto
Posts: 1465
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 Feb 21, 2021 6:13 am

First thing you want to do is hit F2 and make sure your script is actually running.

BlueStaggo
Koopa
Koopa
Posts: 18
Joined: Sat Feb 20, 2021 8:18 am
Pronouns: he/him
Contact:

Re: Need help with lua? - LunaLua General Help

Postby BlueStaggo » Sun Feb 21, 2021 9:43 am

Hoeloe wrote:
Sun Feb 21, 2021 6:13 am
First thing you want to do is hit F2 and make sure your script is actually running.
I've made sure that my script was running. It had other things in it like moving layers in a sine wave. Maybe I can't run code while the end sequence is running?

Hoeloe
Phanto
Phanto
Posts: 1465
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 Feb 21, 2021 8:13 pm

There's no reason why not. Have you checked that your event is triggering correctly?

MrDoubleA
Edward
Edward
Posts: 394
Joined: Mon Aug 20, 2018 7:02 am
Flair: How much munchers?

Re: Need help with lua? - LunaLua General Help

Postby MrDoubleA » Mon Feb 22, 2021 1:23 pm

BlueStag wrote:
Sat Feb 20, 2021 8:35 am
I'm using SMBX 2.0.0b4 and I want to make it so, when I touch the goal tape (npc-197), everything goes dark, like in Super Mario World. I tried attaching a "Level - End" event to the tape's death and run this code when the event happens:

Code: Select all

function onEvent(eventName)
    if eventName == "Level - End" then
        Darkness.create{
            falloff = Darkness.falloff.HARD,
            shadows = Darkness.shadows.NONE,
            bounds = nil,
            section = -1,
            ambient = 0x000000
        }
    end
end
But it won't make the screen go dark. I've also tried using the onNPCKill event but that didn't work either. How can I make it dark when I finish the level?
I've already made a more accurate goal tape as a part of my NPC pack (;

Suckerman7 GM
Eerie
Eerie
Posts: 716
Joined: Fri Jun 19, 2015 9:42 am
Flair: Multiversal Traveller/Prismverse's God of Creation
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Suckerman7 GM » Wed Feb 24, 2021 4:39 am

I need help about a code I'm gonna use for my project.

So, I'm trying to make a sound effect play whenever I trigger an achievement with the activation of an event. It works with one event, like the code is shown below:

Code: Select all

function onEvent(eventname)
    if eventname == "King Bob-omb Defeated" then    
        Audio.playSFX("achievement.ogg")
    end
end
What I want to do is to trigger this code with more than one event. Is there a way to expand this code by adding multiple events into the eventname variable, or do I have to duplicate this code multiple times for each individual event?

MrDoubleA
Edward
Edward
Posts: 394
Joined: Mon Aug 20, 2018 7:02 am
Flair: How much munchers?

Re: Need help with lua? - LunaLua General Help

Postby MrDoubleA » Wed Feb 24, 2021 5:52 am

Suckerman7 GM wrote:
Wed Feb 24, 2021 4:39 am
I need help about a code I'm gonna use for my project.

So, I'm trying to make a sound effect play whenever I trigger an achievement with the activation of an event. It works with one event, like the code is shown below:

Code: Select all

function onEvent(eventname)
    if eventname == "King Bob-omb Defeated" then    
        Audio.playSFX("achievement.ogg")
    end
end
What I want to do is to trigger this code with more than one event. Is there a way to expand this code by adding multiple events into the eventname variable, or do I have to duplicate this code multiple times for each individual event?
You can do

Code: Select all

if eventname == "King Bob-omb Defeated" or eventname == "my other event" or eventname == "or even a third event!" then

Suckerman7 GM
Eerie
Eerie
Posts: 716
Joined: Fri Jun 19, 2015 9:42 am
Flair: Multiversal Traveller/Prismverse's God of Creation
Pronouns: he/him

Re: Need help with lua? - LunaLua General Help

Postby Suckerman7 GM » Wed Feb 24, 2021 8:57 am

MrDoubleA wrote:
Wed Feb 24, 2021 5:52 am
Suckerman7 GM wrote:
Wed Feb 24, 2021 4:39 am
I need help about a code I'm gonna use for my project.

So, I'm trying to make a sound effect play whenever I trigger an achievement with the activation of an event. It works with one event, like the code is shown below:

Code: Select all

function onEvent(eventname)
    if eventname == "King Bob-omb Defeated" then    
        Audio.playSFX("achievement.ogg")
    end
end
What I want to do is to trigger this code with more than one event. Is there a way to expand this code by adding multiple events into the eventname variable, or do I have to duplicate this code multiple times for each individual event?
You can do

Code: Select all

if eventname == "King Bob-omb Defeated" or eventname == "my other event" or eventname == "or even a third event!" then
Thanks!

AAguy11
Bob-Omb
Bob-Omb
Posts: 20
Joined: Sun May 13, 2018 9:26 am

Re: Need help with lua? - LunaLua General Help

Postby AAguy11 » Sat Feb 27, 2021 5:47 am

I think this may be a problem with SMBX and not my code (I'll share it anyway), but it seems like SaveData isn't saving data. I'm trying to save an integer to SaveData indexed by the filename. The number is increased by 1 everytime a player hits a block, which triggers an event.
The code runs just fine, but the data never saves, it always resets back to 0. I also tested to make sure it was the saving that was the problem, and sure enough, it was.

Here's the code:

Code: Select all

SaveData[Level.filename()] = SaveData[Level.filename()] or {}
local hits = SaveData[Level.filename()].BlockHits
hits = hits or 0


function updateSign()
	for _, sign in ipairs(NPC.get(151, player.section)) do
		sign.msg = "Times you have hit the infinite block: "..tostring(hits)
    end
end


function onStart()
	Misc.dialog(SaveData[Level.filename()].BlockHits) -- ?? Always 0
	Misc.dialog(hits) -- ?? Always 0
	updateSign()
end

function onEvent(eventName)
    if eventName == "Restore" then
		hits = hits + 1
		updateSign()
    end
end

Hoeloe
Phanto
Phanto
Posts: 1465
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 » Sat Feb 27, 2021 11:36 am

You never update the value in SaveData. When you use the variable "hits" you're copying the value, not a reference to the SaveData location. The "hits" variable is only in your lua file, and has no "memory" of where it gets its values from. When you assign "SaveData[Level.filename()].BlockHits" to it, all you're doing is copying that value over, and when you then update "blockhits", you're just updating "blockhits", not the SaveData value, since "blockhits" doesn't keep track of where its data comes from.

Note that this is NOT the case if you're copying a table over. Tables are stored by reference, which means they aren't "copied" wholesale, but a reference to them is. Numbers and other values, however, are not copied by reference. So, for example:

Code: Select all

local v = SaveData.myValue
v = v or 0
v = v+1
This will NOT update the value in SaveData.

Code: Select all

SaveData.myTable = SaveData.myTable or {}
local t = SaveData.myTable
t.x = 4
This WILL update the value in SaveData. (Note that the "or {}" is done when assigning to SaveData directly - that part is important.)

DB12
Bob-Omb
Bob-Omb
Posts: 20
Joined: Sat Feb 27, 2021 10:19 am

Re: Need help with lua? - LunaLua General Help

Postby DB12 » Sat Feb 27, 2021 8:57 pm

When i use handycam.lua, the hud is shrunken and when i drop a powerup it drops in the wrong area

This is the code i used

Code: Select all

local handycam = require("handycam")
handycam[1].zoom = 1.5625
handycam[2].zoom = 1.5625
Also im new, so if this is the wrong place to put this i apologize

Hoeloe
Phanto
Phanto
Posts: 1465
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 Feb 28, 2021 8:35 am

What do you mean "the hud is shrunken"? The hud shouldn't be affected by handycam at all. As for reserve powerups - yes those will drop where the base game expects them to. You can fix that with some lua, however.

AAguy11
Bob-Omb
Bob-Omb
Posts: 20
Joined: Sun May 13, 2018 9:26 am

Re: Need help with lua? - LunaLua General Help

Postby AAguy11 » Sun Feb 28, 2021 8:53 am

Hoeloe wrote:
Sat Feb 27, 2021 11:36 am
You never update the value in SaveData. When you use the variable "hits" you're copying the value, not a reference to the SaveData location. The "hits" variable is only in your lua file, and has no "memory" of where it gets its values from. When you assign "SaveData[Level.filename()].BlockHits" to it, all you're doing is copying that value over, and when you then update "blockhits", you're just updating "blockhits", not the SaveData value, since "blockhits" doesn't keep track of where its data comes from.
Ah okay, thanks for explaining it, this kind of stuff, "Does it copy the value or is it referencing that thing?" confuses me. Fixed it and got the saving working.

Hoeloe
Phanto
Phanto
Posts: 1465
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 Feb 28, 2021 9:21 am

It's in part to do with how complicated the data is. Basically, everything that is a table (i.e. any object that can have "fields" or "indices") is far too complex to be just copying around, so instead of storing the object directly, the program makes one copy of it, and then stores the location of that object in the variable. That location is then treated like a normal variable and copied around wherever needed. But, because it's just the location of the object, any changes made to the object itself are reflected in all of the variables storing it, because it changes the object at that location.

I like to think of it like cardboard boxes. Each variable is a box, and inside the box is a piece of paper with a value written on it. For numbers, strings, booleans, etc. when you write something like local v = myVar, you can picture that as picking up a new box, labelling it "v", then finding the box named "myVar", opening it, copying down what's written on the paper inside, and placing that copy in the new box. Now, "v" and "myVar" have the same value, but if something then changes what's written in myVar it's not going to affect what's written in v and vice versa.

For tables, what's written on the paper is more like an address, so if you do something like myTable.x = 4, you first open the box labelled "myTable", then read the address written on the paper in there, follow the address to a new room. This room is itself filled with its own set of boxes. You then find the box in that room labelled "x" (or make a new one if you can't find it), write "4" on a piece of paper, and put it in that box.

This is a little awkward to explain, but if you can get your head around the metaphor it might help explain a few concepts to you.

Goldenemerl64
Fighter Fly
Fighter Fly
Posts: 38
Joined: Tue Jul 07, 2020 3:50 pm

Re: Need help with lua? - LunaLua General Help

Postby Goldenemerl64 » Tue Mar 02, 2021 6:59 am

I'm having problems with Activating auto-scroll on a Collectable Trigger.

Code: Select all

local autoscroll = require("autoscroll") 
function onEvent(Fast)
    if eventName == "Fast" then 
    autoscroll.scrollRight(3.0)
    elseif eventName == "Fast" then
    autoscroll.scrollRight(3.0)
 
    end
end
am I doing it correctly?


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari