Need help with lua? - LunaLua General Help

This is the place for discussion and support for LunaLua and related modifications and libraries.
Forum rules
Before you make a topic/post, consider the following:
-Is there a topic for this already?
-Is your post on topic/appropriate?
-Are you posting in the right forum/following the forum rules?
User avatar
Angelus
Bit
Bit
Posts: 129
Joined: Tue Jun 21, 2016 9:38 am

Re: Need help with lua? - LunaLua General Help

Postby Angelus » Mon Dec 19, 2016 11:48 am

Hello there, I've been trying to use LunaLua for custom music and while it works pretty well for most of the time, I'm facing a little problem here:
I know how to set the music to play into a specific event, but I would like to know how to make it stop after a posterior event, replacing it by silence. The problem is, all my attempts have failed since the music just start over again when I expected it to stop playing. Here's the code I have so far. Am I doing something wrong?
Spoiler: show
play = false

function onLoop()
if play then
Audio.SeizeStream(1.14)
Audio.MusicOpen("1 - 1 Boss.ogg")
Audio.MusicPlay()
end
end

function onEvent(eventname)
if eventname == "Cutscene 4 - 15" then
play = true
else
if eventname == "Cutscene 5 - 1" then
play = false
Audio.ReleaseStream(1.14)
end
end

User avatar
DeMuZ
Bob-omb
Bob-omb
Posts: 37
Joined: Thu Aug 18, 2016 2:35 pm
Current Project: The Episode of Nonsense series

Re: Need help with lua? - LunaLua General Help

Postby DeMuZ » Mon Dec 19, 2016 4:43 pm

Kevsoft wrote:Well a simple way to draw the right frame, is to make a local variable of the current frame you want to draw:

Code: Select all


local frameNum_ssj0 = 1
-- <code>

-- somewhere for rendering
Graphics.draw(ssj0[frameNum_ssj0], X1, Y1, 5)
frameNum_ssj0 = frameNum_ssj0 + 1
if frameNum_ssj0 > #ssj0 then
    frameNum_ssj0 = 1
end
I hope it makes somewhat sense for you
My code still deosn't work for any of draw functions. I get this error message: http://imgur.com/EkGf7At

User avatar
PixelPest
Link
Link
Posts: 7118
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Current Project: Boom Boom Evolution
Chat Username: PixelPest
Contact:

Re: Need help with lua? - LunaLua General Help

Postby PixelPest » Mon Dec 19, 2016 4:47 pm

Check line 179 and make sure that you have not only the correct number of values for Graphics.drawImageToSceneWP() but also the correct types (within this, make sure that none of the variables you're using as arguments are nil)

User avatar
MarioboyFan602
Swooper
Swooper
Posts: 101
Joined: Thu Dec 03, 2015 5:52 pm
Current Project: Super Mario Bros Remake
Chat Username: MarioboyFan602
Contact:

Re: Need help with lua? - LunaLua General Help

Postby MarioboyFan602 » Mon Dec 19, 2016 5:19 pm

Angelus wrote:Hello there, I've been trying to use LunaLua for custom music and while it works pretty well for most of the time, I'm facing a little problem here:
I know how to set the music to play into a specific event, but I would like to know how to make it stop after a posterior event, replacing it by silence. The problem is, all my attempts have failed since the music just start over again when I expected it to stop playing. Here's the code I have so far. Am I doing something wrong?
Spoiler: show
play = false

function onLoop()
if play then
Audio.SeizeStream(1.14)
Audio.MusicOpen("1 - 1 Boss.ogg")
Audio.MusicPlay()
end
end

function onEvent(eventname)
if eventname == "Cutscene 4 - 15" then
play = true
else
if eventname == "Cutscene 5 - 1" then
play = false
Audio.ReleaseStream(1.14)
end
end
well... im no expert at LunaLUA, but have you tried moving the else to after the Audio.ReleaseStream(1.14)? just asking, :/
-----Current Projects-----
Spoiler: show
Image
I Try my Best and be as Nice as Possible to Anyone :D

User avatar
PixelPest
Link
Link
Posts: 7118
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Current Project: Boom Boom Evolution
Chat Username: PixelPest
Contact:

Re: Need help with lua? - LunaLua General Help

Postby PixelPest » Mon Dec 19, 2016 5:24 pm

That wouldn't work. If anything it should be "elseif" instead of "else" and "if" but that shouldn't change its state of working or not working

User avatar
MarioboyFan602
Swooper
Swooper
Posts: 101
Joined: Thu Dec 03, 2015 5:52 pm
Current Project: Super Mario Bros Remake
Chat Username: MarioboyFan602
Contact:

Re: Need help with lua? - LunaLua General Help

Postby MarioboyFan602 » Mon Dec 19, 2016 6:21 pm

PixelPest wrote:That wouldn't work. If anything it should be "elseif" instead of "else" and "if" but that shouldn't change its state of working or not working
didn't you read the "dont know much about LunaLUA" part? and thanks for lettin me know, :)
-----Current Projects-----
Spoiler: show
Image
I Try my Best and be as Nice as Possible to Anyone :D

User avatar
Angelus
Bit
Bit
Posts: 129
Joined: Tue Jun 21, 2016 9:38 am

Re: Need help with lua? - LunaLua General Help

Postby Angelus » Mon Dec 19, 2016 7:10 pm

So what should I do? What am I doing wrong?

User avatar
Enjl
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 7704
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 Dec 20, 2016 1:31 am

The argument for SeizeStream is the section number (from 0 to 20). No idea what you're doing with that 1.14 tbh.
Besides, you're starting the music loop every tick. In onLoop, out of all things.
1) onTick is objectively superior to onLoop unless you're doing arcane sorceries.
2) you only need to initiate music stream once, meaning everything can be handled in onEvent.
3) You also don't NEED an "else" or "elseif" in onEvent. The eventname of the called event isn't gonna change mid-loop unless you're actively changing it.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
Angelus
Bit
Bit
Posts: 129
Joined: Tue Jun 21, 2016 9:38 am

Re: Need help with lua? - LunaLua General Help

Postby Angelus » Tue Dec 20, 2016 7:12 am

I've changed it to OnTick, and it worked perfectly! Thank you! Also, I've fixed the "1.14" to only "14".

User avatar
DeMuZ
Bob-omb
Bob-omb
Posts: 37
Joined: Thu Aug 18, 2016 2:35 pm
Current Project: The Episode of Nonsense series

Re: Need help with lua? - LunaLua General Help

Postby DeMuZ » Wed Dec 21, 2016 12:51 pm

I'm tired of trying to make that f***ing animations work... Here are the graphics I wanted to use: http://imgur.com/a/fSxiV
Please, just make them work with Graphics.drawImageToSceneWP() help ;-;

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

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Wed Dec 21, 2016 12:52 pm

Thing is, 99% of people use spritesheets rather than animated files caue they're easier to make and easier to edit. Loading the animated file I think converts it into seperate images, too, which kinda follows the same idea.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
DeMuZ
Bob-omb
Bob-omb
Posts: 37
Joined: Thu Aug 18, 2016 2:35 pm
Current Project: The Episode of Nonsense series

Re: Need help with lua? - LunaLua General Help

Postby DeMuZ » Wed Dec 21, 2016 1:34 pm

Enjl wrote:Thing is, 99% of people use spritesheets rather than animated files caue they're easier to make and easier to edit. Loading the animated file I think converts it into seperate images, too, which kinda follows the same idea.
So the best option if the animations doesn't work is to remake the graphics into seperate files for each frame? If there isn't any other options... I'll try it.

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

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Wed Dec 21, 2016 1:49 pm

DeMuZ wrote:
Enjl wrote:Thing is, 99% of people use spritesheets rather than animated files caue they're easier to make and easier to edit. Loading the animated file I think converts it into seperate images, too, which kinda follows the same idea.
So the best option if the animations doesn't work is to remake the graphics into seperate files for each frame? If there isn't any other options... I'll try it.
No. Take a look at how SMBX itself organises its spritesheets.
Image
Given this is a custom sprite, it can be displayed like this:

Code: Select all

local img = Graphics.loadImage("img.png")

function onDraw()
	Graphics.drawImageToSceneWP(img, x, y, xOffset, yOffset, width, height, priority)
end
xOffset and yOffset are the top left coordinates of the displayed frame of the spritesheet.
width and height are the dimensions for the drawn frame.
There are 8 frames on this sheet, of 64x64 pixels in size. The xOffset for the frames never changes, either, so we can put 0 for that:

Code: Select all

function onDraw()
	Graphics.drawImageToSceneWP(img, x, y, 0, yOffset, 64, 64, priority)
end
yOffset is a variable which changes depending on the frame you want to display. The most rudimental way to do an 8 frame animation is just to have a timer run through:
local t = 0

Code: Select all

function onDraw()
	t = (t + 1)%8 --%8 makes sure we cycle from 0 to 7.
	Graphics.drawImageToSceneWP(img, x, y, 0, 64 * t, 64, 64, priority)
end
This makes the frame switch every frame, but to circumvent that you can easily slow down the timer and floor it.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
DeMuZ
Bob-omb
Bob-omb
Posts: 37
Joined: Thu Aug 18, 2016 2:35 pm
Current Project: The Episode of Nonsense series

Re: Need help with lua? - LunaLua General Help

Postby DeMuZ » Wed Dec 21, 2016 3:48 pm

Enjl wrote:
DeMuZ wrote:
Enjl wrote:Thing is, 99% of people use spritesheets rather than animated files caue they're easier to make and easier to edit. Loading the animated file I think converts it into seperate images, too, which kinda follows the same idea.
So the best option if the animations doesn't work is to remake the graphics into seperate files for each frame? If there isn't any other options... I'll try it.
No. Take a look at how SMBX itself organises its spritesheets.
Image
Given this is a custom sprite, it can be displayed like this:

Code: Select all

local img = Graphics.loadImage("img.png")

function onDraw()
	Graphics.drawImageToSceneWP(img, x, y, xOffset, yOffset, width, height, priority)
end
xOffset and yOffset are the top left coordinates of the displayed frame of the spritesheet.
width and height are the dimensions for the drawn frame.
There are 8 frames on this sheet, of 64x64 pixels in size. The xOffset for the frames never changes, either, so we can put 0 for that:

Code: Select all

function onDraw()
	Graphics.drawImageToSceneWP(img, x, y, 0, yOffset, 64, 64, priority)
end
yOffset is a variable which changes depending on the frame you want to display. The most rudimental way to do an 8 frame animation is just to have a timer run through:
local t = 0

Code: Select all

function onDraw()
	t = (t + 1)%8 --%8 makes sure we cycle from 0 to 7.
	Graphics.drawImageToSceneWP(img, x, y, 0, 64 * t, 64, 64, priority)
end
This makes the frame switch every frame, but to circumvent that you can easily slow down the timer and floor it.
Enjl and the other people who helped me, thank you very much! :D

User avatar
darkhog
Rex
Rex
Posts: 87
Joined: Sat Dec 10, 2016 11:17 pm
Current Project: something grand...
Chat Username: Darkhog

Re: Need help with lua? - LunaLua General Help

Postby darkhog » Sat Dec 24, 2016 11:07 am

Hello. Currently trying to put smb1-like "walking to a pipe before level starts" (as seen in 1-2 and any underground/underwater levels in SMB1). Managed to do this fine, but now I have a problem: The player simply walks to fast there, want to make him slower for that specific section. So I've made this quick code to halve the speed of player when section starts and restore it by multiplying it back when main level starts:

Code: Select all

function onLoadSection0()
	player.speedX=player.speedX*2
end
function onLoadSection1()
	player.speedX=player.speedX/2
end
But it doesn't work for some reason. Any ideas?

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

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Sat Dec 24, 2016 11:48 am

it sets the speed for one frame. Instead you want to lock the player into holding right and nothing else while in section 1:

Code: Select all

local inputs2 = API.load("inputs2") --makes locking controls easier

function onStart()
	inputs2.locked[1].all = true --1 is player 1
end

function onInputUpdate()
	if player.section == 0 then
		player.rightKeyPressing = true
	end
end

function onLoadSection1() --upon loading the next section
	inputs2.locked[1].all = false --unlock
end
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
DeMuZ
Bob-omb
Bob-omb
Posts: 37
Joined: Thu Aug 18, 2016 2:35 pm
Current Project: The Episode of Nonsense series

Re: Need help with lua? - LunaLua General Help

Postby DeMuZ » Sat Dec 24, 2016 2:53 pm

Can I treat nothing as a block? If the answer is yes, then what ID should I use for it?

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

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Sat Dec 24, 2016 3:00 pm

What exactly are you asking? Nothing is nothing and is there when you don't tell anything else to be there. Nothing doesn't need to be placed.
ImageImageImageImage
ImageImageImageImageImageImage

User avatar
DeMuZ
Bob-omb
Bob-omb
Posts: 37
Joined: Thu Aug 18, 2016 2:35 pm
Current Project: The Episode of Nonsense series

Re: Need help with lua? - LunaLua General Help

Postby DeMuZ » Sat Dec 24, 2016 3:22 pm

Enjl wrote:What exactly are you asking? Nothing is nothing and is there when you don't tell anything else to be there. Nothing doesn't need to be placed.
I just want to make sth spawn randomly in empty space of this section and I'm trying to find the simpliest way to do this xd
Image

I thought I could use this:

Code: Select all

for k,v in pairs(Block.get()) do
--if it is nothing then spawn sth
end
This is the reason why I asked that question :d

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

Re: Need help with lua? - LunaLua General Help

Postby Enjl » Sat Dec 24, 2016 3:25 pm

That won't work. Instead, pick spawn coordinates and after that do a getIntersecting check around that box and spawn the NPC only if the returned list is empty:

local spawnX, spawnY = rng.random(a,b), rng.random(c,d)
local collidingBlocks = Block.getIntersecting(spawnX - 16, spawnY - 16, spawnX + 16, spawnY + 16)
if #collidingBlocks == 0 then
local spawnedNPC = NPC.spawn(id, spawnX, spawnY, section, false, true)
end
ImageImageImageImage
ImageImageImageImageImageImage


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 3 guests