[SOLVED]Problem with my code (likely something really simple

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.
Bobbias
Prole
Posts: 36
Joined: Sat Jun 29, 2013 1:26 pm

[SOLVED]Problem with my code (likely something really simple

Post by Bobbias »

EDIT: new problem. The same line:

Code: Select all

mapLoader = MapLoader()
Is giving me a new error: Attempt to call a table value.

Alright, I'm guess this is going to be something painfully basic that any self respecting Lua coder would know, but I've just started getting into Lua and my grasp of some of it is still a bit shaky...

In any case, I ran across an old project someone made a while back here called FEZ that is a framework for a component based entity system. I've been in the process of figuring out how it works looking at the demo code and FEZ's code, and I think I've got some idea of what's going on. The first piece of code I want to get working is basically just supposed to draw the map on the screen, just to make sure that the component system is working as intended so I can add the rest of the stuff in. Unfortunately, it doesn't run, and give me an error saying:

Code: Select all

preload.lua:35: attempt to call global 'MapLoader' (a nil value)
Now I know what this "means" in the sense that I know it's complaining that MapLoader doesn't actually exist. However, I'm not exactly sure what's wrong with what I'm doing... here's my preload.lua

Code: Select all

preload = {}

require "EntityFactory"

require "attributes.GameStateAttribute"
require "util.fez.ControllerManager"
require "util.fez.EntityManager"
require "util.fez.EntityTagManager"
require "util.fez.EventDispatcher"
require "util.fez.AspectManager"
require "util.fez.Component"
require "util.fez.ComponentCache"
require "controllers.MapController"
require "renderers.MapRenderer"
require "MapLoader"


function preload.init()
  math.randomseed(os.time())
  
  entityManager = EntityManager()
  controllerManager = ControllerManager()
  
  game   = createGame()
  gameState = entityManager:getComponentFromEntity( game, GameStateAttribute )
 
  mapController = MapController( controllerManager )
  mapRenderer = MapRenderer( controllerManager )
  mapLoader = MapLoader()  --This is the error line.
  
  MapLoader:load( 'resources/levels/level1.lua' )
  controllerManager:refresh()
end

return preload
Here's MapLoader.lua

Code: Select all

MapLoader = {}

function MapLoader:load( filepath )
  local mapLuaFile = love.filesystem.load( filepath )
  local leveldata = mapLuaFile()
  local tileset = self:loadTiles( leveldata.tilesets[1] )
  local mainLayer = mapdata.layers[1]
  local tilemap   = mainLayer.data

  local map = entityManager:createEntity('level')
  entityManager:addComponentToEntity( level, LevelAttribute( leveldata, tileset, tilemap ) )
  entityManager:refreshEntity( level )

  return map
end

function MapLoader:loadTiles( tileset )
  local graphics = love.graphics
  local quads 	  = {}
  local numberOfTiles = (tileset.imagewidth / tileset.tilewidth) * (tileset.imageheight / tileset.tileheight)
  local image 	  = graphics.newImage( tileset.image )
  local spriteBatch = graphics.newSpriteBatch( image, 1000 )
	
  local i = 1
  for y = 0, (tileset.imageheight / tileset.tileheight)-1 do
    for x = 0, (tileset.imagewidth / tileset.tilewidth)-1 do
      quads[i] = graphics.newQuad(x * tileset.tilewidth, y * tileset.tileheight, 
        tileset.tilewidth, tileset.tileheight,
        tileset.imagewidth, tileset.imageheight
      )
      i = i + 1
    end
  end

  return {
    ['data'] = tileset;
    ['quads'] = quads;
    ['spriteBatch'] = spriteBatch;
  }
end

return MapLoader
I think everything that's relevant is in the .love I've attached. Some code was taken and slightly modified from the YakManExample that's on the FEZ github.

EDIT: gah, forgot to complete the title... then I had to wait for it to be accepted. Oh well, fixed now.
Attachments
game.love
(28.46 KiB) Downloaded 265 times
Last edited by Bobbias on Tue Jul 09, 2013 2:47 pm, edited 3 times in total.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Problem with my code (likely something really simple)

Post by Plu »

The MapLoader.lua in your .love file initializes itself as "LevelLoader", not "MapLoader", which might explain the issue (although the one you posted here does call itself correctly and should work.)
Bobbias
Prole
Posts: 36
Joined: Sat Jun 29, 2013 1:26 pm

Re: Problem with my code (likely something really simple)

Post by Bobbias »

Ahh, I see what happened. Somehow the project in notepad++ switched the location for that MapLoader file so I actually modified the code in the demo project for FEZ not the file in the directory.

It's the little things like this that always end up catching me :/

Although now it's telling me that I'm attempting to call 'MapLoader' (a table value)... I really need to sit down and figure out the finer points of how this stuff works in more detail.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Problem with my code (likely something really simple)

Post by Plu »

You cannot call tables indeed. I'm not sure why you included the mapLoader = MapLoader() part? It seems that MapLoader is already functional and you can just remove that line and jump directly to the one that does MapLoader:load() as that seems to be how the thing should work.
Bobbias
Prole
Posts: 36
Joined: Sat Jun 29, 2013 1:26 pm

Re: Problem with my code (likely something really simple)

Post by Bobbias »

And you're entirely correct. Still getting used to the specifics of how Lua works... I still don't quite grasp how the OOP stuff works exactly. Guess it's time to sit down to read through middleclass.lua.

Most of my programming experience is in Java and C#, so it's taking a bit to adapt to Lua's differences.

Also, why on earth does this error message not tell you which parameter is wrong: Incorrect parameter type: expected userdata

(yep, that's the newest error I'm getting. I perfectly expected this not to compile for a while until I fix all the kinks, but ugh, this one doesn't seem to make sense to me)

EDIT: ok, so apparently Lua didn't like me using 0 as a tile ID in the map, and for some reason it was turning into nil in a certain spot that was causing that problem. After judicious use of the print command and --console I tracked the issue down... Now I have a rudimentary tile map system working. That was actually fewer errors than I expected to encounter (though they took far longer to deal with than I'd like).
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Problem with my code (likely something really simple)

Post by raidho36 »

Think of it as not an OOP (because it is not, actually). Just a mess of tables and closures.
Bobbias
Prole
Posts: 36
Joined: Sat Jun 29, 2013 1:26 pm

Re: Problem with my code (likely something really simple)

Post by Bobbias »

Yeah, I just meant that I needed to figure out exactly what middleclass was doing to things so I knew the "new rules" for "classes" created by it. I kinda get what Metatables can do, but I need to examine the code so I can really understand what is actually going on.

That said I dicked around in Haskell a while back, and this is nowhere near as hard to wrap my head around than that was.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Problem with my code (likely something really simple)

Post by Lafolie »

raidho36 wrote:Think of it as not an OOP (because it is not, actually). Just a mess of tables and closures.
If it looks like an object, and it behaves like an object, then it's an object. Of course it's object-oriented if you set it out that way. By your logic C++, Java and so on are not OOP languages because they're just a mess of memory addresses and tricks.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Bobbias
Prole
Posts: 36
Joined: Sat Jun 29, 2013 1:26 pm

Re: Problem with my code (likely something really simple)

Post by Bobbias »

Alright, so I've run into something that has me somewhat confused and I figured it wasn't worth making a new thread. I've got things running, and I'm rendering my basic map on the screen... Except that it seems like the tiles on the right side and bottom of my map are being stretched. I've been looking over the relevant code in controllers/MapController.lua and MapLoader.lua but I can't seem to figure out what's going on.

Image

Ignore the block markings, those were just to make sure I was actually rendering the top corner in the right place.
1.love
(30.2 KiB) Downloaded 260 times
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Problem with my code (likely something really simple)

Post by raidho36 »

Lafolie wrote:By your logic C++, Java and so on are not OOP languages because they're just a mess of memory addresses and tricks.
What do you know, lol. Modern OOP has nothing to do with the whole idea of OOP. As of seen today, it's just a mess of dirty tricks and syntax sugar on top of plain imperative programming.
Post Reply

Who is online

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