Author Topic: On_interaction error?  (Read 703 times)

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
On_interaction error?
« on: May 08, 2017, 12:08:12 pm »
I did the following with a custom entity, but the dialog is still called (if I keep pressing the spacebar) when the entity is far away unless I move the hero. How can I fix this?

Edit: This is probably a bug. A quick solaution is to check the distance with on_interaction() Here

Code: Lua
  1.   function self:on_interaction()
  2.     if hero:get_direction() == 0 then
  3.       self:set_direction(2)
  4.       map:get_game():start_dialog(dialog)
  5.     end
  6.     if hero:get_direction() == 1 then
  7.       self:set_direction(3)
  8.       map:get_game():start_dialog(dialog)
  9.     end
  10.     if hero:get_direction() == 2 then
  11.       self:set_direction(0)
  12.       map:get_game():start_dialog(dialog)
  13.     end
  14.     if hero:get_direction() == 3 then
  15.       self:set_direction(1)
  16.       map:get_game():start_dialog(dialog)
  17.     end
  18.   end

« Last Edit: May 09, 2017, 05:13:31 am by Zefk »

MetalZelda

  • Hero Member
  • *****
  • Posts: 507
    • View Profile
Re: On_interaction error?
« Reply #1 on: May 08, 2017, 02:02:11 pm »
Can you post the whole code

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #2 on: May 08, 2017, 03:51:26 pm »
Code: Lua
  1. function metatable_entity:wander(speed, traversable, ignore_obstacles, movement_type, sprite_directory, dialog)
  2.  
  3.   local map = self:get_map()
  4.   local hero = map:get_hero()
  5.   local sprite
  6.   local quick_movement
  7.  
  8.   function self:on_created()
  9.      sprite = self:create_sprite(sprite_directory)
  10.      self:set_can_traverse("hero", traversable)
  11.      self:set_traversable_by("hero", false)
  12.      self:set_drawn_in_y_order(true)
  13.  
  14.      quick_movement = sol.movement.create(movement_type)
  15.      quick_movement:set_ignore_obstacles(ignore_obstacles)
  16.      quick_movement:set_speed(speed)
  17.      quick_movement:start(self)
  18.   end
  19.  
  20.   function self:on_interaction()
  21.     if hero:get_direction() == 0 then
  22.       self:set_direction(2)
  23.       map:get_game():start_dialog(dialog)
  24.     end
  25.     if hero:get_direction() == 1 then
  26.       self:set_direction(3)
  27.       map:get_game():start_dialog(dialog)
  28.     end
  29.     if hero:get_direction() == 2 then
  30.       self:set_direction(0)
  31.       map:get_game():start_dialog(dialog)
  32.     end
  33.     if hero:get_direction() == 3 then
  34.       self:set_direction(1)
  35.       map:get_game():start_dialog(dialog)
  36.     end
  37.   end
  38.  
  39.   function self:on_movement_changed()
  40.      sprite:set_direction(quick_movement:get_direction4())  
  41.   end
  42. end

I declared it in a custom entity like this:

Code: Lua
  1. entity:wander(80, false, false, "random_path", "citizens/village_woman_6", "welcome_sign")
  2.  
« Last Edit: May 08, 2017, 03:55:52 pm by Zefk »

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #3 on: May 08, 2017, 06:39:41 pm »
In my opinion....It might be a bug. I did the following instead of on_interaction().

Code: Lua
  1.   --Create timer on check
  2.   timer = sol.timer.start(300, function()
  3.     if self.on_check ~= nil then
  4.       self:on_check()
  5.    return true
  6.     end
  7.   end)
  8.  
  9.   local test = false  
  10.   --check entity front and back layer and correct it.
  11.   function self:on_check()
  12.     local distance_check = hero:get_distance(self)
  13.  
  14.     if distance_check <= 16  and test == true then
  15.     print("pressed")
  16.     print(distance_check)
  17.       if hero:get_direction() == 0 then
  18.         self:set_direction(2)
  19.         map:get_game():start_dialog(dialog)
  20.       end
  21.       if hero:get_direction() == 1 then
  22.         self:set_direction(3)
  23.         map:get_game():start_dialog(dialog)
  24.       end
  25.       if hero:get_direction() == 2 then
  26.         self:set_direction(0)
  27.         map:get_game():start_dialog(dialog)
  28.       end
  29.       if hero:get_direction() == 3 then
  30.         self:set_direction(1)
  31.         map:get_game():start_dialog(dialog)
  32.       end
  33.     end
  34.  
  35.     function map:on_key_pressed(key)  
  36.       if key == "y" then
  37.         test = true
  38.       print("true")
  39.       else
  40.         test = false
  41.       end
  42.     end  

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #4 on: May 08, 2017, 08:06:03 pm »
A better solution and one that will work for more than one of the same entity is to check the distance with on_interaction().


Code: Lua
  1.   --Create timer on check
  2.   timer = sol.timer.start(300, function()
  3.     if self.on_check ~= nil then
  4.       self:on_check()
  5.    return true
  6.     end
  7.   end)
  8.  
  9.   --check entity front and back layer and correct it.
  10.   function self:on_check()
  11.     local distance_check = hero:get_distance(self)
  12.    function self:on_interaction()
  13.     if distance_check <= 20 then
  14.       if hero:get_direction() == 0 then
  15.         self:set_direction(2)
  16.         map:get_game():start_dialog(dialog)
  17.       end
  18.       if hero:get_direction() == 1 then
  19.         self:set_direction(3)
  20.         map:get_game():start_dialog(dialog)
  21.       end
  22.       if hero:get_direction() == 2 then
  23.         self:set_direction(0)
  24.         map:get_game():start_dialog(dialog)
  25.       end
  26.       if hero:get_direction() == 3 then
  27.         self:set_direction(1)
  28.         map:get_game():start_dialog(dialog)
  29.       end
  30.     end
  31.    end
  32.  end
« Last Edit: May 08, 2017, 08:08:29 pm by Zefk »

Diarandor

  • Hero Member
  • *****
  • Posts: 778
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: On_interaction error?
« Reply #5 on: May 08, 2017, 08:18:37 pm »
Which is the custom entity in your image above?
If it is a sign, maybe you should check if the hero is facing it, but I am not sure if that is really necessary.

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #6 on: May 08, 2017, 08:54:47 pm »
@Diarandor

The pink sprite in the upper left corner. Actually, it traveled all the way across the map and I was still able to activate the dialog as long as the hero did not move. I tested this in more than one place on the map.  Edit: I had to go up to the entity and interact with it before this error started.

Calculating the distance in the post above fixed the problem, but is the on_interaction() function suppose to work that way?

Code: Lua
  1.   function self:on_interaction()
  2.     local distance_check = hero:get_distance(self)
  3.  
  4.     if distance_check <= 20 then
  5.       if hero:get_direction() == 0 then
  6.         self:set_direction(2)
  7.         map:get_game():start_dialog(dialog)
  8.       end
  9.       if hero:get_direction() == 1 then
  10.         self:set_direction(3)
  11.         map:get_game():start_dialog(dialog)
  12.       end
  13.       if hero:get_direction() == 2 then
  14.         self:set_direction(0)
  15.         map:get_game():start_dialog(dialog)
  16.       end
  17.       if hero:get_direction() == 3 then
  18.         self:set_direction(1)
  19.         map:get_game():start_dialog(dialog)
  20.       end
  21.     end
  22.   end
« Last Edit: May 09, 2017, 01:40:42 am by Zefk »

Diarandor

  • Hero Member
  • *****
  • Posts: 778
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: On_interaction error?
« Reply #7 on: May 09, 2017, 02:23:10 am »
I am quite sure that on_interaction does not work in that way. I think that the hero must be facing the bounding box of the custom entity, and if that is the case, maybe the origin point of your custom entity sprite is wrong. Could you check if that is the problem?

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #8 on: May 09, 2017, 05:10:15 am »
The origin was the first thing I checked. Everything is fine in that area and on_interaction() works with my sprites as a NPC entity. (Just tested it again.)

I honestly think this is a bug, but it might not be related to distance or it could be. All I know is that a distance check prevents the bug from occurring for my custom_entity functions.
« Last Edit: May 09, 2017, 05:15:56 am by Zefk »

llamazing

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: On_interaction error?
« Reply #9 on: May 09, 2017, 05:22:24 am »
how big is your custom entity if you do entity:get_size()?

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #10 on: May 09, 2017, 05:29:40 am »
The output is 16 x 16

That was the second thing I checked.  ;D

I even tried manually setting it with self:set_size and self:set_origin. Same exact issue.

 

Diarandor

  • Hero Member
  • *****
  • Posts: 778
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: On_interaction error?
« Reply #11 on: May 09, 2017, 04:26:06 pm »
Just to be sure, have you checked both origin points? (There are 2, the one of the sprite and the one of the entity.)

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #12 on: May 09, 2017, 06:28:09 pm »
Indeed, I did check that. Also, the problem is not only with my sprite. It happens to other sprites as well in the sample_quest. 

As I mentioned before all sprites work with the NPC entity's on_interaction() function. I will point out what we covered.

Custom Entity On_interaction bug:

Solarus version: 1.5.0

If the hero interacts with a custom entity with the function on_interaction(), then the hero can activate the custom entity as long as the hero does not move. The custom entity can be 10,000+ pixels away and the hero can still activate it. If the hero moves, then this bug cancels.

Problems it is not: (I triple checked)
  • Not Origin
  • Not sprite size
  • Not second origin
  • Not second sprite size
  • Not a problem with set_size() and set_origin()
  • Not Code
  • Not Sprites

Current solution is a distance check while using the on_interaction() function for a custom entity.

Code: Lua
  1.   function self:on_interaction()
  2.     local distance_check = hero:get_distance(self)
  3.  
  4.     if distance_check <= 20 then
  5.       if hero:get_direction() == 0 then
  6.         self:set_direction(2)
  7.         map:get_game():start_dialog(dialog)
  8.       end
  9.       if hero:get_direction() == 1 then
  10.         self:set_direction(3)
  11.         map:get_game():start_dialog(dialog)
  12.       end
  13.       if hero:get_direction() == 2 then
  14.         self:set_direction(0)
  15.         map:get_game():start_dialog(dialog)
  16.       end
  17.       if hero:get_direction() == 3 then
  18.         self:set_direction(1)
  19.         map:get_game():start_dialog(dialog)
  20.       end
  21.     end
  22.   end
« Last Edit: May 09, 2017, 06:33:39 pm by Zefk »

Diarandor

  • Hero Member
  • *****
  • Posts: 778
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: On_interaction error?
« Reply #13 on: May 09, 2017, 11:32:58 pm »
So you are using Solarus v1.5.0. Try to use version 1.5.3, does it happen too?

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: On_interaction error?
« Reply #14 on: May 10, 2017, 01:00:55 am »
I just checked and it still happens in Solarus version 1.5.3.

Edit: I reported the bug: https://github.com/solarus-games/solarus/issues/1056

On another note. I think I found another bug unrelated to this post.
« Last Edit: May 10, 2017, 05:26:18 am by Zefk »