Loading another script

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.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Loading another script

Post by MarekkPie »

yarickyarick wrote:I did not understand a bit and then n * n, VarB and the like ...
function A(n) just multiplies n by itself (n^2).
function B(n) calls function A with 5 (which returns 25) and adds n to it.
The return B(7) is called whenever you attempt to load the script.

So if "blabla == 100" then call the script and get the value of B(7)...in our case: 25 + 7 = 32.

I know a lot of people on here are from Europe. I'm surprised there isn't another Ukrainian here to help you out with the translation.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Loading another script

Post by MarekkPie »

So, I think to answer your original question if you did something like:

Code: Select all

-- In file script.lua
function callFunctions()
  A()
  B()
  C()
  D()
end

function A()
  -- stuff
end

function B()
  -- stuff
end

function C()
 -- stuff
end

function D()
  -- stuff
end

return callFunctions()
Then in main.lua you did:

Code: Select all

if blabla == 100 then
  require "script.lua"
end
You could run a bunch of other functions just by loading another script.
yarickyarick
Prole
Posts: 15
Joined: Fri Dec 30, 2011 9:48 pm
Contact:

Re: Loading another script

Post by yarickyarick »

MarekkPie wrote:
yarickyarick wrote:I did not understand a bit and then n * n, VarB and the like ...
function A(n) just multiplies n by itself (n^2).
function B(n) calls function A with 5 (which returns 25) and adds n to it.
The return B(7) is called whenever you attempt to load the script.

So if "blabla == 100" then call the script and get the value of B(7)...in our case: 25 + 7 = 32.



MarekkPie wrote:So, I think to answer your original question if you did something like:

Code: Select all

-- In file script.lua
function callFunctions()
  A()
  B()
  C()
  D()
end

function A()
  -- stuff
end

function B()
  -- stuff
end

function C()
 -- stuff
end

function D()
  -- stuff
end

return callFunctions()
Then in main.lua you did:

Code: Select all

if blabla == 100 then
  require "script.lua"
end
You could run a bunch of other functions just by loading another script.
Well, I talk to many European languages ​​...
But the English language I have never succumbed to ... to my surprise, English and Russian language almost identical, but the Ukrainian language (As for me a simplified version of the Russian language for Europeans ...) just did is very different from English ... And especially in your language does not give me time continuum of words
You have too many ...
Past from the future ...
Past from present, and so on ...

even as examples of the similarity of Russian and English:

December = Декабрь(Deckabr)
November = Ноябрь(Noyabr)
January = Январь(Janvar)
And so on ...

Well, I can hardly be so ...
I have many advanced features introduced ...
This is simply not normal mentally ... as for me just a computer can not survive every second check, for example, or clicked in some of those coordinates, and so on ...
If you click it private, a heavy load on the application after which the average PC will not sustain the load and simply shut down or burn out (Well, if you click "with the speed of light" and possibly burn Comp)
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Loading another script

Post by MarekkPie »

Yeah, I've heard the "tenses" (I think you called it the time continuum of words) is what confuses non-English speakers.

To eat, in all the tenses I can think of right now (it's 2 AM here):

He ate... (already done)
He has eaten... (also already done)
He will have eaten... (referring to some time in the future AFTER he is done)
He had eaten... (referring to the time in the past after he was done)
He eats... (is doing it now)
He will eat... (referring to some time in the future BEFORE he is done)

Anyway...did my second attempt answer your question?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Loading another script

Post by Robin »

A better solution is:

Code: Select all

--main.lua:
require 'script'

-- ....
if b == 100 then
     someFunction()
end

Code: Select all

--script.lua
function someFunction()
  --- put the script here
end
This way, the script is reusable. If you have other functions in script.lua, but them above or under someFunction(), not inside of it.
Help us help you: attach a .love.
yarickyarick
Prole
Posts: 15
Joined: Fri Dec 30, 2011 9:48 pm
Contact:

Re: Loading another script

Post by yarickyarick »

The hierarchy of the fact that I want to create one ...
Иерархия.png
Иерархия.png (13.69 KiB) Viewed 3452 times
Please do contact me in Skype, ICQ, jabber or Agent (Though he hardly you have, since the program greate Russian developer company mail.ru) to explain in detail I could the problem and complete source code as something that is impossible for me in this case ...
My skype: g.man024
My ICQ: 643754338
My Agent: yarickyarick@mail.ru
My jabber: yarickyarick@jabber.ru
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Loading another script

Post by miko »

nevon wrote:I think you're talking about require.
require() caches its result, so if you do:

Code: Select all

require('file.lua')
require('file.lua')
then the file will be read only once. This is on purpose, because require() is usually used to load pieces of code (like classes).

If you really want to make sure the file is read in every time, you should use dofile().
But that would be bad design, as other pointed out, it is better to read all files once as needed (to define some functions), and then execute different functions according to the game logic.

I think you are looking for a GameState solution: http://love2d.org/forums/viewtopic.php? ... =gamestate
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Loading another script

Post by bartbes »

miko wrote: you should use dofile().
Well no, couldn't be more wrong. love.filesystem.load is what is to be used, not dofile.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Loading another script

Post by miko »

bartbes wrote:
miko wrote: you should use dofile().
Well no, couldn't be more wrong. love.filesystem.load is what is to be used, not dofile.
Oh, right, dofile() would be for plain lua.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Loading another script

Post by coffee »

yarickyarick, I another guy that english isn't my native language and also came from Basic language style.

So my advice to you is that you need to rethink how to flow your code in LOVE. Don't think much in GoTo jumping style or the old Basic program running flow. That way don't work much here.

You need very quickly to learn that the fundamental flow of LOVE code are the callbacks http://love2d.org/wiki/Tutorial:Callback_Functions. The faster you inner this concept and also game-states the better you will know how to do things in LOVE
Post Reply

Who is online

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