General discussion about LÖVE, Lua, game development, puns, and unicorns.
-
Bindie
- Party member
- Posts: 151
- Joined: Fri Jan 23, 2015 1:29 pm
Post
by Bindie » Wed May 06, 2015 6:53 pm
Hey, I have some require questions.
Do I have to assign different data files to local names? Like:
Best case would be if I could just
In love.load and then for example call player[1];
Code: Select all
function love.update()
player[1].x = 1
end
-
micha
- Inner party member
- Posts: 1083
- Joined: Wed Sep 26, 2012 5:13 pm
Post
by micha » Wed May 06, 2015 7:29 pm
No you don't have to assign require-calls to local variables, but you should do so.
When you call require, then the code in the required file is executed. If this code defines a global variable, then it will also exist in your normal program.
However, in most cases, it is bad practice to define global variable in external code files. That way different libraries could get coupled or break each other.
-
Muris
- Party member
- Posts: 131
- Joined: Fri May 23, 2014 9:18 am
Post
by Muris » Wed May 06, 2015 8:28 pm
I think if you use require, you need to return a value from the end of the lua file?
Also if you use require, I think you will get same object as a return value such as:
Code: Select all
-- something.lua
return { moo = 'yo' }
-- main.lua
local a = require 'something'
a.moo = "test" -- change the moo in the table to 'test'
local b = require 'something'
print(b.moo) -- prints test instead of yo
I bumped into this
issue feature when I tried to reset an object to the default values by just re-requiring it.
So it is good to know if you need to require the same file multiple times for whatever the reason, it only creates one instance.
-
s-ol
- Party member
- Posts: 1077
- Joined: Mon Sep 15, 2014 7:41 pm
- Location: Cologne, Germany
-
Contact:
Post
by s-ol » Wed May 06, 2015 9:27 pm
Muris wrote:I think if you use require, you need to return a value from the end of the lua file?
Also if you use require, I think you will get same object as a return value such as:
Code: Select all
-- something.lua
return { moo = 'yo' }
-- main.lua
local a = require 'something'
a.moo = "test" -- change the moo in the table to 'test'
local b = require 'something'
print(b.moo) -- prints test instead of yo
I bumped into this
issue feature when I tried to reset an object to the default values by just re-requiring it.
So it is good to know if you need to require the same file multiple times for whatever the reason, it only creates one instance.
Both correct, except for that you don't
need to return anything (but usually want).
-
Bindie
- Party member
- Posts: 151
- Joined: Fri Jan 23, 2015 1:29 pm
Post
by Bindie » Wed May 06, 2015 9:31 pm
Thanks, I try to be really ordered this time when making a game. Right now I'm requiring data for my player:
Code: Select all
require 'player'
require 'graphics'
In my player I have:
Code: Select all
player = {
{x, y, controls = {w,a,s,d}}
{x, y, controls = {w,a,s,d}}
}
To make it readable should I instead maybe:
local player = require 'player'
which then look like:
Code: Select all
data = {
{x, y, controls = {w,a,s,d}}
{x, y, controls = {w,a,s,d}}
}
So I can refere to:
-
Kingdaro
- Party member
- Posts: 395
- Joined: Sun Jul 18, 2010 3:08 am
Post
by Kingdaro » Thu May 07, 2015 3:45 am
That's still not quite right. What you'll want to do is localize the player inside player.lua then return it at the end as though it were a function on its own.
Code: Select all
-- player.lua
local player = {
{x = 0, y = 0, controls = {'w','a','s','d'} }
{x = 0, y = 0, controls = {'up', 'left', 'down', 'right'} }
}
return player
Then, from main.lua, you can do as you please.
Code: Select all
-- main.lua
local player = require 'player'
player[1].x = 100
-
Bindie
- Party member
- Posts: 151
- Joined: Fri Jan 23, 2015 1:29 pm
Post
by Bindie » Thu May 07, 2015 3:31 pm
Kingdaro wrote:That's still not quite right. What you'll want to do is localize the player inside player.lua then return it at the end as though it were a function on its own.
Code: Select all
-- player.lua
local player = {
{x = 0, y = 0, controls = {'w','a','s','d'} }
{x = 0, y = 0, controls = {'up', 'left', 'down', 'right'} }
}
return player
Then, from main.lua, you can do as you please.
Code: Select all
-- main.lua
local player = require 'player'
player[1].x = 100
I'll try that, when I was away I had an idea that requiring lua files with data without setting it to a local variable may not be bad however setting something like bump.lua or libraries using a local variable I totally see the point.
Would you explain the benefit of assigning the returned table to a local variable?
Is this in order to become ordered while making architecture preventing crashes, conflicts?
-
Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
-
Contact:
Post
by Robin » Thu May 07, 2015 4:50 pm
It's to try and prevent hidden dependencies. Suppose your modules all assign to global variable, then you have no idea what modules are loaded when looking at a specific module. So if module A depends on module B, but doesn't require it, and module C requires module B, then it works out if module C just happens to be loaded before module A. But suppose module C is no longer needed, or it is changed to not need module B, or it is decided for some reason module A needs to be loaded before module C, then it all breaks. That's why it's better to make your dependencies explicit, and assign all modules to local variables.
-
Bindie
- Party member
- Posts: 151
- Joined: Fri Jan 23, 2015 1:29 pm
Post
by Bindie » Thu May 07, 2015 7:13 pm
So when assigning files and libraries to local variables the do not depend on each other in the same way?
-
cohadar
- Prole
- Posts: 25
- Joined: Mon May 04, 2015 5:46 am
-
Contact:
Post
by cohadar » Fri May 08, 2015 3:09 am
Users browsing this forum: Bing [Bot] and 7 guests