Use require function.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Use require function.

Post 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
Attachments
Suyd Cyk.love
(885 Bytes) Downloaded 112 times
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Use require function.

Post by zorg »

Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
sefan
Prole
Posts: 23
Joined: Fri Jul 31, 2015 7:03 am
Contact:

Re: Use require function.

Post 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
Sefan.se GitHub.com Itch.io
Looking for help on graphics/music/sound effects. PM me if interested.
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: Use require function.

Post by Pospos »

Can you require multiple function in one require?
User avatar
sefan
Prole
Posts: 23
Joined: Fri Jul 31, 2015 7:03 am
Contact:

Re: Use require function.

Post 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
Sefan.se GitHub.com Itch.io
Looking for help on graphics/music/sound effects. PM me if interested.
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: Use require function.

Post by Pospos »

yeah that's what i did
Thank a lot for your help.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Use require function.

Post 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

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 156 guests