Help me with an intro and cutscenes script

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
Shodax
Fighter Fly
Fighter Fly
Posts: 42
Joined: Sat Feb 05, 2022 2:24 pm
Pronouns: he

Help me with an intro and cutscenes script

Postby Shodax » Fri Apr 07, 2023 8:22 pm

Hello.
I want to put an intro and cutscenes to my project through slides that appear still images one after the other controlling for seconds the moment they change so that they tell the story. Can you help me with a script to be able to display and disappear PNG images that are triggered with Events, please? I will thank you very much

krakin
Birdo
Birdo
Posts: 2890
Joined: Sun Dec 11, 2016 8:02 pm
Flair: i wish squids and octos were real
Pronouns: he/him
Contact:

Re: Help me with an intro and cutscenes script

Postby krakin » Wed Apr 12, 2023 12:19 pm

Spoiler: show

Code: Select all

local image = Graphics.loadImage("path/to/image.png")
local image2 = Graphics.loadImage("path/to/image2.png")
local displayTime = 120 -- in frames (60 frames = 1 second)
local currentImage = image
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage" then
        frameCount = 0
        currentImage = image
    elseif eventName == "hideImage" then
        currentImage = nil
    end
end

function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == image then
                currentImage = image2
            else
                currentImage = image
            end
            frameCount = 0
        end
    end
end

function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage, 0, 0)
    end
end
This script is supposed to show an image when showImage is triggered and then hide it when hideImage is triggered. The variable for displayTime can be increased or decreased to change how long each image is shown for. Let me know if something is off.

Marioman2007
Lakitu
Lakitu
Posts: 466
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: Help me with an intro and cutscenes script

Postby Marioman2007 » Wed Apr 12, 2023 11:54 pm

krakin wrote:
Wed Apr 12, 2023 12:19 pm

Code: Select all

local displayTime = 120 -- in frames (60 frames = 1 second)

64.1 frames are equal to one second. If you want to get accurate number of ticks as an integer you can do math.floor(lunatime.toTicks(2) + 0.5) to convert 2 seconds to ticks.

Relevant docs pages:
https://docs.codehaus.moe/#/concepts/ticks
https://docs.codehaus.moe/#/reference/lunatime

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

Re: Help me with an intro and cutscenes script

Postby Hoeloe » Thu Apr 13, 2023 10:14 am

Marioman2007 wrote:
Wed Apr 12, 2023 11:54 pm
If you want to get accurate number of ticks as an integer you can do math.floor(lunatime.toTicks(2) + 0.5) to convert 2 seconds to ticks.
This is unnecessary. You can just do:

Code: Select all

lunatime.toTicks(2)
The toTicks function will already convert this to an integer.

krakin
Birdo
Birdo
Posts: 2890
Joined: Sun Dec 11, 2016 8:02 pm
Flair: i wish squids and octos were real
Pronouns: he/him
Contact:

Re: Help me with an intro and cutscenes script

Postby krakin » Thu Apr 13, 2023 11:24 am

Good to know, thanks.

Shodax
Fighter Fly
Fighter Fly
Posts: 42
Joined: Sat Feb 05, 2022 2:24 pm
Pronouns: he

Re: Help me with an intro and cutscenes script

Postby Shodax » Thu Apr 13, 2023 7:23 pm

krakin wrote:
Wed Apr 12, 2023 12:19 pm
Spoiler: show

Code: Select all

local image = Graphics.loadImage("path/to/image.png")
local image2 = Graphics.loadImage("path/to/image2.png")
local displayTime = 120 -- in frames (60 frames = 1 second)
local currentImage = image
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage" then
        frameCount = 0
        currentImage = image
    elseif eventName == "hideImage" then
        currentImage = nil
    end
end

function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == image then
                currentImage = image2
            else
                currentImage = image
            end
            frameCount = 0
        end
    end
end

function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage, 0, 0)
    end
end
This script is supposed to show an image when showImage is triggered and then hide it when hideImage is triggered. The variable for displayTime can be increased or decreased to change how long each image is shown for. Let me know if something is off.
oh! Thanks a lot!
I thought to do this with custom NPCs but thanks to your help I can make it more aesthetic. But I have a doubt: if I wanted to put more scenes (images) and put different duration to each image, would it be like this?:

Code: Select all


local image = Graphics.loadImage("path/to/image.png")
local image2 = Graphics.loadImage("path/to/image2.png")
local image3 = Graphics.loadImage("path/to/image3.png")
local image4 = Graphics.loadImage("path/to/image4.png")
local displayTime = 120 -- in frames (60 frames = 1 second)
local currentImage = image
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage" then
        frameCount = 0
        currentImage = image
    elseif eventName == "hideImage" then
        currentImage = nil
    end
end

function onEvent(eventName)
    if eventName == "showImage2" then
        frameCount = 0
        currentImage = image
    elseif eventName == "hideImage2" then
        currentImage = nil
    end
end


function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == image then
                currentImage = image2
            else
                currentImage = image
            end
            frameCount = 0
        end
    end
end


function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == image3 then
                currentImage = image4
            else
                currentImage = image3
            end
            frameCount = 0
        end
    end
end




function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage, 0, 0)
    end
end


Or what would it be like?

krakin
Birdo
Birdo
Posts: 2890
Joined: Sun Dec 11, 2016 8:02 pm
Flair: i wish squids and octos were real
Pronouns: he/him
Contact:

Re: Help me with an intro and cutscenes script

Postby krakin » Fri Apr 14, 2023 4:37 pm

Shodax wrote:
Thu Apr 13, 2023 7:23 pm
krakin wrote:
Wed Apr 12, 2023 12:19 pm
Spoiler: show

Code: Select all

local image = Graphics.loadImage("path/to/image.png")
local image2 = Graphics.loadImage("path/to/image2.png")
local displayTime = 120 -- in frames (60 frames = 1 second)
local currentImage = image
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage" then
        frameCount = 0
        currentImage = image
    elseif eventName == "hideImage" then
        currentImage = nil
    end
end

function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == image then
                currentImage = image2
            else
                currentImage = image
            end
            frameCount = 0
        end
    end
end

function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage, 0, 0)
    end
end
This script is supposed to show an image when showImage is triggered and then hide it when hideImage is triggered. The variable for displayTime can be increased or decreased to change how long each image is shown for. Let me know if something is off.
oh! Thanks a lot!
I thought to do this with custom NPCs but thanks to your help I can make it more aesthetic. But I have a doubt: if I wanted to put more scenes (images) and put different duration to each image, would it be like this?:

Code: Select all


local image = Graphics.loadImage("path/to/image.png")
local image2 = Graphics.loadImage("path/to/image2.png")
local image3 = Graphics.loadImage("path/to/image3.png")
local image4 = Graphics.loadImage("path/to/image4.png")
local displayTime = 120 -- in frames (60 frames = 1 second)
local currentImage = image
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage" then
        frameCount = 0
        currentImage = image
    elseif eventName == "hideImage" then
        currentImage = nil
    end
end

function onEvent(eventName)
    if eventName == "showImage2" then
        frameCount = 0
        currentImage = image
    elseif eventName == "hideImage2" then
        currentImage = nil
    end
end


function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == image then
                currentImage = image2
            else
                currentImage = image
            end
            frameCount = 0
        end
    end
end


function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == image3 then
                currentImage = image4
            else
                currentImage = image3
            end
            frameCount = 0
        end
    end
end




function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage, 0, 0)
    end
end


Or what would it be like?
Thanks SpencerlyEverly and KBM-Quine for helping me fix this code up.
image.path has multiple values so only the last one will be triggered. You have to number them in square brackers like this: images[1].path, not like this: images.path
You can also call the loadImage where you're specifying the path, that way the images will load before the level does and it'll be more efficient.

Code: Select all

local images = {
    [1] = {image = Graphics.loadImage("path/to/image.png"), duration = 120}, -- in frames (60 frames = 1 second)
    [2] = {image = Graphics.loadImage("path/to/image2.png"), duration = 180},
    [3] = {image = Graphics.loadImage("path/to/image3.png"), duration = 240},
    [4] = {image = Graphics.loadImage("path/to/image4.png"), duration = 300}
}

Shodax
Fighter Fly
Fighter Fly
Posts: 42
Joined: Sat Feb 05, 2022 2:24 pm
Pronouns: he

Re: Help me with an intro and cutscenes script

Postby Shodax » Mon Apr 17, 2023 10:43 pm

Sorry but I'm very new to programming.
I tried to call the image with your number in each condition but I get an error.
What am I doing wrong?

Image

Code: Select all

local images = {
    [1] = {image = Graphics.loadImage("image.png"), duration = 120}, -- in frames (60 frames = 1 second)
    [2] = {image = Graphics.loadImage("image2.png"), duration = 180},
    [3] = {image = Graphics.loadImage("image3.png"), duration = 240},
    [4] = {image = Graphics.loadImage("image4.png"), duration = 300}
}
local currentImage = images
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage" then
        frameCount = 0
        currentImage = images
    elseif eventName == "hideImage" then
        currentImage = nil
    end
end

function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= displayTime then
            if currentImage == 1 then
                currentImage = 2
            else
                currentImage = 3
            end
            frameCount = 0
        end
    end
end

function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage, 0, 0)
    end
end

Shodax
Fighter Fly
Fighter Fly
Posts: 42
Joined: Sat Feb 05, 2022 2:24 pm
Pronouns: he

Re: Help me with an intro and cutscenes script

Postby Shodax » Sun Apr 23, 2023 11:37 pm

use the GPT Chat to know where I had the error and I could solve it.
I leave the code in case it serves someone else:

Code: Select all



local images = {
    [1] = {image = Graphics.loadImage("image.png"), duration = 20},
    [2] = {image = Graphics.loadImage("image2.png"), duration = 20},
    [3] = {image = Graphics.loadImage("image3.png"), duration = 20},
    [4] = {image = Graphics.loadImage("image4.png"), duration = 20}
}
local currentImage = images[1]
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage1" then
        frameCount = 0
        currentImage = images[1]
    elseif eventName == "hideImage1" then
        currentImage = nil
    end
    if eventName == "showImage2" then
        frameCount = 0
        currentImage = images[2]
    elseif eventName == "hideImage2" then
        currentImage = nil
    end
    if eventName == "showImage3" then
        frameCount = 0
        currentImage = images[3]
    elseif eventName == "hideImage3" then
        currentImage = nil
    end
end


function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= currentImage.duration then
            if currentImage == images[4] then
                currentImage = images[1]
          
            end
            frameCount = 0
        end
    end
end

function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage.image, 0, 0)
    end
end


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

Re: Help me with an intro and cutscenes script

Postby Hoeloe » Mon Apr 24, 2023 8:48 am

Shodax wrote:
Sun Apr 23, 2023 11:37 pm
use the GPT Chat to know where I had the error and I could solve it.
Don't use ChatGPT to try and code. It's basically a glorified predictive text, it has no way to verify that what it tells you is correct, and it's quite likely to just confidently spout nonsense - for code or anything else.

Shodax
Fighter Fly
Fighter Fly
Posts: 42
Joined: Sat Feb 05, 2022 2:24 pm
Pronouns: he

Re: Help me with an intro and cutscenes script

Postby Shodax » Mon Apr 24, 2023 12:50 pm

Hoeloe wrote:
Mon Apr 24, 2023 8:48 am
Shodax wrote:
Sun Apr 23, 2023 11:37 pm
use the GPT Chat to know where I had the error and I could solve it.
Don't use ChatGPT to try and code. It's basically a glorified predictive text, it has no way to verify that what it tells you is correct, and it's quite likely to just confidently spout nonsense - for code or anything else.
The gpt chat I only use as a plugin not for it to do all the work. In fact the code he gave me was this:

Code: Select all

local images = {
    [1] = {image = Graphics.loadImage("image.png"), duration = 120},
    [2] = {image = Graphics.loadImage("image2.png"), duration = 180},
    [3] = {image = Graphics.loadImage("image3.png"), duration = 240},
    [4] = {image = Graphics.loadImage("image4.png"), duration = 300}
}
local currentImage = images[1]
local frameCount = 0

function onEvent(eventName)
    if eventName == "showImage" then
        frameCount = 0
        currentImage = images[1]
    elseif eventName == "hideImage" then
        currentImage = nil
    end
end

function onTick()
    if currentImage then
        frameCount = frameCount + 1
        if frameCount >= currentImage.duration then
            if currentImage == images[4] then
                currentImage = images[1]
            else
                currentImage = images[currentImage.index + 1]
            end
            frameCount = 0
        end
    end
end

function onDraw()
    if currentImage then
        Graphics.drawImage(currentImage.image, 0, 0)
    end
end
as time has error in line 26. But I was able to correct it and now it works as I expected.
The GPT Chat is not perfect, nor does it do all the work but it helps for those who do not know much about programming

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

Re: Help me with an intro and cutscenes script

Postby Hoeloe » Wed Apr 26, 2023 12:59 pm

Shodax wrote:
Mon Apr 24, 2023 12:50 pm
it helps for those who do not know much about programming
It really doesn't. It's just as likely to give you something confidently wrong than something actually useful. Like I said, it's effectively just a glorified predictive text - it's just browsing the internet and pasting text that is likely to show up following your queries - it's not actually thinking in any meaningful sense.

You'd be far better off using StackOverflow for help with programming, which is basically a large programming answer board with a ton of useful questions and answers, and a huge backlog of information.

Shodax
Fighter Fly
Fighter Fly
Posts: 42
Joined: Sat Feb 05, 2022 2:24 pm
Pronouns: he

Re: Help me with an intro and cutscenes script

Postby Shodax » Tue May 02, 2023 11:34 pm

Hoeloe wrote:
Wed Apr 26, 2023 12:59 pm
Shodax wrote:
Mon Apr 24, 2023 12:50 pm
it helps for those who do not know much about programming
It really doesn't. It's just as likely to give you something confidently wrong than something actually useful. Like I said, it's effectively just a glorified predictive text - it's just browsing the internet and pasting text that is likely to show up following your queries - it's not actually thinking in any meaningful sense.

You'd be far better off using StackOverflow for help with programming, which is basically a large programming answer board with a ton of useful questions and answers, and a huge backlog of information.
And what do you suggest to learn? Where should I start to learn Lua for SMBX2? I'm learning to program in C#, will it help me?

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

Re: Help me with an intro and cutscenes script

Postby Hoeloe » Sat May 06, 2023 7:28 pm

C# would help a little, as a lot of the concepts are shared, but also general Lua tutorials are pretty easy to find, it's a common language!


Return to “LunaLua Help”

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari