Author Topic: [solved]Jump movement direction4 error?  (Read 908 times)

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: Jump movement direction4 error?
« Reply #15 on: June 24, 2017, 02:18:29 am »
I confirm the bug. I got the same problem when I checked this with a custom entity.

Diarandor

  • Hero Member
  • *****
  • Posts: 816
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Jump movement direction4 error?
« Reply #16 on: June 24, 2017, 06:09:37 am »
Sorry for my arrogance before, but I am under too much stress these days.
I have now tried the script directly (I didn't before, I had only read the code).

After "cleaning" the code a bit, it works, so I confirm that there is no bug (unless someone can prove that I am wrong  :P).
The problem is just that the script is not well coded, in the following parts:

I tested it with a custom_entity script. There is a line of code missing since "metatable_entity" is not defined, but I guess that it is
Code: Lua
  1. metatable_entity = ...

and in that case "metatable_entity" is not really a metatable in the formal sense (I think).

1) Your events on_interaction() and on_movement_changed() are not called the first time you create a jump, because they are defined inside your function "metatable_entity:jump". These events should be defined outside that function. Although the variable quick_movement is not defined outside the function "jump", you can still access the movement with entity:get_movement().

2) This part is really confusing and gives problems:
Code: Lua
  1.   sol.timer.start(450, function()
  2.  
  3.     function quick_movement:on_finished()
  4.       set_stop_aniamtion = true
  5.     end
  6.  
  7.     if set_stop_aniamtion == true then
  8.      sol.audio.play_sound(finish_jump_sound)
  9.                  self:get_sprite():set_animation(finish_animation) --finish_animation
  10.    return false
  11.     end
  12.  
  13.    return true
  14.   end)
  15.  
If your jump ends in less than 450 ms, the event quick_movement:on_finished() would never be called since it would be defined after the movement has ended. Actually you do not need a timer, and should probably write something like this instead of the above code:
Code: Lua
  1.     function quick_movement:on_finished()
  2.       sol.audio.play_sound(finish_jump_sound)
  3.       entity:get_sprite():set_animation(finish_animation)
  4.     end
  5.  

I can paste a copy of the modified script if you want. It works and the sprite of the custom entity is always facing the direction of the jump, which I believe is what you wanted (otherwise I did not understand your final purpose).

And I insist that having a clean code is more important that it may appear, not only for finding bugs, but also to make the code understandable to others who want to help you.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #17 on: June 24, 2017, 07:32:36 am »
Quote
2) This part is really confusing and gives problems:

Ah, I think you are correct about the timer. I will do some tests later. That would not prevent a facing direction change though.

Quote
1) Your events on_interaction() and on_movement_changed() are not called the first time you create a jump, because they are defined inside your function "metatable_entity:jump". These events should be defined outside that function. Although the variable quick_movement is not defined outside the function "jump", you can still access the movement with entity:get_movement().

Everything is defined in my "Entity Create Jump Method" and it works, so I am not sure that is the problem. The only difference is that function self:on_created() is inside the script and the sprite is created by the function instead of being picked from the custom entity.

Code: Lua
  1. -------------------------------------------
  2. --Entity Create Jump Method
  3. -------------------------------------------
  4. function metatable_entity:jump_create(sprite_directory, speed, traversable, set_can_traverse_hero, set_traversable_by_hero, ignore_obstacles, direction8, jump_distance, start_animation, finish_animation, finish_jump_sound, start_jump_sound, sound_timer, dialog, dialog_activation_distance)
  5.  
  6.   local map = self:get_map()
  7.   local hero = map:get_hero()
  8.   local game = map:get_game()
  9.   local sprite
  10.   local quick_movement
  11.  
  12.   --speed default value
  13.   if speed == nil then
  14.     speed = 40
  15.   end
  16.  
  17.   if traversable == nil then
  18.     traversable = false
  19.   end
  20.  
  21.   --By default the entity cannot traverse the hero
  22.   if set_can_traverse_hero == nil then
  23.     set_can_traverse_hero = false
  24.   end
  25.  
  26.   --By default the hero cannot traverse the entity.
  27.   if set_traversable_by_hero == nil then
  28.     set_traversable_by_hero = false
  29.   end
  30.  
  31.   --By default the entity does not ignore obstacles
  32.   if ignore_obstacles == nil then
  33.     ignore_obstacles = false
  34.   end
  35.  
  36.   if direction8 == nil then
  37.     direction8 = 0
  38.   end
  39.  
  40.   if jump_distance == nil then
  41.     jump_distance = 100
  42.   end
  43.  
  44.   --By default the sprite directory is the hero tunic1
  45.   if sprite_directory == nil then
  46.     sprite_directory = "main_heroes/Eldran"
  47.   end
  48.  
  49.   if start_animation == nil then
  50.     start_animation = "jumping"
  51.   end
  52.  
  53.   if finish_animation == nil then
  54.    finish_animation = "stopped"
  55.   end
  56.  
  57.   if start_jump_sound == nil then
  58.     start_jump_sound = "jump"
  59.   end
  60.  
  61.   if finish_jump_sound == nil then
  62.     finish_jump_sound = "jump"
  63.   end
  64.  
  65.   if sound_timer == nil then
  66.     sound_timer = 500
  67.   end
  68.  
  69.   --By default the welcome sign dialog is used.
  70.   if dialog == nil then
  71.     dialog = "welcome_sign"
  72.   end
  73.  
  74.   --dialog_activation_distance default value
  75.   if dialog_activation_distance == nil then
  76.     dialog_activation_distance = 20
  77.   end
  78.  
  79.   function self:on_created()
  80.     sprite = self:create_sprite(sprite_directory) -- sprite_directory
  81.     self:set_can_traverse("hero", set_can_traverse_hero) -- set_can_traverse_hero
  82.     self:set_traversable_by("hero", set_traversable_by_hero) -- set_traversable_by_hero
  83.     self:set_drawn_in_y_order(true)
  84.     self:set_traversable_by(traversable)
  85.  
  86.     quick_movement = sol.movement.create("jump")
  87.     self:get_sprite():set_animation(start_animation) -- animation
  88.     quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
  89.     quick_movement:set_direction8(direction8) --direction8
  90.     quick_movement:set_distance(jump_distance) -- distance
  91.     quick_movement:set_speed(speed) -- speed
  92.     quick_movement:start(self)
  93.  
  94.     sol.timer.start(sound_timer, function() --finish_time
  95.                  sol.audio.play_sound(start_jump_sound) --finish_animation
  96.           end)
  97.    
  98.   end
  99.  
  100.   local set_stop_aniamtion = false
  101.  
  102.   sol.timer.start(450, function()
  103.  
  104.     function quick_movement:on_finished()
  105.       set_stop_aniamtion = true
  106.     end
  107.  
  108.     if set_stop_aniamtion == true then
  109.      sol.audio.play_sound(finish_jump_sound)
  110.                  self:get_sprite():set_animation(finish_animation) --finish_animation
  111.    return false
  112.     end
  113.  
  114.    return true
  115.   end)
  116.  
  117.   function self:on_interaction()
  118.     local distance_check = hero:get_distance(self)
  119.  
  120.     if distance_check <= dialog_activation_distance then -- dialog_activation_distance
  121.       if hero:get_direction() == 0 then
  122.         self:set_direction(2)
  123.         map:get_game():start_dialog(dialog) -- dialog
  124.       end
  125.       if hero:get_direction() == 1 then
  126.         self:set_direction(3)
  127.         map:get_game():start_dialog(dialog) -- dialog
  128.       end
  129.       if hero:get_direction() == 2 then
  130.         self:set_direction(0)
  131.         map:get_game():start_dialog(dialog) -- dialog
  132.       end
  133.       if hero:get_direction() == 3 then
  134.         self:set_direction(1)
  135.         map:get_game():start_dialog(dialog) -- dialog
  136.       end
  137.     end
  138.   end
  139.  
  140.   function self:on_movement_changed()
  141.      sprite:set_direction(quick_movement:get_direction4())  
  142.   end
  143. end

Quote
I can paste a copy of the modified script if you want. It works and the sprite of the custom entity is always facing the direction of the jump, which I believe is what you wanted (otherwise I did not understand your final purpose).

I found the problem. The problem is that the facing direction will not change because function entity:on_movement_changed() is inside the function entity:on_created(). Taking it out results in correct direction change. That means it is not a bug I think. That is the only reason my script does not work for facing direction change.

I declared my function like this and that is why will not work.

function entity:on_created()
   entity:jump()
end

Try it out if you do not believe me.

Code: Lua
  1. local entity = ...
  2. local game = entity:get_game()
  3. local map = entity:get_map()
  4.  
  5. function entity:on_created()
  6.  
  7.     entity:set_drawn_in_y_order(true)
  8.     entity:set_traversable_by(true)
  9.  
  10.     quick_movement = sol.movement.create("jump")
  11.     entity:get_sprite():set_animation("jumping") -- animation
  12.     quick_movement:set_ignore_obstacles(true) -- ignore obstacles
  13.     quick_movement:set_direction8(2) --direction8
  14.     quick_movement:set_distance(100) -- distance
  15.     quick_movement:set_speed(100) -- speed
  16.     quick_movement:start(entity)
  17.  
  18.   function entity:on_movement_changed()
  19.      entity:set_direction(quick_movement:get_direction4())  
  20.   end
  21. end

I will go ahead and close the issue I reported.

Diarandor

  • Hero Member
  • *****
  • Posts: 816
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Jump movement direction4 error?
« Reply #18 on: June 24, 2017, 08:16:57 am »
I found the problem. The problem is that the facing direction will not change because function entity:on_movement_changed() is inside the function entity:on_created().
It is good to see that there is no engine bug this time and all is fixed. (The less bugs it has, the sooner we will have new features added by Chris.  ;D) Yes, we should close the github issue.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”