Page 1 of 1

Need help for my loader...

Posted: Wed Apr 26, 2017 12:45 pm
by XaafCode
Hello everyone,

I am making a custom level loader, but when I run it, it gives me an error saying "= expected near ..."

My code:

Code: Select all

Level = {}

function Level:get(map)
  if map ~= nil then
    level = map
  else
    level = "../levels/levelCrash"
  end
  
  return level
end

function Level:draw(level)
  Map = require level
  
  Map:create()
end

return Level
The exact error is: levelLoader.lua:16: '=' expected near 'Map'
Can someone help me fix this problem?

Thanks in advance,
- Xaaf Code

Re: Need help for my loader...

Posted: Wed Apr 26, 2017 12:59 pm
by OnACoffeeBreak
If you have level.lua file, try putting quotes around level parameter in the require command:

Code: Select all

  Map = require "level"
http://lua-users.org/wiki/ModulesTutorial

Re: Need help for my loader...

Posted: Wed Apr 26, 2017 1:53 pm
by XaafCode
I know, but I want that it requires the value of the variable level...

Re: Need help for my loader...

Posted: Wed Apr 26, 2017 2:03 pm
by Beelz
Require is not a magic word, it is a function. With functions as long as you only have one argument and it's a string, then you do not need parenthesis. If you use a variable or concatenate a string, you'll need to enclose it.

Re: Need help for my loader...

Posted: Wed Apr 26, 2017 2:51 pm
by zorg
So, to finish up the above info fragments, you want to do it like this:

Code: Select all

Map = require(level)

Re: Need help for my loader...

Posted: Wed Apr 26, 2017 3:13 pm
by Sir_Silver
I just wanted to say that that is some very strange code. You have your level object overriding what it is in the get method, which means that the level would lose the methods you've attached to it.

Also in your draw method, you are calling require and create map. That doesn't make any sense for something you would expect to be called in rendering.

Re: Need help for my loader...

Posted: Wed Apr 26, 2017 4:14 pm
by XaafCode
Thanks to all for replying, I have cleaned up and fixed my code now. Thanks! :D