I need help (again)

I'm really new to lua (aka i've never used it before) and I'm trying to make a game for a game jam. I have it mostly figured out, it's an idle game where you have to print money and you can get more printers and people to print for you and stuff. BUT, I'm trying to incorporate the upgrades into the system menu because I can't figure out how to make a menu of my own. However, I'm using an if statement so that the menu item doesn't appear unless you have enough money, to avoid going into negative money. The menu item works just fine, but it doesn't appear when I use the if statement, and the opposite happens when I put the whole thing into the playdate.update() function (It makes 3 menu items that are all the same), and I have no idea what to do. Here's the part of the code that I'm currently working on:

local menu = playdate.getSystemMenu()

if money >= 100 then

    local addPrinter = menu:addMenuItem("+Printer", function()

        money = money - 100

        printers = printers + 1

    end)

end

scene.set(game, LDtk.playerStartLocation)

function playdate.update()

    input.update()

    gfx.sprite.update()

    scene.update()

    if config.showFPS then

        playdate.drawFPS()

    end

    if playdate.isCrankDocked() == true then

        playdate.ui.crankIndicator:update()

    end

    playdate.timer.updateTimers()

    playdate.inputHandlers.push(myInputHandlers)

    playdate.graphics.drawText("*Money:* " .. money, 40, 200)

    playdate.graphics.drawText("*Printers:* " .. printers, 20, 25)

end

Also I forgot to mention just a small thing...I have less than 24 hours to finish the whole game :\

aaaaaaaaaaaand also images aren't working at all

The reason for there appearing no menu items for the code you have is that this part

if money >= 100 then

    local addPrinter = menu:addMenuItem("+Printer", function()

        money = money - 100

        printers = printers + 1

    end)

end

is only called once when the game is first started as its not in the update function. But by having it in the update function a new menu item will be added for each frame where money is greater then 100, this is why you get 3 of the same menu items (3 is the maximum number of items you can add).

In the sdk there is a callback (playdate.gameWillPause) that is called when the menu button has been pressed. By populating the menu items within this callback you should get the behaviour you want. You could do something like this:

playdate.gameWillPause = function()
	menu:removeAllMenuItems()
	if money >= 100 then
		local addPrinter = menu:addMenuItem("+Printer", function()
			money = money - 100
			printers = printers + 1
		end)
	end
end

Hope this helps

Thanks! Do you have an idea for images? I'm trying to add an animation for the printer, so I found royalty-free photos for a printer and cash, but can't get the images to display. It's creating playdate image files for them when I compile it, but not actually loading the image.

Have you got any code I could have a look at for drawing the images?

Sure, here:

function playdate.update()

    input.update()

    gfx.sprite.update()

    scene.update()

    if config.showFPS then

        playdate.drawFPS()

    end

    if playdate.isCrankDocked() == true then

        if printers >= 1 then

            playdate.ui.crankIndicator:update()

        end

    end

    playdate.timer.updateTimers()

    playdate.inputHandlers.push(myInputHandlers)

    playdate.graphics.drawText("*Money:* " .. money, 40, 200)

    playdate.graphics.drawText("*Printers:* " .. printers, 20, 25)

    playdate.graphics.image.new("images/printer")

end

Just to note, there IS a file at Source/images/printer.png and it IS 200 x 120 and black-and-white.

Aa ok, so this code here

creates a new image object, it doesn’t draw the image. But the object that is returned does have a draw function. You also have this in the update loop, meaning you are creating a new image object every frame.

What you must do is:

local image = playdate.graphics.image.new("images/printer")
function playdate.update()

    ...

    playdate.graphics.drawText("*Money:* " .. money, 40, 200)

    playdate.graphics.drawText("*Printers:* " .. printers, 20, 25)

    image:draw(x, y)
end

where x and y is the position you want to draw it.

1 Like

a-HA. Thanks, sorry for asking so many things, I really suck at this lol

1 Like

No problem, we always have to start somewhere

1 Like

OK I'm just about to log off for today, so ONE LAST teensy little thing. I want to make a cool little animation where when you print money, then the cash picture comes out of the printer. I know I can probably use playdate.timer.keyRepeatTimerWithDelay(), but just in case, do you know if there's a built in function to move images? That's the only other thing that I need help with, so I won't bother you again :smiley:

If you want to move an image I wouldn’t use playdate.timer.keyRepeatTimerWithDelay() as that is mostly used for repeating a button input. There is something called animator in the sdk, which is used for what you want. It animates between to numbers, info about it can be found here: https://sdk.play.date/1.12.3/Inside%20Playdate.html#C-graphics.animator

You can use that to animate the x and y position of the image. E.g.:

import "CoreLibs/animator"

local image = playdate.graphics.image.new("images/printer")

local animatorX = playdate.graphics.animator.new(10000, 0, 50)
local animatorY = playdate.graphics.animator.new(10000, 0, 50)

function playdate.update()

    ...

    playdate.graphics.drawText("*Money:* " .. money, 40, 200)

    playdate.graphics.drawText("*Printers:* " .. printers, 20, 25)

    image:draw(animatorX:currentValue(), animatorY:currentValue())
end
1 Like

alr thanks I think I can finish this tomorrow!

1 Like