Page 3 of 4

Re: LUNAR Lua+Narrative

Posted: Fri Aug 17, 2012 3:31 pm
by Echo

Code: Select all

save_icon.dragging = {active = false , diffX = 0 , diffY = 0}  
solved the problem, no more error messages BUT there is no effect when I try dragging and dropping the save icon.
how-come?

Re: LUNAR Lua+Narrative

Posted: Fri Aug 17, 2012 3:39 pm
by Nixola
I didn't even fully understand what you're trying to do ^^'
If you could upload a .love of Lunar in its current status I'd be able to help you better

Re: LUNAR Lua+Narrative

Posted: Fri Aug 17, 2012 5:07 pm
by Roland_Yonaba
Echo wrote:

Code: Select all

save_icon.dragging = {active = false , diffX = 0 , diffY = 0}  
solved the problem, no more error messages BUT there is no effect when I try dragging and dropping the save icon.
how-come?
For what it is worth, hope this will help figure out.
A very simple dragging. Just try to get how it works and try to replicate.

Code: Select all


function love.load()	
	collide = function(o1x,o1y,o1w,o1h,o2x,o2y,o2w,o2h)
		return not ((o2x > o1x + o1w) or (o2x + o2w < o1x) or
			       (o2y > o1y + o1h) or (o2y + o2h < o1y)) 
	end
	
	obj = { x = 10, y = 10, w = 50, h = 50}
	obj.onDragging = false
	obj.dPos = { x = obj.x, y = obj.y}
end

function love.update(dt)
	mx,my = love.mouse.getPosition()
	if obj.onDragging then
		obj.x = mx - obj.dPos.x
		obj.y = my - obj.dPos.y
	end
end

function love.draw()
	love.graphics.rectangle('line',obj.x,obj.y,obj.w,obj.h)
end

function love.mousepressed(x,y,button)
	if button == 'l' then
		obj.onDragging = collide(x,y,1,1,obj.x,obj.y,obj.w,obj.h)
		if obj.onDragging then
			obj.dPos.x = (x - obj.x)
			obj.dPos.y = (y - obj.y)			
		end
	end
end

function love.mousereleased(x,y,button)
	if button == 'l' then obj.onDragging = false end
end


Re: LUNAR Lua+Narrative

Posted: Fri Aug 17, 2012 5:40 pm
by Echo
I suspect that there is something wrong with this collision check after looking at yonabas code

Code: Select all

if ( button == "1" )
   and ( x > save_icon.x > x ) and ( x < save_icon.x + save_icon.width )
   and ( y > save_icon.y > y ) and ( y < save_icon.y + save_icon.height )
   then
   save_icon.dragging.active = true
       save_icon.dragging.diffX = ( x - save_icon.x )
       save_icon.dragging.diffY = ( y - save_icon.y )
   end
maybe is should be the other way around, like it checks the save_icon's x and y co-ordinates against the mouse x and y co-ordinates.

I have attached Lunar at it's current state as a .love file. Not much as of now but I think it will help you guys understand what I'm doing.
I'm a neat programmer so it should be clean to read

tell me if you find something weird.
.(^-^)(^_^)(^-^)(^_^)...(0_0).

Re: LUNAR Lua+Narrative

Posted: Fri Aug 17, 2012 6:27 pm
by Roland_Yonaba
Well, you do have several problems in your code.

First, your collision detection is mistyped.
When you write :

Code: Select all

a > b > c
Lua checks that a > b, and creates a boolean. Which result in:

Code: Select all

(true|false) > b
Next, it tries to compare that boolean with b. And it err, of course.
So, rewrite it as follows:

Code: Select all

  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
Second, in love.mousepressed and love.mousereleased, the button check string should be string "l" ( that is letter "l", instead of "1", string number 1).

Third, in your love.draw, draw the save_icon after drawing the GUI, or you will never see the dragging, even if it is actually working.

See the file attached, I've altered your source code.

Re: LUNAR Lua+Narrative

Posted: Fri Aug 17, 2012 8:13 pm
by Echo
Ah so it was l not 1. I think that was partially hard to tell because of the text in Notepad++

The rest where my mistake, I should learn to be more careful with programming. with time maybe.

Thanks again for the feedback.

One more thing related to the drag and drop.
How can I set an image to destroy itself once the mouse has been released.

Re: LUNAR Lua+Narrative

Posted: Fri Aug 17, 2012 8:37 pm
by Roland_Yonaba
Echo wrote: One more thing related to the drag and drop.
How can I set an image to destroy itself once the mouse has been released.
It could be as simple as:

Code: Select all

function love.draw()
	if save_icon.dragging.active then love.graphics.draw(save_icon.image,save_icon.x,save_icon.y) end
end
But actually, that won't be a nice solution.
IMHO.
Because, If I understood correctly, what you aim to do is let the use drag the save icon and let it drop at a very specific place.

So maybe this is what you need for :
You can state that, if the user click on the save icon, he can drag it as long as the left-mouse button remains down.
Then, if the user releases the left-mouse button:
- if the save_icon was not correctly superposed with the targetted location, then it should be reset to its original location.
- otherwise, the save icon 'drops', some saving logic is executed, a confirmation is displayed, then the save_icon is reset to its original location.

Re: LUNAR Lua+Narrative

Posted: Wed Aug 22, 2012 11:47 am
by Echo
I have a problem. It saves fine and the serialization is fine but when I try loading the game nothing happens.
I suspect that it was because the data is not being put back into its place when it loads ( if that makes sense )

I have attached the project since there might be something bigger that I'm not seeing.

To save drag the floppy disk over into the notepad
To load drag the floppy disk over into the canvas

When you save it will print the save directory overhead.

Re: LUNAR Lua+Narrative

Posted: Wed Aug 22, 2012 1:48 pm
by Roland_Yonaba
Echo wrote:It saves fine and the serialization is fine but when I try loading the game nothing happens.
Are you sure about that ?
Actually I got nothing in my save directory. Even though I got the confirmation message.
And that makes sense, because in your code you just display this confirmation message, but you're not calling the saving/loading function.
But this is not the real problem. The real deal is that your saving/loading functions actually do not exist.

In the entry point of your game, (in love.load I mean), you state this:

Code: Select all

  ...
  save = {}
  save.state = false
  ...
  load = {}
  load.state = false
   ...
   if ( save.state == true ) then
     function save(data)
     love.filesystem.write('sav',_tostring(data))   
     end	
   end
   if ( load.state == true ) then
      function load(file)
      return love.filesystem.load(file)()
      end
   end  
   ...
Well, do you actually realize that function save() and load() will never be created ?
See, love.load is a kind of entry point. This callback is executed only once.
When love goes through love.load callback, load.state and save.state are both false.
So save function and load function are not defined. And that's all.

Also, take into account that Löve works this way:

Code: Select all

run love.load() --only once

-- run in loop
while running do
   process_events()
   love.update()
   love.draw()
end
Well, not entirely true, but that's the general idea.

So, no matter how you change load.state and save.state to true later in the code. Functions named save and load will never ever be created, as Löve will not come back inside love.load.

Well, that's not all.
Even if you fix this, there's still a name clashing issue, as save cannot refer at the same time to a table AND a function.
Same problem for load.Thus, you will have to rename these functions.

So, you might want to consider this.
Hope this will help fixing the problem.

There are some other problems in your code. Not directly related to the actual issue, yet it must be fixed.
First, use proper indentation, as it makes your code easily readable.

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.

Re: LUNAR Lua+Narrative

Posted: Wed Aug 22, 2012 2:53 pm
by Nixola
You could use metatables to make save and load both a function and a table (not exactly actually, but it's ok):

Code: Select all

setmetatable(save, {__call = save_function})
setmetatable(load, {__call = load_function})
By putting this after the creation of the table and the function, you can call the tables load and save like if they were function, but they'll still be tables. If you don't understand this post either ignore it or ask for clarifications