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?
Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9707
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat Dec 01, 2018 1:40 pm

8lue Storm wrote:
Sat Dec 01, 2018 12:49 pm
Dog Gord Cat wrote:
Sat Dec 01, 2018 12:25 pm
How would I make it so a block disappears when it gets touched by a player? I'm new to lunalua : (
You can detect player collision with a block through the function Block.collidesWith(player), if it's 0, there's no collision, 1 means the player is on top of the block, 2 means in the right, 3 means bottom and 4 means in the left. To "get" the block, you can do a for loop with Block.get.

Code: Select all

function onTick()
	for _,banana in ipairs(Block.get(--[id of the block])) do
		if banana:collidesWith(player)~= 0 then
			banana:remove(true)
		end
	end
end
You can name "banana" anything, as long as you write the block id instead of "--[id of the block]". The if condition that includes the function checks if it's differnt than 0, or rather, if there is any kind of collision with the player. If you set banana:remove(false) instead of banana:remove(true), you won't see the effect or hear the sound of the block breaking.
IMPORTANT LINKS
https://wohlsoft.ru/pgewiki/Block_(class)
https://wohlsoft.ru/pgewiki/How_To:_NPC ... tiple_NPCs (this one was written to be used for NPCs, but the process for blocks is almost the same.)
Hope it worked! Good luck!
That's the inefficient way to do it. You're always checking all blocks in the level, even if they're maybe 5 sections away. Better to perform a Block.getIntersecting check around the player's coordinates instead.

Novarender
Monty Mole
Monty Mole
Posts: 145
Joined: Sat Aug 06, 2016 6:59 pm
Flair: Whoa

Re: Need help with lua? - LunaLua General Help

Postby Novarender » Sat Dec 15, 2018 1:15 pm

Could you play two custom musics at the same time but have one of them at zero volume?

Hoeloe
Foo
Foo
Posts: 1463
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 Dec 15, 2018 7:58 pm

No. Unfortunately, SMBX's music engine can only play one piece of music at once.

Novarender
Monty Mole
Monty Mole
Posts: 145
Joined: Sat Aug 06, 2016 6:59 pm
Flair: Whoa

Re: Need help with lua? - LunaLua General Help

Postby Novarender » Sat Dec 15, 2018 10:57 pm

Can you start a song from a certain point?

Taycamgame
Gold Yoshi Egg
Gold Yoshi Egg
Posts: 1483
Joined: Mon Jun 19, 2017 11:35 am
Flair: Stargard
Contact:

Re: Need help with lua? - LunaLua General Help

Postby Taycamgame » Sun Dec 16, 2018 7:33 am

Notxarb wrote:
Sat Dec 15, 2018 1:15 pm
Could you play two custom musics at the same time but have one of them at zero volume?
Even if you could do this... why would you need to? You might as well just have one track playing.

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sun Dec 16, 2018 9:19 am

Taycamgame wrote:
Sun Dec 16, 2018 7:33 am
Notxarb wrote:
Sat Dec 15, 2018 1:15 pm
Could you play two custom musics at the same time but have one of them at zero volume?
Even if you could do this... why would you need to? You might as well just have one track playing.
Crossfade. And no you can't.

Novarender
Monty Mole
Monty Mole
Posts: 145
Joined: Sat Aug 06, 2016 6:59 pm
Flair: Whoa

Re: Need help with lua? - LunaLua General Help

Postby Novarender » Sun Dec 16, 2018 4:34 pm

Taycamgame wrote:
Sun Dec 16, 2018 7:33 am
Notxarb wrote:
Sat Dec 15, 2018 1:15 pm
Could you play two custom musics at the same time but have one of them at zero volume?
Even if you could do this... why would you need to? You might as well just have one track playing.
For example if you have a drumbeat that is added to the song once a certain event happens.

Hoeloe
Foo
Foo
Posts: 1463
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 » Mon Dec 17, 2018 11:56 am

There are plenty of very good reasons for supporting multiple music tracks, and it's something we're looking into for the future.

Taycamgame
Gold Yoshi Egg
Gold Yoshi Egg
Posts: 1483
Joined: Mon Jun 19, 2017 11:35 am
Flair: Stargard
Contact:

Re: Need help with lua? - LunaLua General Help

Postby Taycamgame » Tue Dec 18, 2018 1:15 pm

Ah, ok. I initially thought it was useless because having a music track at 0 volume is essentially the same as not having it at all. But yeah, it does sound like it could do some interesting things.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sun Dec 30, 2018 10:34 am

After playing around with nsmbwalls.lua for a bit, I wanted to see if I could adapt it into a dark level API. However, I can't seem to find any documentation on imagic. It's literally the only red link on the LunaLua helper libraries page on the PGE wiki and a google search doesn't help either. Any help please?

Added in 32 minutes 2 seconds:
Also, how would I fill the entire screen black using draw functions? The documentation on the PGE wiki is not helping at all.

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sun Dec 30, 2018 12:55 pm

The Dwarven Digger wrote:
Sun Dec 30, 2018 11:06 am
After playing around with nsmbwalls.lua for a bit, I wanted to see if I could adapt it into a dark level API. However, I can't seem to find any documentation on imagic. It's literally the only red link on the LunaLua helper libraries page on the PGE wiki and a google search doesn't help either. Any help please?

Added in 32 minutes 2 seconds:
Also, how would I fill the entire screen black using draw functions? The documentation on the PGE wiki is not helping at all.
imagic documentation is in the imagic.lua file.

Black drawing can be done using glDraw.
https://wohlsoft.ru/pgewiki/Graphics.glDraw

ThEnderYoshi
Goomba
Goomba
Posts: 2
Joined: Sat Apr 15, 2017 9:43 pm

Re: Need help with lua? - LunaLua General Help

Postby ThEnderYoshi » Wed Jan 02, 2019 4:57 pm

I wanted to set infinite lives for my episode so I searched about it on wohlsoft.ru and this subforum for information and came up with this:

Code: Select all

onStart()
	mem(0x00B2C5AC, FIELD_FLOAT, 99)
end
But when i started the episode to make sure it was working this error message popped up:

Code: Select all

Error: ...X2/data/worlds/lunalua test episode/lunaworld.lua:5:
'<eof>' expected near 'end'
that's about it

PixelPest
Raccoon Mario
Raccoon Mario
Posts: 7111
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Contact:

Re: Need help with lua? - LunaLua General Help

Postby PixelPest » Wed Jan 02, 2019 5:16 pm

ThEnderYoshi wrote:
Wed Jan 02, 2019 4:57 pm
I wanted to set infinite lives for my episode so I searched about it on wohlsoft.ru and this subforum for information and came up with this:

Code: Select all

onStart()
	mem(0x00B2C5AC, FIELD_FLOAT, 99)
end
But when i started the episode to make sure it was working this error message popped up:

Code: Select all

Error: ...X2/data/worlds/lunalua test episode/lunaworld.lua:5:
'<eof>' expected near 'end'
that's about it
You forgot the function keyword before onStart() which is used when declaring a function. Should be:

Code: Select all

function onStart()
    mem(0x00B2C5AC, FIELD_FLOAT, 99);
end

ThEnderYoshi
Goomba
Goomba
Posts: 2
Joined: Sat Apr 15, 2017 9:43 pm

Re: Need help with lua? - LunaLua General Help

Postby ThEnderYoshi » Wed Jan 02, 2019 5:20 pm

PixelPest wrote:
Wed Jan 02, 2019 5:16 pm
You forgot the function keyword before onStart() which is used when declaring a function. Should be:

Code: Select all

function onStart()
    mem(0x00B2C5AC, FIELD_FLOAT, 99);
end
of course it was something that obvious -_-

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Fri Jan 04, 2019 9:12 am

I'm doing some code for a custom playable for my episode, and despite looking through the documentation for particles.lua I have no idea how it works. I want to create a ground-pound style smoke effect, ideally with adjustable width.

Added in 7 hours 38 minutes 24 seconds:
I'm also working on a modified lives counter, how could I overwrite/remove just the lives number from the HUD? (keeping the 1-up and "x" symbols as-is)

Quantumenace
Ripper II
Ripper II
Posts: 308
Joined: Mon Dec 28, 2015 2:17 am

Re: Need help with lua? - LunaLua General Help

Postby Quantumenace » Fri Jan 04, 2019 9:20 pm

For the smoke, create an emitter:

(this goes outside functions, you only need to do it once:)
poundsmoke=particles.Emitter(0,0, Misc.resolveFile("p_smoke_small.ini")) --you can make your own ini based off this one and change the name here
poundsmoke.enabled=false --you don't want it to keep spawning smoke on its own
poundsmoke.boundleft=-1000000; poundsmoke.boundright=1000000 --this is to force it to draw regardless of how far away the emitter's position is
poundsmoke.boundtop=-1000000; poundsmoke.boundbottom=1000000

in onCameraUpdate():
poundsmoke:Draw()

And when you want to create a puff of smoke, move the emitter to that position and use Emit:

poundsmoke.x=player.x+0.5*player.width; poundsmoke.y=player.y+player.height
poundsmoke:Emit(25)

You can adjust the emitter's size or other properties at any time using setParam or by setting poundsmoke.minX, poundsmoke.maxX etc. directly.


For the hud, I don't think there's a simple way to remove the number, though I could be wrong. Best I can figure, either disable the hud and draw your own, or use the capture buffer to overwrite the section of screen where the number is with what's behind it and then draw over that. If you do use the capture buffer, you probably need it and the hud to be the last step in onCameraUpdate, after particles and everything else are drawn.

PixelPest
Raccoon Mario
Raccoon Mario
Posts: 7111
Joined: Sun Jul 12, 2015 5:38 pm
Flair: Tamer of Boom Booms
Contact:

Re: Need help with lua? - LunaLua General Help

Postby PixelPest » Sat Jan 05, 2019 10:16 am

Quantumenace wrote:
Make sure poundsmoke is a local variable

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sat Jan 05, 2019 2:30 pm

For the HUD I've used a capture buffer (which works fine) and haven't tested the smoke yet. I do however have a (massive) new problem.
After rewriting the timer code, it crashes the game upon loading every time. It's definitely the API causing it since everything works perfectly fine if I remove the API.load() statement. It bypasses the error handler (since no window popup appears) and causes the window to freeze and the top says something like "SMBX2.0 (Not responding)". I have to close it via task manager and when I try testing again the whole tester window has to reload the game instead of bypassing the loading screen. Any clue why this is happening? I've added the full code below for you to look at. (the indentation is correct in the code)
Spoiler: show
local lifeCountPlus = {}

lifeCountPlus.digits = 3
lifeCountPlus.doCrowns = true

lifeCountPlus.starReward = true
lifeCountPlus.starInterval = 100

local lifeTotal = 5

local imagic = API.load("imagic")

function lifeCountPlus.onInitAPI()
registerEvent(lifeCountPlus, "onTick", "tickLoop", true)
registerEvent(lifeCountPlus, "onStart", "atStart", true)
end

local overlayBuffer = Graphics.CaptureBuffer(800, 600)

local crownImg = Graphics.loadImage("lifeCount/crown.png")

local function getMaxLives()
local maxLives = 0
if lifeCountPlus.doCrowns then
for i=1, lifeCountPlus.digits, 1 do
maxLives = maxLives + (10^i)
end
else
maxLives = (10^lifeCountPlus.digits) - 1
end
return maxLives
end

local function strWithLen(aNumber, length)
local strNum = tostring(aNumber)
if #strNum < length then
for i=#strNum, length, 1 do
strNum = "0"..strNum
end
end
return strNum
end

function lifeCountPlus.setLives(newLives)
local maxLives = getMaxLives()
if newLives > maxLives then
newLives = maxLives
end
if newLives < 0 then
newLives = 0
end
lifeTotal = newLives
end

function lifeCountPlus.getLives()
return lifeTotal
end

function lifeCountPlus.atStart()
if lifeTotal > 2 then
mem(0x00B2C5AC, FIELD_FLOAT, 2)
end
end

function lifeCountPlus.addLives(newLives)
if newLives < 0 then
newLives = 0
end
local maxLives = getMaxLives()
if lifeTotal + newLives > maxLives then
lifeTotal = maxLives
else
if lifeCountPlus.starInterval - (lifeTotal % lifeCountPlus.starInterval) < newLives then
--give the player a star
if player.character == CHARACTER_MARIO or player.character == CHARACTER_LUIGI then
player:mem(0x158, FIELD_WORD, 293)
else
NPC.spawn(293, player.x, player.y - 64, player.section)
end
end
lifeTotal = lifeTotal + newLives
end
end

function lifeCountPlus.removeLives(newLives)
if newLives < 0 then
newLives = 0
end
if newLives > lifeTotal then
lifeTotal = 0
else
lifeTotal = lifeTotal - newLives
end
end

function lifeCountPlus.tickLoop()
--Update the life total
livesVis = mem(0x00B2C5AC, FIELD_FLOAT)
if livesVis > 2 then
lifeCountPlus.addLives(livesVis - 2)
else
if livesVis < 2 then
lifeCountPlus.removeLives(2 - livesVis)
end
end
if lifeTotal > 2 then
mem(0x00B2C5AC, FIELD_FLOAT, 2)
else
mem(0x00B2C5AC, FIELD_FLOAT, lifeTotal)
end

-- Update graphics
local boxX = 296
local boxY = 25
local boxW = 40
local boxH = 16
local cX = math.floor(boxX + boxW/2)
local cY = math.floor(boxY + boxH/2)
overlayBuffer:captureAt(-9.9)
local coverBox = imagic.Box -- Draws rectangular section of original screen
{
width = boxW,
height = boxH,
x = boxX,
y = boxY,
scene = false,
texture = overlayBuffer,
filltype = imagic.TEX_FILL,
texoffsetX = (cX - 400)/800,
texoffsetY = (cY - 300)/600
}
coverBox:ScaleTexture(800/boxW, 600/boxH)
coverBox:Draw(9.8, 0xffffffff)

if doCrowns then
local num = 0
local crownNum = 0
local num2 = 0
for i=0, lifeCountPlus.digits - 1, 1 do
j = lifeCountPlus.digits - i
num = num + (10 ^ j)
if lifeTotal >= num then
crownNum = crownNum + 1
num2 = num
end
end
if crownNum == 0 then
Text.printWP(tostring(trueLives), 1, 297, 27, 9.9)
else
drawX = 297
for i=1, crownNum, 1 do
Graphics.drawImageWP(crownImg, drawX - 1, 25, 9.9)
drawX = drawX + 18
end
local text = strWithLen(trueLives-num2, lifeCountPlus.digits - crownNum)
Text.printWP(text, 1, drawX, 27, 9.9)
end
else
Text.printWP(tostring(trueLives), 1, 297, 27, 9.9)
end
end

return lifeCountPlus

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

Re: Need help with lua? - LunaLua General Help

Postby Emral » Sat Jan 05, 2019 2:47 pm

Just a hunch but... might be related to you capturing outside of functions with the capture buffer. At that point in time most SMBX objects aren't initialised yet, so that might lead to an error.

The Dwarven Digger
Lakitu
Lakitu
Posts: 484
Joined: Sun Oct 30, 2016 11:17 am
Pronouns: they/them

Re: Need help with lua? - LunaLua General Help

Postby The Dwarven Digger » Sat Jan 05, 2019 2:57 pm

Enjl wrote:
Sat Jan 05, 2019 2:47 pm
Just a hunch but... might be related to you capturing outside of functions with the capture buffer. At that point in time most SMBX objects aren't initialised yet, so that might lead to an error.
The buffer object is initialised outside of functions but the captureAt function is only called inside the onTick() function. This is the exact setup I had in the previous version that worked fine, so that's not it. The main new things I've used (new for the API and for me) are the different type of for loops (as in "for i=1, 3, 1 do ... "), the % operator and the ^ operator. I've used everything else in some way before.


Return to “LunaLua”

Who is online

Users browsing this forum: No registered users and 2 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari