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

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
[solved]Jump movement direction4 error?
« on: May 10, 2017, 05:55:26 am »
I made the following two functions. The bug happens with my entity:jump function. It seems not to want to change the facing direction.

Code: Lua
  1. entity:jump_create() -- default jump sprite Eldran is created for this one.

and

Code: Lua
  1. entity:jump() -- no sprite set to be created

The direction does not change even though I set:

Code: Lua
  1.   function self:on_movement_changed()
  2.      self:set_direction(quick_movement:get_direction4())  
  3.   end

The problem only occurs if I do not manually create the sprite with script. (If I do not pick the sprite from the custom entity.)

Code: Lua
  1.    
  2.   function self:on_created()
  3.      sprite = self:create_sprite(sprite_directory) -- sprite_directory
  4.   end
  5.  
  6.   function self:on_movement_changed()
  7.      sprite:set_direction(quick_movement:get_direction4())  
  8.   end
  9.  

What makes no sense is I have other functions with identical code. For example, entity:wander() and entity:follow(). Both those functions use a sprite I picked from the custom entity and work perfectly.

I used "self:set_direction(quick_movement:get_direction4())" on both of those functions and they change direction correctly.

Code: Lua
  1.   function self:on_movement_changed()
  2.      self:set_direction(quick_movement:get_direction4())  
  3.   end

My only solution was to manually check.
Code: Lua
  1.  if direction8 == 0 or direction8 == 1 then
  2.     if self:get_direction() == 1 then -- up
  3.       self:set_direction(0)
  4.     end
  5.  
  6.     if self:get_direction() == 2 then -- left
  7.       self:set_direction(0)
  8.     end
  9.  
  10.     if self:get_direction() == 3 then -- down
  11.       self:set_direction(0)    
  12.     end
  13.  
  14.     if self:get_direction() == 0 then-- right
  15.       self:set_direction(0)
  16.     end
  17.   end
  18.  
  19.   if direction8 == 2 or direction8 == 3  then
  20.     if self:get_direction() == 1 then -- up
  21.       self:set_direction(1)
  22.     end
  23.  
  24.     if self:get_direction() == 2 then -- left
  25.       self:set_direction(1)
  26.     end
  27.  
  28.     if self:get_direction() == 3 then -- down
  29.       self:set_direction(1)    
  30.     end
  31.  
  32.     if self:get_direction() == 0 then-- right
  33.       self:set_direction(1)
  34.     end
  35.   end
  36.  
  37.   if direction8 == 4 or direction8 == 6 then
  38.     if self:get_direction() == 1 then -- up
  39.       self:set_direction(2)
  40.     end
  41.  
  42.     if self:get_direction() == 2 then -- left
  43.       self:set_direction(2)
  44.     end
  45.  
  46.     if self:get_direction() == 3 then -- down
  47.       self:set_direction(2)    
  48.     end
  49.  
  50.     if self:get_direction() == 0 then-- right
  51.       self:set_direction(2)
  52.     end
  53.   end
  54.  
  55.   if direction8 == 6 or direction8 == 7 then
  56.     if self:get_direction() == 1 then -- up
  57.       self:set_direction(3)
  58.     end
  59.  
  60.     if self:get_direction() == 2 then -- left
  61.       self:set_direction(3)
  62.     end
  63.  
  64.     if self:get_direction() == 3 then -- down
  65.       self:set_direction(3)    
  66.     end
  67.  
  68.     if self:get_direction() == 0 then-- right
  69.       self:set_direction(3)
  70.     end
  71.   end
« Last Edit: June 24, 2017, 07:39:16 am by Zefk »

MetalZelda

  • Hero Member
  • *****
  • Posts: 507
    • View Profile
Re: Jump movement direction4 error?
« Reply #1 on: May 10, 2017, 10:19:33 pm »
This is a bit unclear to me, can you upload the whole code ?

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #2 on: May 11, 2017, 05:44:00 am »
entity:jump_create() works, but entity:jump() does not. The only main different is entity:jump_create() makes the sprite with script and entity:jump() needs a sprite and on_created():

Code: Lua
  1. -- Event called when the custom entity is initialized.
  2. function entity:on_created()
  3.   entity:jump()
  4. end

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, animation, finish_animation, finish_time, finish_jump_sound, 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 animation == nil then
  50.     animation = "jumping"
  51.   end
  52.  
  53.   if finish_animation == nil then
  54.    finish_animation = "stopped"
  55.   end
  56.  
  57.   if finish_time == nil then
  58.     finish_time = 3000
  59.   end
  60.  
  61.   if jump_sound == nil then
  62.     jump_sound = "jump"
  63.   end
  64.  
  65.   if finish_jump_sound == nil then
  66.     finish_jump_sound = "jump"
  67.   end
  68.  
  69.   if sound_timer == nil then
  70.     sound_timer = 500
  71.   end
  72.  
  73.   --By default the welcome sign dialog is used.
  74.   if dialog == nil then
  75.     dialog = "welcome_sign"
  76.   end
  77.  
  78.   --dialog_activation_distance default value
  79.   if dialog_activation_distance == nil then
  80.     dialog_activation_distance = 20
  81.   end
  82.  
  83.   function self:on_created()
  84.     sprite = self:create_sprite(sprite_directory) -- sprite_directory
  85.     self:set_can_traverse("hero", set_can_traverse_hero) -- set_can_traverse_hero
  86.     self:set_traversable_by("hero", set_traversable_by_hero) -- set_traversable_by_hero
  87.     self:set_drawn_in_y_order(true)
  88.     self:set_traversable_by(traversable)
  89.  
  90.     quick_movement = sol.movement.create("jump")
  91.     self:get_sprite():set_animation(animation) -- animation
  92.     quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
  93.     quick_movement:set_direction8(direction8) --direction8
  94.     quick_movement:set_distance(jump_distance) -- distance
  95.     quick_movement:set_speed(speed) -- speed
  96.     quick_movement:start(self)
  97.  
  98.     sol.timer.start(sound_timer, function() --finish_time
  99.                  sol.audio.play_sound(jump_sound) --finish_animation
  100.           end)
  101.    
  102.   end
  103.  
  104.          sol.timer.start(finish_time, function() --finish_time
  105.      sol.audio.play_sound(finish_jump_sound)
  106.                  self:get_sprite():set_animation(finish_animation) --finish_animation
  107.          end)
  108.  
  109.   function self:on_interaction()
  110.     local distance_check = hero:get_distance(self)
  111.  
  112.     if distance_check <= dialog_activation_distance then -- dialog_activation_distance
  113.       if hero:get_direction() == 0 then
  114.         self:set_direction(2)
  115.         map:get_game():start_dialog(dialog) -- dialog
  116.       end
  117.       if hero:get_direction() == 1 then
  118.         self:set_direction(3)
  119.         map:get_game():start_dialog(dialog) -- dialog
  120.       end
  121.       if hero:get_direction() == 2 then
  122.         self:set_direction(0)
  123.         map:get_game():start_dialog(dialog) -- dialog
  124.       end
  125.       if hero:get_direction() == 3 then
  126.         self:set_direction(1)
  127.         map:get_game():start_dialog(dialog) -- dialog
  128.       end
  129.     end
  130.   end
  131.  
  132.   function self:on_movement_changed()
  133.      sprite:set_direction(quick_movement:get_direction4())  
  134.   end
  135. end

Code: Lua
  1. -------------------------------------------
  2. --Entity Jump Method
  3. -------------------------------------------
  4. function metatable_entity:jump(speed, traversable, ignore_obstacles, direction8, jump_distance, animation, finish_animation, finish_time, finish_jump_sound, 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 quick_movement
  10.  
  11.   --speed default value
  12.   if speed == nil then
  13.     speed = 40
  14.   end
  15.  
  16.   if traversable == nil then
  17.     traversable = false
  18.   end
  19.  
  20.   --By default the entity does not ignore obstacles
  21.   if ignore_obstacles == nil then
  22.     ignore_obstacles = false
  23.   end
  24.  
  25.   if direction8 == nil then
  26.     direction8 = 0
  27.   end
  28.  
  29.   if jump_distance == nil then
  30.     jump_distance = 100
  31.   end
  32.  
  33.   if animation == nil then
  34.     animation = "jumping"
  35.   end
  36.  
  37.   if finish_animation == nil then
  38.    finish_animation = "stopped"
  39.   end
  40.  
  41.   if finish_time == nil then
  42.     finish_time = 3000
  43.   end
  44.  
  45.   if jump_sound == nil then
  46.     jump_sound = "jump"
  47.   end
  48.  
  49.   if finish_jump_sound == nil then
  50.     finish_jump_sound = "jump"
  51.   end
  52.  
  53.   if sound_timer == nil then
  54.     sound_timer = 500
  55.   end
  56.  
  57.   --By default the welcome sign dialog is used.
  58.   if dialog == nil then
  59.     dialog = "welcome_sign"
  60.   end
  61.  
  62.   --dialog_activation_distance default value
  63.   if dialog_activation_distance == nil then
  64.     dialog_activation_distance = 20
  65.   end
  66.  
  67.     self:set_drawn_in_y_order(true)
  68.     self:set_traversable_by(traversable)
  69.  
  70.     quick_movement = sol.movement.create("jump")
  71.     self:get_sprite():set_animation(animation) -- animation
  72.     quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
  73.     quick_movement:set_direction8(direction8) --direction8
  74.     quick_movement:set_distance(jump_distance) -- distance
  75.     quick_movement:set_speed(speed) -- speed
  76.     quick_movement:start(self)
  77.  
  78.     sol.timer.start(sound_timer, function() --finish_time
  79.                  sol.audio.play_sound(jump_sound) --finish_animation
  80.           end)
  81.  
  82.          sol.timer.start(finish_time, function() --finish_time
  83.      sol.audio.play_sound(finish_jump_sound)
  84.                  self:get_sprite():set_animation(finish_animation) --finish_animation
  85.          end)
  86.  
  87.   function self:on_interaction()
  88.     local distance_check = hero:get_distance(self)
  89.  
  90.     if distance_check <= dialog_activation_distance then -- dialog_activation_distance
  91.       if hero:get_direction() == 0 then
  92.         self:set_direction(2)
  93.         map:get_game():start_dialog(dialog) -- dialog
  94.       end
  95.       if hero:get_direction() == 1 then
  96.         self:set_direction(3)
  97.         map:get_game():start_dialog(dialog) -- dialog
  98.       end
  99.       if hero:get_direction() == 2 then
  100.         self:set_direction(0)
  101.         map:get_game():start_dialog(dialog) -- dialog
  102.       end
  103.       if hero:get_direction() == 3 then
  104.         self:set_direction(1)
  105.         map:get_game():start_dialog(dialog) -- dialog
  106.       end
  107.     end
  108.   end
  109.  
  110.   function self:on_movement_changed()
  111.      self:set_direction(quick_movement:get_direction4())  
  112.   end
  113. end

The Fix:
Code: Lua
  1. --Manual jump direction check
  2.   if direction8 == 0 or direction8 == 1 then
  3.     if self:get_direction() == 1 then -- up
  4.       self:set_direction(0)
  5.     end
  6.  
  7.     if self:get_direction() == 2 then -- left
  8.       self:set_direction(0)
  9.     end
  10.  
  11.     if self:get_direction() == 3 then -- down
  12.       self:set_direction(0)    
  13.     end
  14.  
  15.     if self:get_direction() == 0 then-- right
  16.       self:set_direction(0)
  17.     end
  18.   end
  19.  
  20.   if direction8 == 2 or direction8 == 3  then
  21.     if self:get_direction() == 1 then -- up
  22.       self:set_direction(1)
  23.     end
  24.  
  25.     if self:get_direction() == 2 then -- left
  26.       self:set_direction(1)
  27.     end
  28.  
  29.     if self:get_direction() == 3 then -- down
  30.       self:set_direction(1)    
  31.     end
  32.  
  33.     if self:get_direction() == 0 then-- right
  34.       self:set_direction(1)
  35.     end
  36.   end
  37.  
  38.   if direction8 == 4 or direction8 == 6 then
  39.     if self:get_direction() == 1 then -- up
  40.       self:set_direction(2)
  41.     end
  42.  
  43.     if self:get_direction() == 2 then -- left
  44.       self:set_direction(2)
  45.     end
  46.  
  47.     if self:get_direction() == 3 then -- down
  48.       self:set_direction(2)    
  49.     end
  50.  
  51.     if self:get_direction() == 0 then-- right
  52.       self:set_direction(2)
  53.     end
  54.   end
  55.  
  56.   if direction8 == 6 or direction8 == 7 then
  57.     if self:get_direction() == 1 then -- up
  58.       self:set_direction(3)
  59.     end
  60.  
  61.     if self:get_direction() == 2 then -- left
  62.       self:set_direction(3)
  63.     end
  64.  
  65.     if self:get_direction() == 3 then -- down
  66.       self:set_direction(3)    
  67.     end
  68.  
  69.     if self:get_direction() == 0 then-- right
  70.       self:set_direction(3)
  71.     end
  72.   end

MetalZelda

  • Hero Member
  • *****
  • Posts: 507
    • View Profile
Re: Jump movement direction4 error?
« Reply #3 on: May 12, 2017, 05:53:55 pm »
http://www.solarus-games.org/doc/latest/lua_api_entity.html#lua_api_entity_on_movement_changed
Your approach is good but try with

Code: Lua
  1. function self:on_movement_changed(movement)
  2.   self:set_direction(movement:get_direction4())
  3. end

If it doesn't work, try
http://www.solarus-games.org/doc/latest/lua_api_movement.html#lua_api_movement_on_changed

Also, you are overwriting the on_interaction function, all custom entities with undefined on_interaction will have this "default" code. This might be related to http://forum.solarus-games.org/index.php/topic,974.0.html
« Last Edit: May 12, 2017, 06:02:28 pm by MetalZelda »

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #4 on: May 13, 2017, 03:18:28 am »
Quote
If it doesn't work, try
http://www.solarus-games.org/doc/latest/lua_api_movement.html#lua_api_movement_on_changed 

Does not work.

Quote
Also, you are overwriting the on_interaction function, all custom entities with undefined on_interaction will have this "default" code. This might be related to http://forum.solarus-games.org/index.php/topic,974.0.html

No matter how I test it. If the custom entity is able to wander off after an on_interaction, then I can activate it as long as the hero does not move. If the hero moves 1 step, then it cancels.  The fact that it cancels when the hero moves it super strange because my code has nothing to do with the hero movement.
« Last Edit: May 13, 2017, 03:23:12 am by Zefk »

MetalZelda

  • Hero Member
  • *****
  • Posts: 507
    • View Profile
Re: Jump movement direction4 error?
« Reply #5 on: May 13, 2017, 10:28:21 pm »
Can you send your project here ? I'll investigate this, weird issues there

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #6 on: May 14, 2017, 01:19:00 am »
I attached the jump_lib.lua. Put it in the script directory.

From your main.lua put the following at the top.

Code: Lua
  1. require("scripts/jump_lib.lua")

Pick a sprite from the custom entity and paste the following into a custom entity script. The sprite will need a "jumping" and "stopped" animation. Also, a sound called "jump". Using the sample_quest and Eldran will work. Remember to change the direction of the custom entity. It jumps "right" by default.

Code: Lua
  1. -- Event called when the custom entity is initialized.
  2. function entity:on_created()
  3.   entity:jump()
  4. end

I have tested this outside of my project. I am pretty sure you will get the same result. That is why I reported it as a bug. This only happens with the jump movement.

https://github.com/solarus-games/solarus/issues/1057

For entity:jump_create() all you gotta do is paste it into a custom entity. This is the function that works. It uses Eldran as the default sprite. main_heroes/eldran

Code: Lua
  1. entity:jump_create()

You can change the directory.

Code: Lua
  1. entity:jump_create("main_heroes/eldran")

The jump_create function creates the sprite with script instead.

Code: Lua
  1. sprite = self:create_sprite(sprite_directory)
« Last Edit: May 14, 2017, 01:57:12 am by Zefk »

MetalZelda

  • Hero Member
  • *****
  • Posts: 507
    • View Profile
Re: Jump movement direction4 error?
« Reply #7 on: May 14, 2017, 02:36:25 am »
What I see is, you try to get the direction4, which doesn't exist in the jump movement

You can only get direction8 from a jump movement, which is understandable

http://www.solarus-games.org/doc/latest/lua_api_jump_movement.html#lua_api_jump_movement_get_direction8

So then, to get a 4 direction way, simply divide by 2

Also in on_interaction, you can save a lot of space by doing

Code: Lua
  1. self:set_direction((2 + hero:get_direction()) % 4) -- Make the entity face the hero
  2. map:get_game():start_dialog(dialog) -- dialog

And i maintain, i don't get the on_interaction issue you're facing, so it comes from your code

If this doesn't solve the issue then it have something to do with your code, i'll investigate it tomorrow.
« Last Edit: May 14, 2017, 02:41:06 am by MetalZelda »

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #8 on: May 14, 2017, 03:01:56 am »
What I see is, you try to get the direction4, which doesn't exist in the jump movement

You can only get direction8 from a jump movement, which is understandable

http://www.solarus-games.org/doc/latest/lua_api_jump_movement.html#lua_api_jump_movement_get_direction8

So then, to get a 4 direction way, simply divide by 2

I do not understand what you mean. Doesn't the following script make the hero turn depending on the direction the movement is moving? The hero should still face the jumping direction.

Code: Lua
  1.   function self:on_movement_changed()
  2.      self:set_direction(quick_movement:get_direction4())  
  3.   end

Jump movements are particular movement objects. Therefore, they inherit all methods from the type movement.
http://www.solarus-games.org/doc/latest/lua_api_movement.html#lua_api_movement_get_direction4

0 will make the hero jump to the right with the direction8 movement. If I face the hero up from the custom entity and jump, then the hero jumps to the right while facing up. It does this for everything else. I understand that the direction8 is different from the direction4.

Quote
Also in on_interaction, you can save a lot of space by doing

Code: Lua
  1. self:set_direction((2 + hero:get_direction()) % 4) -- Make the entity face the hero
  2. map:get_game():start_dialog(dialog) -- dialog

Thanks for the tip. I will try it out sometime.

Quote
And i maintain, i don't get the on_interaction issue you're facing, so it comes from your code

If this doesn't solve the issue then it have something to do with your code, i'll investigate it tomorrow.

Did you try letting the custom entity move away from the hero after interacting with it? If you move the hero one step, then the bug cancels. If the custom entity is standing still, then you will never get the bug because you will move the hero. Also, this does not happen with an NPC entity. This is kinda off topic.
« Last Edit: May 14, 2017, 03:27:36 am by Zefk »

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #9 on: May 14, 2017, 03:51:18 am »
For example, If I set the direction8 to 2 for sprite to jump up and I face him down from the custom entity, then the following script should make him face up.

Maybe a visual explanation will help.


I use this script to change his direction, but it does not work.
Code: Lua
  1.   function self:on_movement_changed()
  2.      self:set_direction(quick_movement:get_direction4())  
  3.   end


1.I set direction8 to 0 for the hero to jump to the right. From the entity I face the entity up. He will jump right while facing up.

Code: Lua
  1.   entity:jump(speed, traversable, ignore_obstacles, 0, jump_distance, start_animation, finish_animation, finish_jump_sound, start_jump_sound, sound_timer, dialog, dialog_activation_distance)

2.I set the direction8 to 2 for the hero to jump up. From the entity I face the entity right. He will jump up while facing right.

Code: Lua
  1.   entity:jump(speed, traversable, ignore_obstacles, 2, jump_distance, start_animation, finish_animation, finish_jump_sound, start_jump_sound, sound_timer, dialog, dialog_activation_distance)

My entity:jump_create() function works. It is 95% identical to entity:jump(). The major difference with entity:jump_create() is that it creates the sprite by script.  I believe that using a sprite from the custom entity with the jump movement causes a facing direction bug.
« Last Edit: May 16, 2017, 03:17:38 pm by Zefk »

Diarandor

  • Hero Member
  • *****
  • Posts: 778
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Jump movement direction4 error?
« Reply #10 on: June 23, 2017, 07:07:39 am »
Could you post the full code of each script again? (The newest version.)

Also, indicate the filename of each script above its code. Then, if you want to comment something about the code, you only need to give the script name and the line number. (Copy-pasting isolated pieces of code does not help at all!!!)

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #11 on: June 23, 2017, 09:09:53 am »
I believe the problem is when picking the sprite from the custom entity. it seems to ignore directional change. The "Entity Create Jump Method" creates the sprite with script and it works. That is the only difference between the two functions.

1. Entity Create Jump Method = make sprite with script
2. Entity Jump Method = pick sprite from custom entity GUI

Entity Create Jump Method
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

The following function is what does not work correctly. The above function is almost identical. This function does not work without the code I commented out.

Entity Jump Method
Code: Lua
  1. -------------------------------------------
  2. --Entity Jump Method
  3. -------------------------------------------
  4. function metatable_entity:jump(speed, traversable, 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 quick_movement
  10.  
  11.   --speed default value
  12.   if speed == nil then
  13.     speed = 40
  14.   end
  15.  
  16.   if traversable == nil then
  17.     traversable = false
  18.   end
  19.  
  20.   --By default the entity does not ignore obstacles
  21.   if ignore_obstacles == nil then
  22.     ignore_obstacles = false
  23.   end
  24.  
  25.   if direction8 == nil then
  26.     direction8 = 0
  27.   end
  28.  
  29.   if jump_distance == nil then
  30.     jump_distance = 100
  31.   end
  32.  
  33.   if start_animation == nil then
  34.     start_animation = "jumping"
  35.   end
  36.  
  37.   if finish_animation == nil then
  38.    finish_animation = "stopped"
  39.   end
  40.  
  41.   if start_jump_sound == nil then
  42.     start_jump_sound = "jump"
  43.   end
  44.  
  45.   if finish_jump_sound == nil then
  46.     finish_jump_sound = "jump"
  47.   end
  48.  
  49.   if sound_timer == nil then
  50.     sound_timer = 500
  51.   end
  52.  
  53.   --By default the welcome sign dialog is used.
  54.   if dialog == nil then
  55.     dialog = "welcome_sign"
  56.   end
  57.  
  58.   --dialog_activation_distance default value
  59.   if dialog_activation_distance == nil then
  60.     dialog_activation_distance = 20
  61.   end
  62.  
  63. --[[
  64. --Manual jump direction check
  65.   if direction8 == 0 or direction8 == 1 then
  66.     if self:get_direction() == 1 then -- up
  67.       self:set_direction(0)
  68.     end
  69.  
  70.     if self:get_direction() == 2 then -- left
  71.       self:set_direction(0)
  72.     end
  73.  
  74.     if self:get_direction() == 3 then -- down
  75.       self:set_direction(0)    
  76.     end
  77.  
  78.     if self:get_direction() == 0 then-- right
  79.       self:set_direction(0)
  80.     end
  81.   end
  82.  
  83.   if direction8 == 2 or direction8 == 3  then
  84.     if self:get_direction() == 1 then -- up
  85.       self:set_direction(1)
  86.     end
  87.  
  88.     if self:get_direction() == 2 then -- left
  89.       self:set_direction(1)
  90.     end
  91.  
  92.     if self:get_direction() == 3 then -- down
  93.       self:set_direction(1)    
  94.     end
  95.  
  96.     if self:get_direction() == 0 then-- right
  97.       self:set_direction(1)
  98.     end
  99.   end
  100.  
  101.   if direction8 == 4 or direction8 == 6 then
  102.     if self:get_direction() == 1 then -- up
  103.       self:set_direction(2)
  104.     end
  105.  
  106.     if self:get_direction() == 2 then -- left
  107.       self:set_direction(2)
  108.     end
  109.  
  110.     if self:get_direction() == 3 then -- down
  111.       self:set_direction(2)    
  112.     end
  113.  
  114.     if self:get_direction() == 0 then-- right
  115.       self:set_direction(2)
  116.     end
  117.   end
  118.  
  119.   if direction8 == 6 or direction8 == 7 then
  120.     if self:get_direction() == 1 then -- up
  121.       self:set_direction(3)
  122.     end
  123.  
  124.     if self:get_direction() == 2 then -- left
  125.       self:set_direction(3)
  126.     end
  127.  
  128.     if self:get_direction() == 3 then -- down
  129.       self:set_direction(3)    
  130.     end
  131.  
  132.     if self:get_direction() == 0 then-- right
  133.       self:set_direction(3)
  134.     end
  135.   end
  136. --]]
  137.  
  138.     self:set_drawn_in_y_order(true)
  139.     self:set_traversable_by(traversable)
  140.  
  141.     quick_movement = sol.movement.create("jump")
  142.     self:get_sprite():set_animation(start_animation) -- animation
  143.     quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
  144.     quick_movement:set_direction8(direction8) --direction8
  145.     quick_movement:set_distance(jump_distance) -- distance
  146.     quick_movement:set_speed(speed) -- speed
  147.     quick_movement:start(self)
  148.  
  149.     sol.timer.start(sound_timer, function() --finish_time
  150.                  sol.audio.play_sound(start_jump_sound) --finish_animation
  151.           end)
  152.  
  153.   local set_stop_aniamtion = false
  154.  
  155.   sol.timer.start(450, function()
  156.  
  157.     function quick_movement:on_finished()
  158.       set_stop_aniamtion = true
  159.     end
  160.  
  161.     if set_stop_aniamtion == true then
  162.      sol.audio.play_sound(finish_jump_sound)
  163.                  self:get_sprite():set_animation(finish_animation) --finish_animation
  164.    return false
  165.     end
  166.  
  167.    return true
  168.   end)
  169.  
  170.   function self:on_interaction()
  171.     local distance_check = hero:get_distance(self)
  172.  
  173.     if distance_check <= dialog_activation_distance then -- dialog_activation_distance
  174.       if hero:get_direction() == 0 then
  175.         self:set_direction(2)
  176.         map:get_game():start_dialog(dialog) -- dialog
  177.       end
  178.       if hero:get_direction() == 1 then
  179.         self:set_direction(3)
  180.         map:get_game():start_dialog(dialog) -- dialog
  181.       end
  182.       if hero:get_direction() == 2 then
  183.         self:set_direction(0)
  184.         map:get_game():start_dialog(dialog) -- dialog
  185.       end
  186.       if hero:get_direction() == 3 then
  187.         self:set_direction(1)
  188.         map:get_game():start_dialog(dialog) -- dialog
  189.       end
  190.     end
  191.   end
  192.  
  193.   function self:on_movement_changed()
  194.      self:set_direction(quick_movement:get_direction4())  
  195.   end
  196. end

Usage directions:
http://forum.solarus-games.org/index.php/topic,976.msg5677.html#msg5677

P.S. I plan to use tables for easier usage in the future.
« Last Edit: June 23, 2017, 09:16:24 am by Zefk »

Diarandor

  • Hero Member
  • *****
  • Posts: 778
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Jump movement direction4 error?
« Reply #12 on: June 23, 2017, 05:42:19 pm »
I will try to find out what is the problem.

Anyway, some advices that you should take seriously into account to write better code:

1) This was already pointed out by MetalZelda but you did not change it:
Lines 121-136 of your "Entity Create Jump Method" and lines 174-189 of your "Entity Jump Method" can be shortened in a clearer way with only 2 lines:
Code: Lua
  1. self:set_direction((hero:get_direction() + 2) % 4)
  2. game:start_dialog(dialog)
  3.  
(You were repeating too much code unnecessarily.) If you don't understand the math, just ask.

2) This is just a matter of taste(not so important), but lines like:
Code: Lua
  1.   if speed == nil then
  2.     speed = 40
  3.   end
  4.  
can be also written (in a shorter way) like
Code: Lua
  1.   speed = speed or 40
  2.  
(Check the "or" and "and" operators in the Lua maual for more info.)

Diarandor

  • Hero Member
  • *****
  • Posts: 778
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Jump movement direction4 error?
« Reply #13 on: June 23, 2017, 07:23:53 pm »
I cannot understand anything. You did not explain properly what you are doing and what is the problem. And there is too much unnecessary information in the explanations.

-The image does not help at all. We cannot even know which entity is the hero, or what you are doing.
-In your explanations I cannot understand "what" is facing "what". All is unclear.
-You should learn to debug properly before we get crazy with your code.

Try this function instead:
Code: Lua
  1.   function self:on_movement_changed()
  2.      print("Movement Direction: " .. quick_movement:get_direction4())
  3.      print("Entity Initial Direction: " .. self:get_direction())
  4.      self:set_direction(quick_movement:get_direction4())
  5.      print("Entity Final Direction: " .. self:get_direction())
  6.   end
  7.  

Now, tell us the printed values that you get in the console, and what really happens in the game when you test it.
(We need more information. But only the useful one.)
I have tested the function custom_entity:set_direction() and it works perfectly for me, at least with the main sprite, so I cannot find functions that work in a wrong way as you say.

If you can write a new script with the MINIMAL and SIMPLEST code necessary to reproduce the bug, that would help a lot.

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Jump movement direction4 error?
« Reply #14 on: June 24, 2017, 01:37:20 am »
If you cannot figure it out by trying my script yourself, then I do not know what to tell you. The script is very simple in my opinion. It is just a bunch of defaults,  self properties, and movement functions. That is if we ignore the stop/start jump sound and interaction part of the script.

The only thing that needs to be looked at is this:
(I would suggest making a custom entity and apply a jump movement to it.)

Code: Lua
  1. function entity:on_created()
  2.     self:set_drawn_in_y_order(true)
  3.     self:set_traversable_by(traversable)
  4.  
  5.     quick_movement = sol.movement.create("jump")
  6.     self:get_sprite():set_animation(start_animation) -- animation
  7.     quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
  8.     quick_movement:set_direction8(direction8) --direction8
  9.     quick_movement:set_distance(jump_distance) -- distance
  10.     quick_movement:set_speed(speed) -- speed
  11.     quick_movement:start(self)
  12.  
  13.   function self:on_movement_changed()
  14.      self:set_direction(quick_movement:get_direction4())  
  15.   end
  16. end

The problem lies with picking the sprite from the custom entity. If this is done, then the custom entity will not face the proper direction when jumping.


If the custom entity sprite is created with code, then the custom entity faces the proper direction. That is all I know.
Code: Lua
  1.  sprite = self:create_sprite(sprite_directory) -- sprite_directory

Why would "only" making the custom entity sprite from script work for the jump movement? I do not understand why the sprite will not face the proper direction when it is picked from the custom entity. That is literally the only major difference between the two scripts I posted previously. I only get this facing direction problem with the "jump" movement. That is why I think it is a bug.

Note:
I did not make this post for the following. (I do however appreciate the tips and advice because I will change my code later.)
1. Report an issue for a debug lesson
2. Shorten my code
3. Shorten the temporary fix.
« Last Edit: June 24, 2017, 02:06:32 am by Zefk »