Page 41 of 76

Re: Need help with lua? - LunaLua General Help

Posted: Mon Dec 19, 2016 4:43 pm
by DeMuZ
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

Re: Need help with lua? - LunaLua General Help

Posted: Mon Dec 19, 2016 4:47 pm
by PixelPest
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)

Re: Need help with lua? - LunaLua General Help

Posted: Mon Dec 19, 2016 5:19 pm
by MarioboyFan602
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, :/

Re: Need help with lua? - LunaLua General Help

Posted: Mon Dec 19, 2016 5:24 pm
by PixelPest
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

Re: Need help with lua? - LunaLua General Help

Posted: Mon Dec 19, 2016 6:21 pm
by MarioboyFan602
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, :)

Re: Need help with lua? - LunaLua General Help

Posted: Mon Dec 19, 2016 7:10 pm
by Angelus
So what should I do? What am I doing wrong?

Re: Need help with lua? - LunaLua General Help

Posted: Tue Dec 20, 2016 1:31 am
by Emral
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.

Re: Need help with lua? - LunaLua General Help

Posted: Tue Dec 20, 2016 7:12 am
by Angelus
I've changed it to OnTick, and it worked perfectly! Thank you! Also, I've fixed the "1.14" to only "14".

Re: Need help with lua? - LunaLua General Help

Posted: Wed Dec 21, 2016 12:51 pm
by DeMuZ
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 ;-;

Re: Need help with lua? - LunaLua General Help

Posted: Wed Dec 21, 2016 12:52 pm
by Emral
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.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Dec 21, 2016 1:34 pm
by DeMuZ
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.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Dec 21, 2016 1:49 pm
by Emral
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.

Re: Need help with lua? - LunaLua General Help

Posted: Wed Dec 21, 2016 3:48 pm
by DeMuZ
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

Re: Need help with lua? - LunaLua General Help

Posted: Sat Dec 24, 2016 11:07 am
by darkhog
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?

Re: Need help with lua? - LunaLua General Help

Posted: Sat Dec 24, 2016 11:48 am
by Emral
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

Re: Need help with lua? - LunaLua General Help

Posted: Sat Dec 24, 2016 2:53 pm
by DeMuZ
Can I treat nothing as a block? If the answer is yes, then what ID should I use for it?

Re: Need help with lua? - LunaLua General Help

Posted: Sat Dec 24, 2016 3:00 pm
by Emral
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.

Re: Need help with lua? - LunaLua General Help

Posted: Sat Dec 24, 2016 3:22 pm
by DeMuZ
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

Re: Need help with lua? - LunaLua General Help

Posted: Sat Dec 24, 2016 3:25 pm
by Emral
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

Re: Need help with lua? - LunaLua General Help

Posted: Sat Dec 24, 2016 3:32 pm
by DeMuZ
Enjl wrote: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
Ok, thank you. You saved my time from being wasted! :)