Page 1 of 1

Use require function.

Posted: Sun Jul 30, 2017 6:17 pm
by Pospos
i want to use require function in my game, but this don't work, i want to know why.
this creates a blank black screen and nothing else
main.lua

Code: Select all

function love.load()
 require("color")
 menu = false
 
end
function love.update(dt)
	if menu == false then
		require("player")
	end
end
function love.draw()

end
player.lua

Code: Select all

function love.load()
	-- player values
	player = {}
	player.x = 0
	player.y = 0
	player.w = 32
	player.h = 32
	player.items = {}
	player.items[1] = BasicWolfTrap
	player.speed = 1
	-- animations
	player[1] = "blank1"
	player[2] = "blank2"
	player[3] = "blank3"
	-- first item of the player
	BasicWolfTrap = {}
	BasicWolfTrap[1] = 10
	BasicWolfTrap[2] = "Trap"
	BasicWolfTrap[3] = "blank"
	keymap = {}
	keymap[1] =  love.keyboard.isDown("down")
	keymap[2] =  love.keyboard.isDown("up")
	keymap[3] =  love.keyboard.isDown("left")
	keymap[4] = love.keyboard.isDown("right")
end


function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
	--creates a collision rectangle
 local bx1,by1,bx2, by2 = x1 + w1 ,y1 + h1, x2 + w2, y2 + h2
 return x1 < x2+w2 and
   x2 < x1+w1 and
    y1 < y2+h2 and
    y2 < y1+h1
end 

function love.update(dt)
	-- use this to call keyboard operations
	if keymap[1]then
		player.y = player.y + player.speed
	end

	if keymap[2]then
		player.y = player.y - player.speed
	end

	if keymap[3]then
		player.x = player.x - player.speed
	end

	if keymap[4]then
		player.x = player.x - player.speed
	end
end

function love.draw()
	love.graphics.setColor(indigo)
	-- draw using the pallet required
	love.graphics.rectangle("fill", player.x, player.y)
end
and color.lua

Code: Select all

function love.load()
	indigo = {77, 0, 255, 255}
	pastel = {93, 138, 205}
	green = {88, 236, 127}
	olive = {15, 111, 54}
	yellow = {167, 226, 4}
	orange = {196, 130, 44}
	rouge = {222, 26, 80}
	rose = {222, 28, 183}
end
function love.update(...)
	
end
function love.draw(...)
	
end

Re: Use require function.

Posted: Sun Jul 30, 2017 8:11 pm
by zorg

Re: Use require function.

Posted: Sun Jul 30, 2017 8:24 pm
by sefan
You can only have one load/update/draw. Name you other functions different and call them. Like this:

main.lua

Code: Select all

require("color") --require file color.lua
require("player") --require file player.lua
		
function love.load()
 menu = false
 	colors() --Calls colors in color.lua
end
function love.update(dt)
	if menu == false then
		playerupdate(dt) --calls playerupdate in player.lua
	end
end
function love.draw()
	
end
player.lua

Code: Select all

...
function playerupdate(dt)
	-- use this to call keyboard operations
	if keymap[1]then
		player.y = player.y + player.speed
	end

	if keymap[2]then
		player.y = player.y - player.speed
	end

	if keymap[3]then
		player.x = player.x - player.speed
	end

	if keymap[4]then
		player.x = player.x - player.speed
	end
end
...
and color.lua

Code: Select all

function colors()
	indigo = {77, 0, 255, 255}
	pastel = {93, 138, 205}
	green = {88, 236, 127}
	olive = {15, 111, 54}
	yellow = {167, 226, 4}
	orange = {196, 130, 44}
	rouge = {222, 26, 80}
	rose = {222, 28, 183}
end

Re: Use require function.

Posted: Sun Jul 30, 2017 8:32 pm
by Pospos
Can you require multiple function in one require?

Re: Use require function.

Posted: Sun Jul 30, 2017 8:44 pm
by sefan
After you require a file you can use all functions in that file.
So if you have the functions playerload, playerupdate and playerdraw in your player.lua file. You can write this:

Main.lua

Code: Select all

require "player" --require file player.lua

function love.load()
	playerload() --loading player
end

function love.update(dt)
	playerupdate(dt) --updating player
end

function love.draw()
	playerdraw() --draw player
end

Re: Use require function.

Posted: Sun Jul 30, 2017 8:52 pm
by Pospos
yeah that's what i did
Thank a lot for your help.

Re: Use require function.

Posted: Mon Jul 31, 2017 7:43 pm
by Beelz
The general thing to do is return a local table from another file and place into a variable.

main.lua

Code: Select all

local myRequiredTable = require 'otherfile'

function love.load()
	myRequiredTable.printStuff()
end
otherfile.lua

Code: Select all

local myLocalTable= {
	a = 1,
	b = 2,
	c = 3
}

function myLocalTable.printStuff()
	print(myLocalTable.a, myLocalTable.b, myLocalTable.c)
end

return myLocalTable