[SOLVED] Require/local/global doubt

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
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

[SOLVED] Require/local/global doubt

Post by coffee »

Hi, still dealing with basic Lua mistakes. Can you help me understand this please?

So I have a main.lua

Code: Select all

require ("_lib/colors.lua")

function setColor(c,a)
   love.graphics.setColor(c[1], c[2], c[3], a)
end
and a colors.lua

Code: Select all

color.black = { 0,0,0 } 
if call in main.lua

Code: Select all

setColor( color.black )
give me a "attempt to index local "c" (nil value)
but all works fine if I add

Code: Select all

color.black = { 0,0,0 } 
to main.lua

This is a local/global problem right? What I'm doing wrong?
Last edited by coffee on Thu Dec 15, 2011 7:39 pm, edited 4 times in total.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: [Lua] Require/local/global doubt

Post by GijsB »

Code: Select all

require ("_lib/colors.lua")
are you sure that's the right path?
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: [Lua] Require/local/global doubt

Post by coffee »

GijsB wrote:

Code: Select all

require ("_lib/colors.lua")
are you sure that's the right path?
Sure, it would give me a "not found error" instead. I also tested that.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: [Lua] Require/local/global doubt

Post by coffee »

SOLVED!
I was doing

Code: Select all

color = {}
after the require and not before!
Stupid newbie! :death:
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: [SOLVED] Require/local/global doubt

Post by tentus »

Quick tip. You have this:

Code: Select all

require ("_lib/colors.lua")

You should get in the habit of doing it this way:

Code: Select all

require "_lib/colors"
The reason is that require() is going to be realigned to the Lua standard in Love 0.8.0, so require() won't support paths that include file extensions. I took out the parenthesis since they're unneeded, but that part's just an aesthetic change.
Kurosuke needs beta testers
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: [SOLVED] Require/local/global doubt

Post by osgeld »

heh funny, I have been doing lua systems so long I never even tried to pass an extension to love
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: [SOLVED] Require/local/global doubt

Post by bartbes »

tentus wrote: [/code]
You should get in the habit of doing it this way:

Code: Select all

require "_lib/colors"
But not. Proper syntax is:

Code: Select all

require("_lib.colors")
(and I like my parentheses, lisp represent)
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: [SOLVED] Require/local/global doubt

Post by coffee »

Thanks for the tip. There is only left few days till 0.8 is released and I didn't yet saw yet a OSX test binary that worked and run in my machine. So I probably will not use 0.8 so soon.

But I'm a bit shocked and I'm sure if understanding well. That means that I must then start use files without lua extension in requires? Does that not complicate OS/editor recognition of Lua files?

BTW I just use a named "_lib" dir/folder just only to be "placed" in Finder on top of other "normal" dirs (gfx, sfx, fnt...)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [SOLVED] Require/local/global doubt

Post by Robin »

coffee wrote:But I'm a bit shocked and I'm sure if understanding well. That means that I must then start use files without lua extension in requires? Does that not complicate OS/editor recognition of Lua files?
What? Why?

Unless you think that the actual files would have no extension, which is not the case.

To require a file called "foo.lua" in the "lib" folder, one does:

Code: Select all

require "lib.foo"
That's consistent with how Lua normally works.
Help us help you: attach a .love.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: [SOLVED] Require/local/global doubt

Post by coffee »

Robin wrote: To require a file called "foo.lua" in the "lib" folder, one does:

Code: Select all

require "lib.foo"
That's consistent with how Lua normally works.
Ah with that example I see clearly now. Thank you for clarify Robin. Ok, will take a bit to think that way about external files (even be the Lua way) but I understand now the new mechanics.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 228 guests