Page 1 of 1

Help me Idk anymore (Importing other scripts)

Posted: Thu Feb 23, 2023 9:50 am
by Kurogami Neko
Hello kind humans,
I have been trying for days to import my game.lua to my main.lua. It just won't work :death: :death:
I want game.lua to run after pressing the start game...
I have been trying by watching videos, reading tutorials but IDK how to ! I know how to import simple var like x=1 from other script but dont know how to execute the entire script! Please help...
ps: sorry for the mess in the code.. I have been trying too much
my main.lua

Code: Select all

fullscreen = false
require "game"
Menu = require 'library/menu'


function love.load()
	start = game.f1()
	player = game.f2()
	game = game.f3()

	testmenu = Menu.new()
	testmenu:addItem{
		name = 'Start Game',
		action = function()
			
			start.game.f1()
		
		end
	}
	testmenu:addItem{
		name = 'Load Game',
		action = function()
			-- do something
		end
	}
	testmenu:addItem{
		name = 'Options',
		action = function()
			-- do something
		end
	}
	testmenu:addItem{
		name = 'Quit',
		action = function()
			love.event.quit()
		end
	}

	
end

function love.update(dt)
	testmenu:update(dt)
	
end

function love.draw()
	testmenu:draw(250, 250)--menu table position(x,y)

end

function love.keypressed(key)
	testmenu:keypressed(key)
end
my game.lua

Code: Select all

game = {}

game.f1 = function()
	 anim8 = require 'library/anim8'
	love.graphics.setDefaultFilter("nearest","nearest")

	player = {}   --empty table
	player.x = 400
	player.y = 200
	player.speed = 2
	
	player.spritesheet = love.graphics.newImage('pic/lady.png')
	player.sprite = love.graphics.newImage('pic/parrot.png')
	player.grid = anim8.newGrid(103, 121, player.spritesheet: getWidth(), player.spritesheet: getHeight() )--create grid
--get grid by dividing resolution with 4
	player.animations = {}
	player.animations.down = anim8.newAnimation(player.grid('1-4',1),0.2 )
	player.animations.left = anim8.newAnimation(player.grid('1-4',2),0.2 )
	player.animations.right = anim8.newAnimation(player.grid('1-4',3),0.2 )
	player.animations.up = anim8.newAnimation(player.grid('1-4',4),0.2 )

	player.anim = player.animations.down



	background = love.graphics.newImage('pic/background.png')

	
end

game.f2 = function(dt)
	local isMoving = false

	if love.keyboard.isDown("right") then
		player.x = player.x + player.speed
		player.anim = player.animations.right
		isMoving = true
	end

	if love.keyboard.isDown("left")then
		player.x = player.x - player.speed
		player.anim = player.animations.left
		isMoving = true
	end

	if love.keyboard.isDown("up")then
		player.y =player.y - player.speed
		player.anim = player.animations.up
		isMoving = true
	end

	if love.keyboard.isDown("down")then
		player.y =player.y + player.speed
		player.anim = player.animations.down
		isMoving = true
	end

	if isMoving == false then
		player.anim:gotoFrame(2)
	end

	player.anim:update(dt)
end

game.f3 = function()
	love.graphics.draw(background,0,0)
	--player.anim:draw(player.spritesheet,player.x, player.y, nil, 5)
	player.anim:draw(player.spritesheet,player.x, player.y)
end
return game



Thank you *sobs*

Re: Help me Idk anymore (Importing other scripts)

Posted: Thu Feb 23, 2023 12:59 pm
by darkfrei
Working code from chatGPT:

Code: Select all

--button.lua
-- Button class
local Button = {}
Button.__index = Button

function Button:new(x, y, w, h, text, action)
    local button = setmetatable({}, Button)
    button.x = x
    button.y = y
    button.w = w
    button.h = h
    button.text = text
    button.action = action
    return button
end

function Button:draw()
    love.graphics.setColor(1, 1, 1)
    love.graphics.rectangle("line", self.x, self.y, self.w, self.h)
    love.graphics.printf(self.text, self.x, self.y + self.h / 2 - 8, self.w, "center")
end

function Button:isClicked(x, y)
    return x > self.x and x < self.x + self.w and y > self.y and y < self.y + self.h
end

return Button

Code: Select all

--menu.lua
-- Menu class
local Menu = {}
Menu.__index = Menu

function Menu:new(buttons)
    local menu = setmetatable({}, Menu)
    menu.buttons = buttons
    return menu
end

function Menu:update(dt)
    -- TODO: handle update logic if needed
end

function Menu:draw()
    for i, button in ipairs(self.buttons) do
        button:draw()
    end
end

function Menu:mousepressed(x, y, button)
    if button ~= 1 then
        return
    end
    
    for i, button in ipairs(self.buttons) do
        if button:isClicked(x, y) then
            button.action()
        end
    end
end

return Menu

Code: Select all

--main.lua
Button = require ("button")
Menu = require ("menu")
-- Example usage
function love.load()
    local buttons = {
        Button:new(100, 100, 200, 50, "Button 1", function() print("Button 1 clicked") end),
        Button:new(100, 200, 200, 50, "Button 2", function() print("Button 2 clicked") end),
        Button:new(100, 300, 200, 50, "Exit", function() love.event.quit( ) end),
    }
    menu = Menu:new(buttons)
end

function love.update(dt)
    menu:update(dt)
end

function love.draw()
    menu:draw()
end

function love.mousepressed(x, y, button)
    menu:mousepressed(x, y, button)
end

Re: Help me Idk anymore (Importing other scripts)

Posted: Sat Feb 25, 2023 3:34 pm
by RNavega
The Lua PIL ebook lacks much needed clarity in some parts, like this thing about 'require': it returns the value(s) returned by the script it's loading.
Therefore instead of...

require "game"

... you need to have...

local game = require("game")

That's why you're putting that 'return' at the end of game.lua, after all.

Re: Help me Idk anymore (Importing other scripts)

Posted: Sat Feb 25, 2023 3:39 pm
by RNavega
As for it not working, have you ever got it to work at some point?
Instead of trying to do a complex thing at first (menu, gameplay etc), I really suggest going step by step: getting some graphics showing on screen; getting the player to move from keyboard input; getting a workable menu, and so on.

Once you have all these individual parts working isolated (like writing a separate project for each part), then you can try linking them in the same project with state transitions.
Because if you've never written a menu ever before, it's quite a challenge.