## LUNAR Lua+Narrative

Show off your games, demos and other (playable) creations.
Echo
Prole
Posts: 46
Joined: Fri Jul 13, 2012 4:50 pm
Location: Lucid Moon

### Re: LUNAR Lua+Narrative

Can I put the save and load function in love.update instead? and what are meta tables? Can I use plain variables like save_state and load_state instead of save.state and load.state.
Second, it is better to define once a collision check function and then reuse it, instead of repeating ;

CODE: SELECT ALL
if ... and ( save_icon.x > note.x ) and ( save_icon.x < note.x + note.width )
and ( save_icon.y > note.y ) and ( save_icon.y < note.y + note.height )

That is less verbose, and of course better.
What do you mean by that?
Are you saying I just toss the contents of the if statement into some variable and use that instead?
But I only check the collision once for the notepad and the canvas I actually never need to reuse the collision ever again, it is only for the floppy icon and nothing more. but I guess it might look cleaner or something...
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

### Re: LUNAR Lua+Narrative

Roland_Yonaba wrote: Second, it is better to define once a collision check function and then reuse it, instead of repeating ;

Code: Select all

  if ... and ( save_icon.x > note.x ) and ( save_icon.x < note.x + note.width )
and ( save_icon.y > note.y ) and ( save_icon.y < note.y + note.height )
Echo wrote: What do you mean by that?
Are you saying I just toss the contents of the if statement into some variable and use that instead?
Well, let's take (for instance) the code inside your save.lua .

Code: Select all

function love.mousepressed(x,y,button)
if ( button == "l" )
and ( x > save_icon.x ) and ( x < save_icon.x + save_icon.width )
and ( y > save_icon.y ) and ( y < save_icon.y + save_icon.height )
then
...
end
end

function love.update(dt)
...
end
--drag to notepad to save data
function love.mousereleased(x, y, button)
if ( button == "l" )
and ( save_icon.x > note.x ) and ( save_icon.x < note.x + note.width )
and ( save_icon.y > note.y ) and ( save_icon.y < note.y + note.height )
then
...
else
...
end
end
--drag to canvas to load data
if ( button == "l" )
and ( save_icon.x > canvas.x ) and ( save_icon.x < canvas.x + canvas.width )
and ( save_icon.y > canvas.y ) and ( save_icon.y < canvas.y + canvas.height )
then
...
else
if ( button == "l" ) then
...
end
end
end

My point is it is better to define a checkCollision function:

Code: Select all

function checkCollision(item1,item2)
return (( item1.x > item2.x ) and ( item1.x < item2.x + item2.width )
and ( item1.y > item2.y ) and ( item1.y < item2.y + item2.height ))
end

Then everywhere you need to test such a collision, it is now easy to write:

Code: Select all

if button =='l' and checkCollision(save_icon,canvas) then
...
end
Now, second point. Metatables are just simple tables.
But on the top of that, they allow to change the behaviour of tables.
What Nixola says is a brilliant idea.

Code: Select all

setmetatable(save, {__call = save_function})

Doing this, you assign to tables save and load two unnamed tables as their respective metatables.
The __call field is social here. What arises here is, when save (or load) which are actually tables, are called as functions, instead of getting a error, it will return a function, save_function or load_function.
It is quite magic and it works fine.
There's a very straightforward tutorial about metatables at Nova-fusion.

Practically, that corresponds to:

Code: Select all

save = { var = ..., var2 = ..., ...} -- a table
function save_function(...) ... end -- a function

setmetatable(save, {__call = save_function})

print(save.var) --> outputs save.var
save(...) --> Calls save_function(...), instead of yielding an error!

But, I won't second using it, if you are not familair with the concept of metatables.
It is way more simple to rename properly your save and load function.
Echo
Prole
Posts: 46
Joined: Fri Jul 13, 2012 4:50 pm
Location: Lucid Moon

### Re: LUNAR Lua+Narrative

I'm going to learn meta-tables and then use them because they seem to be very interesting, first I should use checkColliison to clean up my if statements and yes it is much better that way after all another reason I am doing this anyway is to learn Lua and real programming so I cant just dumb down the variables instead of taking the next step and learning something new like meta-tables.

Since I'm learning, I never really understood your _toString function or what it does and how it does it exactly

Code: Select all

   function _tostring(data)
local buf = 'return {'
for i,v in pairs(data) do
buf = buf .. i ..' = '..v..','
end
buf = buf .. '}'
return buf
end


Also when creating a function do you have to use the following systax?

Code: Select all

function myname
--statement
--return statement
end

It's a little confusing for me... I should porbably just go look for a tutorial on functions for Lua instead of bothering you guys but this is why I found _toString wierd.
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

### Re: LUNAR Lua+Narrative

Echo wrote:I'm going to learn meta-tables and then use them because they seem to be very interesting
Fine, it is up to you.
Echo wrote: another reason I am doing this anyway is to learn Lua and real programming so I cant just dumb down the variables instead of taking the next step and learning something new like meta-tables.
Well, I am just saying it is not really needed here.
But that's just a simple opinion.
Echo wrote: Since I'm learning, I never really understood your _toString function or what it does and how it does it exactly

Code: Select all

   function _tostring(data)
local buf = 'return {'
for i,v in pairs(data) do
buf = buf .. i ..' = '..v..','
end
buf = buf .. '}'
return buf
end

Well, in human language, it corresponds to:

Code: Select all

   function _tostring(data)
Create a local buffer to store a string (local, so that it won't be accessible outside this function)
Store "return {" inside this buffer

for each key, value in table (data) do
concatenate the buffer with all these : key, "=", value, ","
end for

concatenate the buffer with "}"
return the buffer

Echo wrote: Also when creating a function do you have to use the following systax?

Code: Select all

function myname
--statement
--return statement
end

Somehow. In Lua, functions receive arguments, or not, make some processing, and then return one or more results, or not.
All of theses a valid, for instance

Code: Select all

-- Takes nothing, returns nothing, just prints 'hello'
function a()
print('Hello')
end

-- Takes something, prints it, but returns nothing
function a(str)
print(str)
end

-- Takes something, capitalizes it, prints it, and returns it
function a(str)
str = str:upper()
print(str)
return str
end

-- Takes, nothing, do nothing and returns nothing
function foo() end
Echo wrote: I should porbably just go look for a tutorial on functions for Lua instead of bothering you guys but this is why I found _toString wierd.
Learning Lua, at least the basics, isn't that hard.
As for me, french guy, it was complicated cause I couldn't find french resources at first.
I sat in one day, opened PiL in my browser, and went through, while trying some samples. And was highly helpful.

Here is some good resources, ready to consume.
To me, here is your bible: PiL
Lots of interesting stuff here, too: Lua-Users Tutorial directory
And of course, the Ref manual (5.1, 5.2, though Löve features Lua 5.1, I guess)
Echo
Prole
Posts: 46
Joined: Fri Jul 13, 2012 4:50 pm
Location: Lucid Moon

### Re: LUNAR Lua+Narrative

I've been reading up PIL, so far most of it is making much more sense now but I'm about quarter way with PIL. might take a few months to soak up everything completely ( after PIL I'll revise the Wiki properly ) then after that I will resume with my Lunar project with much gained confidence and knowledge.
sorry for the inconvenience but I have put this on hiatus since In addition to this I am also currently moving to Cyprus this September 12th for collage so everything has been very busy and I will have much more priorities to take care of then ( since before I pretty much had nothing much to do with my time ) but I'll try save some time for this project and for Love2d.

until then and thanks for the great help so far (^_^)
Echo
Prole
Posts: 46
Joined: Fri Jul 13, 2012 4:50 pm
Location: Lucid Moon

### Re: LUNAR Lua+Narrative

Hi guys, I'm back.
Sorry for the long wait.
but things will move onward from here.

I'm now working on this same project with my sister, she's a great artist and we decided to make a Visual Novel together.
I also simplified the engine a lot now, right now its still in design but Its going to have a comic-book feel, no text boxes and
face-sets and I cut out the choices for later. Just a very simplistic and aesthetic visual novel engine for a linear story.

Later I can start thinking of adding in more complex features like choices, menu systems and maybe an editor mode but for now
those are just ambitions. I have a bad habit of thinking to big and not keeping up, but I'll tackle this one step and a time.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(UNRELATED: Oh and you should never go to North-Cyprus, Its very horrible and South Cyprus is facing some heavy financial difficulties, the whole Cyprus economy is falling apart.)

### Who is online

Users browsing this forum: No registered users and 21 guests