keep geting nill for the monsters global but idk why plz have a look at my code

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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: keep geting nill for the monsters global but idk why plz have a look at my code

Post by s-ol »

pedrosgali wrote:Think you just need to add return enemy as the last line of enemy.lua, currently from what I can tell you don't return the enemy table so require would return nil.
that was my first thought but he never actually uses the table. The enemy file also exports another table "monsters" which actually contains the "enemy" instances. His structure is... weird.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: keep geting nill for the monsters global but idk why plz have a look at my code

Post by 4aiman »

Is it me or the problem is that he never calls function enemy.load() ?..
pincholinco
Prole
Posts: 6
Joined: Thu Jun 09, 2016 10:11 am

Re: keep geting nill for the monsters global but idk why plz have a look at my code

Post by pincholinco »

plz if you are not going to help with the issue or try too by running the code plz don't post on this thread I can run the code I gave you perfectly the error is on the title of the thread and if u want to know the line it is line 115 and I do use the char on the main lua by updating the player table the problem is that the monsters table is not being loaded I think for some reason
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: keep geting nill for the monsters global but idk why plz have a look at my code

Post by zorg »

4aiman wrote:Is it me or the problem is that he never calls function enemy.load() ?..
That seems to be the issue in my opinion as well.
pincholinco wrote:plz if you are not going to help with the issue or try too by running the code plz don't post on this thread I can run the code I gave you perfectly the error is on the title of the thread and if u want to know the line it is line 115 and I do use the char on the main lua by updating the player table the problem is that the monsters table is not being loaded I think for some reason
to. try to. please. you.
And the reason is what 4aiman and me both figured out, but let me be an uncharacteristically (by my standards anyway) nice guy and give you a better version of your code; only on the indentation-side, i didn't fix any diction or grammar issues, i'm not your english teacher.

Code: Select all

local anim8 = require 'anim8'
local enemy = require "enemy"
local char = require "char"


function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
	return x1 < x2+w2 and
	       x2 < x1+w1 and
	       y1 < y2+h2 and
	       y2 < y1+h1
end

function love.load()

	--WORLD--

	world = love.physics.newWorld(0, 200, true)

	ground = {}
	ground.body = love.physics.newBody(world, 400, 575, "static") 
	ground.shape = love.physics.newRectangleShape(1000, 50) 
	ground.fixture = love.physics.newFixture(ground.body,ground.shape,100) 
 

	restart = function()
		if alive == -2 and love.keyboard.isDown("r") then
			alive = 2
			player.lifes = 100
			player.bulletsr = {}
			player.bulletsl = {}
			player.coldown = 20
			monsters = {} -- You were missing either this, or after looking into your enemies file, you never call enemy.load -zorg
			monsters.coldown = 0
			monsters.sup = {}
			bitecooldown = 10
		end
	end

	-- PLAYER--

	--

	--MONSTER--

	--

end



function love.draw()

	--PLAYER-- 

	--

	--MONSTER--  

  	--

	--WORLD--

	love.graphics.polygon("fill", ground.body:getWorldPoints(ground.shape:getPoints())) 
	
	love.graphics.print(player.thisy , 90, 0)
	
	love.graphics.print(player.lifes)
	
	love.graphics.print(player.score,30, 0 )
	
	love.graphics.setBackgroundColor( 0, 0, 60)
	  
	if alive == 2 then
		love.graphics.print( "true", 60, 0)
	else
		love.graphics.print( "false", 60, 0)
	end

end




function love.update(dt)

	if player then
		player:update(dt)
	end

	if monsters then
    	monsters:update(dt)
	end

	--WORLD--

	restart() -- This will mess you up in the long run, boy; just call it at the end of love.load -zorg

	world:update(dt)

	for i, monster in ipairs(monsters.sup) do
		for j, bullet in ipairs(player.bulletsr) do
			if CheckCollision(monster.x, monster.y , 38, 46 , bullet.x, bullet.y, 10, 3) then
				table.remove(player.bulletsr, j)
				table.remove(monsters.sup, i)  
				player.score = player.score + 1 
			end
		end
	end

	for i, monster in ipairs(monsters.sup) do
		for j, bullet in ipairs(player.bulletsl) do
			if CheckCollision(monster.x, monster.y , 38, 46 , bullet.x, bullet.y, 10, 3) then
				table.remove(player.bulletsl, j)
				table.remove(monsters.sup, i) 
				player.score = player.score + 1 
			end
		end
	end

	for i, monster in ipairs(monsters.sup) do
		if  CheckCollision(monster.x, monster.y, 38, 46, player.x, player.y,22 ,54 ) 
		and player.lifes > 0 and bitecooldown <= 0 then
			bitecooldown = 10
			player.lifes = player.lifes - 1    
		end
	end

	--PLAYER--

	--

	--MONSTER--

	--

end



function love.keypressed(key)

	if key == 'escape' then love.event.quit() end

	if key == "right" then
		t =  1
	elseif key == "left" then
		t = -1
	end

	if key == "r" then
		alive = 2
	end

end
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
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: keep geting nill for the monsters global but idk why plz have a look at my code

Post by s-ol »

pincholinco wrote:plz if you are not going to help with the issue or try too by running the code plz don't post on this thread I can run the code I gave you perfectly the error is on the title of the thread and if u want to know the line it is line 115 and I do use the char on the main lua by updating the player table the problem is that the monsters table is not being loaded I think for some reason
Hey, it's no secret I wasn't being super nice with my last post, but the intention was to help: I provided you with seven things you could and should do or have done to help people help you instead of being mad at you or just closing the tab with your thread again.

This is a public forum and usually a very nice one at helping people, provided they follow the rules. There is a big stickied thread called "REQUESTING HELP - Posting Rules". On the way to making this thread you will have to have seen the read banner above telling you to read it at least three times, and probably a lot more often than that, it's been at the top of every page of the forum you ever visited:

Image

There's four points in the "short" version:
  1. Check first, on the forums and on the wiki, if the same problem hasn't been asked and answered before.
  2. Tell us exactly what the problem is. This also means having a thread title more meaningful than "plz help me" or "it does not work".
  3. Give us a .love of your game. This allows us to find the error, see it in context and possibly try out some candidate solutions.
  4. Here is the offline documentation: http://love2d.org/forums/viewtopic.php?f=3&t=1796
You disregarded two of the four points (2 & 3), and the last one doesn't even apply to your problem.

And if you think the rules don't matter or don't make sense, I can assure you they do. Apart from writing the last posts, I actually spent something like five minutes reading your code and I tried downloading and running the files you provided (even though you uploaded them outside of a .love which made things harder), only to find that they are in fact not runnable without the other files you did not attach.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests