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.
pincholinco
Prole
Posts: 6
Joined: Thu Jun 09, 2016 10:11 am

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

Post by pincholinco »

this is main.lua

Code: Select all

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


function love.load()
  
  
  --WORLD--
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

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.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()

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

this is the enemy module

Code: Select all

local enemy = {}


function enemy.load()
image2 =  love.graphics.newImage("media/monster1.png")
local gmonster = anim8.newGrid(38,46, 228,46)
monsterr = anim8.newAnimation(gmonster('4-6',1), 0.1)
monsterl = anim8.newAnimation(gmonster('1-3',1), 0.1)

monsters = {}  
bitecooldown = 10 
monsters.coldown = 100
mdirection = -1
monsters.sup = {}
  
monsters.spawn = function()
if monsters.coldown <= 0 and player.lifes > 0 then
monsters.coldown = 100
monster = {} 
randomNumber = love.math.random(1, 800)
if player.x < randomNumber and randomNumber <= player.x + 22 then 
randomNumber = player.x +50
end
monster.x = randomNumber
monster.y = 490
monster.thisxM = nil
monster.thisyM = nil
monster.b = love.physics.newBody(world, monster.x + 38/2, monster.y + 46/2, "dynamic") 
monster.b:setMass(10)  
monster.s = love.physics.newRectangleShape(38, 46) 
monster.f = love.physics.newFixture(monster.b, monster.s, 1) 
table.insert(monsters.sup, monster)
end
end

end


function enemy.update()

monsters.spawn()

for i,v in ipairs(monsters.sup) do 
monster.thisxM, monster.thisyM = v.b:getWorldPoints(v.s:getPoints())
v.y = monster.thisyM
monter.thisxM = v.x
end

bitecooldown = bitecooldown - 1

monsters.coldown = monsters.coldown - 1  
  
for i,v in ipairs(monsters.sup) do 
if v.x > player.x + 20 then
mdirection = -1
v.x = v.x - 3
elseif v.x < player.x - 30
then
v.x = v.x + 3
mdirection = 1
end
end

for i,v in ipairs(monsters.sup) do 
v.b:setLinearVelocity(0, 300)
end

monsterl:update(dt)
monsterr:update(dt)



end

function enemy.draw()
  
  if alive == 2 then
for i,v in ipairs(monsters.sup) do
if mdirection == -1 then
monsterl:draw(image2, v.x, v.y)
elseif mdirection == 1 then
monsterr:draw(image2, v.x, v.y)
end
end
end
  
  
  
end

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 »

I don't understand because I used require for the enemy module therefore the enemy should be loading and also don't have any processes to set the table "monsters" to a nil value therefore it should be working I think could you guys have a look at the code and try to figure out what is wrong please ?
User avatar
Linkpy
Party member
Posts: 102
Joined: Fri Aug 29, 2014 6:05 pm
Location: France
Contact:

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

Post by Linkpy »

Code: Select all

local enemy = {}
Remove the "local". With local, the "enemy" table is only usable within the file "enemy.lua".
Founder of NeoShadow Studio. Currently working on the project "Sirami".
github / linkpy
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 »

Also please provide a .love file next time in case the issue is not as obvious as this one and for the love of god start indenting your code. I hope it's just a copy&paste issue, you are hurting yourself so bad by not doing it.

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
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 »

thank you and I have another file that is the char.lua file and on that file I use local char = {} and it works why does the enemy doesn't ?

and also what is indenting ?
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 »

Attachments
char.lua
(2.97 KiB) Downloaded 186 times
enemy.lua
(1.79 KiB) Downloaded 240 times
main.lua
(2.4 KiB) Downloaded 187 times
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 »

those are all the files of my game code and tried what you suggested but it didn't work I am still getting nil for "monsters"
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

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

Post by Sheepolution »

What is the error message? It would help a lot if we knew on what line and in what file it would happen.
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 »

1. that is not a .love file, that are three different files.
2. because its not a .love file and you didnt include your dependencies (like anim8) I can't run it, so I can't really help you unless I put in extra effort and download them by myself.
3. like sheepo said, you didn't even tell us the error message.
4. this thread is in the wrong forum. It should be in support & development.
5. you aren't even using "char" so there is nothing that can go wrong with char.lua.
6. you triple posted. If you notice you have something to add edit your last post instead of creating a new one.
7. http://lmgtfy.com/?q=indenting+code

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
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

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

Post by pedrosgali »

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.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests