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

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
[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) [Select]
entity:jump_create() -- default jump sprite Eldran is created for this one.
and

Code: ( lua) [Select]
entity:jump() -- no sprite set to be created
The direction does not change even though I set:

Code: ( lua) [Select]
  function self:on_movement_changed()
     self:set_direction(quick_movement:get_direction4())   
  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) [Select]
   
  function self:on_created()
     sprite = self:create_sprite(sprite_directory) -- sprite_directory
  end

  function self:on_movement_changed()
     sprite:set_direction(quick_movement:get_direction4())   
  end

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) [Select]
  function self:on_movement_changed()
     self:set_direction(quick_movement:get_direction4())   
  end

My only solution was to manually check.
Code: ( lua) [Select]
if direction8 == 0 or direction8 == 1 then
    if self:get_direction() == 1 then -- up
      self:set_direction(0)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(0)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(0)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(0)
    end
  end

  if direction8 == 2 or direction8 == 3  then
    if self:get_direction() == 1 then -- up
      self:set_direction(1)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(1)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(1)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(1)
    end
  end

  if direction8 == 4 or direction8 == 6 then
    if self:get_direction() == 1 then -- up
      self:set_direction(2)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(2)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(2)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(2)
    end
  end

  if direction8 == 6 or direction8 == 7 then
    if self:get_direction() == 1 then -- up
      self:set_direction(3)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(3)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(3)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(3)
    end
  end

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • 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

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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) [Select]
-- Event called when the custom entity is initialized.
function entity:on_created()
  entity:jump()
end

Code: ( lua) [Select]
-------------------------------------------
--Entity Create Jump Method
-------------------------------------------
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)

  local map = self:get_map()
  local hero = map:get_hero()
  local game = map:get_game()
  local sprite
  local quick_movement

  --speed default value
  if speed == nil then
    speed = 40
  end

  if traversable == nil then
    traversable = false
  end

  --By default the entity cannot traverse the hero
  if set_can_traverse_hero == nil then
    set_can_traverse_hero = false
  end

  --By default the hero cannot traverse the entity.
  if set_traversable_by_hero == nil then
    set_traversable_by_hero = false
  end

  --By default the entity does not ignore obstacles
  if ignore_obstacles == nil then
    ignore_obstacles = false
  end

  if direction8 == nil then
    direction8 = 0
  end

  if jump_distance == nil then
    jump_distance = 100
  end

  --By default the sprite directory is the hero tunic1
  if sprite_directory == nil then
    sprite_directory = "main_heroes/Eldran"
  end

  if animation == nil then
    animation = "jumping"
  end

  if finish_animation == nil then
   finish_animation = "stopped"
  end

  if finish_time == nil then
    finish_time = 3000
  end

  if jump_sound == nil then
    jump_sound = "jump"
  end

  if finish_jump_sound == nil then
    finish_jump_sound = "jump"
  end

  if sound_timer == nil then
    sound_timer = 500
  end

  --By default the welcome sign dialog is used.
  if dialog == nil then
    dialog = "welcome_sign"
  end

  --dialog_activation_distance default value
  if dialog_activation_distance == nil then
    dialog_activation_distance = 20
  end

  function self:on_created()
    sprite = self:create_sprite(sprite_directory) -- sprite_directory
    self:set_can_traverse("hero", set_can_traverse_hero) -- set_can_traverse_hero
    self:set_traversable_by("hero", set_traversable_by_hero) -- set_traversable_by_hero
    self:set_drawn_in_y_order(true)
    self:set_traversable_by(traversable)

    quick_movement = sol.movement.create("jump")
    self:get_sprite():set_animation(animation) -- animation
    quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
    quick_movement:set_direction8(direction8) --direction8
    quick_movement:set_distance(jump_distance) -- distance
    quick_movement:set_speed(speed) -- speed
    quick_movement:start(self)

    sol.timer.start(sound_timer, function() --finish_time
sol.audio.play_sound(jump_sound) --finish_animation
  end)
   
  end

sol.timer.start(finish_time, function() --finish_time
     sol.audio.play_sound(finish_jump_sound)
self:get_sprite():set_animation(finish_animation) --finish_animation
end)

  function self:on_interaction()
    local distance_check = hero:get_distance(self)

    if distance_check <= dialog_activation_distance then -- dialog_activation_distance
      if hero:get_direction() == 0 then
        self:set_direction(2)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 1 then
        self:set_direction(3)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 2 then
        self:set_direction(0)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 3 then
        self:set_direction(1)
        map:get_game():start_dialog(dialog) -- dialog
      end
    end
  end

  function self:on_movement_changed()
     sprite:set_direction(quick_movement:get_direction4())   
  end
end

Code: ( lua) [Select]
-------------------------------------------
--Entity Jump Method
-------------------------------------------
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)

  local map = self:get_map()
  local hero = map:get_hero()
  local game = map:get_game()
  local quick_movement

  --speed default value
  if speed == nil then
    speed = 40
  end

  if traversable == nil then
    traversable = false
  end

  --By default the entity does not ignore obstacles
  if ignore_obstacles == nil then
    ignore_obstacles = false
  end

  if direction8 == nil then
    direction8 = 0
  end

  if jump_distance == nil then
    jump_distance = 100
  end

  if animation == nil then
    animation = "jumping"
  end

  if finish_animation == nil then
   finish_animation = "stopped"
  end

  if finish_time == nil then
    finish_time = 3000
  end

  if jump_sound == nil then
    jump_sound = "jump"
  end

  if finish_jump_sound == nil then
    finish_jump_sound = "jump"
  end

  if sound_timer == nil then
    sound_timer = 500
  end

  --By default the welcome sign dialog is used.
  if dialog == nil then
    dialog = "welcome_sign"
  end

  --dialog_activation_distance default value
  if dialog_activation_distance == nil then
    dialog_activation_distance = 20
  end

    self:set_drawn_in_y_order(true)
    self:set_traversable_by(traversable)

    quick_movement = sol.movement.create("jump")
    self:get_sprite():set_animation(animation) -- animation
    quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
    quick_movement:set_direction8(direction8) --direction8
    quick_movement:set_distance(jump_distance) -- distance
    quick_movement:set_speed(speed) -- speed
    quick_movement:start(self)

    sol.timer.start(sound_timer, function() --finish_time
sol.audio.play_sound(jump_sound) --finish_animation
  end)

sol.timer.start(finish_time, function() --finish_time
     sol.audio.play_sound(finish_jump_sound)
self:get_sprite():set_animation(finish_animation) --finish_animation
end)

  function self:on_interaction()
    local distance_check = hero:get_distance(self)

    if distance_check <= dialog_activation_distance then -- dialog_activation_distance
      if hero:get_direction() == 0 then
        self:set_direction(2)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 1 then
        self:set_direction(3)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 2 then
        self:set_direction(0)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 3 then
        self:set_direction(1)
        map:get_game():start_dialog(dialog) -- dialog
      end
    end
  end

  function self:on_movement_changed()
     self:set_direction(quick_movement:get_direction4())   
  end
end

The Fix:
Code: ( lua) [Select]
--Manual jump direction check
  if direction8 == 0 or direction8 == 1 then
    if self:get_direction() == 1 then -- up
      self:set_direction(0)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(0)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(0)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(0)
    end
  end

  if direction8 == 2 or direction8 == 3  then
    if self:get_direction() == 1 then -- up
      self:set_direction(1)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(1)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(1)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(1)
    end
  end

  if direction8 == 4 or direction8 == 6 then
    if self:get_direction() == 1 then -- up
      self:set_direction(2)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(2)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(2)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(2)
    end
  end

  if direction8 == 6 or direction8 == 7 then
    if self:get_direction() == 1 then -- up
      self:set_direction(3)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(3)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(3)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(3)
    end
  end

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • 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) [Select]
function self:on_movement_changed(movement)
  self:set_direction(movement:get_direction4())
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

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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.

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • 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

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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) [Select]
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) [Select]
-- Event called when the custom entity is initialized.
function entity:on_created()
  entity:jump()
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) [Select]
entity:jump_create()
You can change the directory.

Code: ( lua) [Select]
entity:jump_create("main_heroes/eldran")
The jump_create function creates the sprite with script instead.

Code: ( lua) [Select]
sprite = self:create_sprite(sprite_directory)

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • 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) [Select]
self:set_direction((2 + hero:get_direction()) % 4) -- Make the entity face the hero
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

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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) [Select]
  function self:on_movement_changed()
     self:set_direction(quick_movement:get_direction4())   
  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) [Select]
self:set_direction((2 + hero:get_direction()) % 4) -- Make the entity face the hero
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.

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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) [Select]
  function self:on_movement_changed()
     self:set_direction(quick_movement:get_direction4())   
  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) [Select]
  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) [Select]
  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.

Diarandor

  • Hero Member
  • *****
  • Posts: 1061
  • 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!!!)
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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) [Select]
-------------------------------------------
--Entity Create Jump Method
-------------------------------------------
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)

  local map = self:get_map()
  local hero = map:get_hero()
  local game = map:get_game()
  local sprite
  local quick_movement

  --speed default value
  if speed == nil then
    speed = 40
  end

  if traversable == nil then
    traversable = false
  end

  --By default the entity cannot traverse the hero
  if set_can_traverse_hero == nil then
    set_can_traverse_hero = false
  end

  --By default the hero cannot traverse the entity.
  if set_traversable_by_hero == nil then
    set_traversable_by_hero = false
  end

  --By default the entity does not ignore obstacles
  if ignore_obstacles == nil then
    ignore_obstacles = false
  end

  if direction8 == nil then
    direction8 = 0
  end

  if jump_distance == nil then
    jump_distance = 100
  end

  --By default the sprite directory is the hero tunic1
  if sprite_directory == nil then
    sprite_directory = "main_heroes/Eldran"
  end

  if start_animation == nil then
    start_animation = "jumping"
  end

  if finish_animation == nil then
   finish_animation = "stopped"
  end

  if start_jump_sound == nil then
    start_jump_sound = "jump"
  end

  if finish_jump_sound == nil then
    finish_jump_sound = "jump"
  end

  if sound_timer == nil then
    sound_timer = 500
  end

  --By default the welcome sign dialog is used.
  if dialog == nil then
    dialog = "welcome_sign"
  end

  --dialog_activation_distance default value
  if dialog_activation_distance == nil then
    dialog_activation_distance = 20
  end

  function self:on_created()
    sprite = self:create_sprite(sprite_directory) -- sprite_directory
    self:set_can_traverse("hero", set_can_traverse_hero) -- set_can_traverse_hero
    self:set_traversable_by("hero", set_traversable_by_hero) -- set_traversable_by_hero
    self:set_drawn_in_y_order(true)
    self:set_traversable_by(traversable)

    quick_movement = sol.movement.create("jump")
    self:get_sprite():set_animation(start_animation) -- animation
    quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
    quick_movement:set_direction8(direction8) --direction8
    quick_movement:set_distance(jump_distance) -- distance
    quick_movement:set_speed(speed) -- speed
    quick_movement:start(self)

    sol.timer.start(sound_timer, function() --finish_time
sol.audio.play_sound(start_jump_sound) --finish_animation
  end)
   
  end

  local set_stop_aniamtion = false

  sol.timer.start(450, function()

    function quick_movement:on_finished()
      set_stop_aniamtion = true
    end

    if set_stop_aniamtion == true then
     sol.audio.play_sound(finish_jump_sound)
self:get_sprite():set_animation(finish_animation) --finish_animation
   return false
    end

   return true
  end)

  function self:on_interaction()
    local distance_check = hero:get_distance(self)

    if distance_check <= dialog_activation_distance then -- dialog_activation_distance
      if hero:get_direction() == 0 then
        self:set_direction(2)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 1 then
        self:set_direction(3)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 2 then
        self:set_direction(0)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 3 then
        self:set_direction(1)
        map:get_game():start_dialog(dialog) -- dialog
      end
    end
  end

  function self:on_movement_changed()
     sprite:set_direction(quick_movement:get_direction4())   
  end
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) [Select]
-------------------------------------------
--Entity Jump Method
-------------------------------------------
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)

  local map = self:get_map()
  local hero = map:get_hero()
  local game = map:get_game()
  local quick_movement

  --speed default value
  if speed == nil then
    speed = 40
  end

  if traversable == nil then
    traversable = false
  end

  --By default the entity does not ignore obstacles
  if ignore_obstacles == nil then
    ignore_obstacles = false
  end

  if direction8 == nil then
    direction8 = 0
  end

  if jump_distance == nil then
    jump_distance = 100
  end

  if start_animation == nil then
    start_animation = "jumping"
  end

  if finish_animation == nil then
   finish_animation = "stopped"
  end

  if start_jump_sound == nil then
    start_jump_sound = "jump"
  end

  if finish_jump_sound == nil then
    finish_jump_sound = "jump"
  end

  if sound_timer == nil then
    sound_timer = 500
  end

  --By default the welcome sign dialog is used.
  if dialog == nil then
    dialog = "welcome_sign"
  end

  --dialog_activation_distance default value
  if dialog_activation_distance == nil then
    dialog_activation_distance = 20
  end

--[[
--Manual jump direction check
  if direction8 == 0 or direction8 == 1 then
    if self:get_direction() == 1 then -- up
      self:set_direction(0)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(0)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(0)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(0)
    end
  end

  if direction8 == 2 or direction8 == 3  then
    if self:get_direction() == 1 then -- up
      self:set_direction(1)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(1)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(1)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(1)
    end
  end

  if direction8 == 4 or direction8 == 6 then
    if self:get_direction() == 1 then -- up
      self:set_direction(2)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(2)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(2)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(2)
    end
  end

  if direction8 == 6 or direction8 == 7 then
    if self:get_direction() == 1 then -- up
      self:set_direction(3)
    end

    if self:get_direction() == 2 then -- left
      self:set_direction(3)
    end

    if self:get_direction() == 3 then -- down
      self:set_direction(3)   
    end

    if self:get_direction() == 0 then-- right
      self:set_direction(3)
    end
  end
--]]

    self:set_drawn_in_y_order(true)
    self:set_traversable_by(traversable)

    quick_movement = sol.movement.create("jump")
    self:get_sprite():set_animation(start_animation) -- animation
    quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
    quick_movement:set_direction8(direction8) --direction8
    quick_movement:set_distance(jump_distance) -- distance
    quick_movement:set_speed(speed) -- speed
    quick_movement:start(self)

    sol.timer.start(sound_timer, function() --finish_time
sol.audio.play_sound(start_jump_sound) --finish_animation
  end)

  local set_stop_aniamtion = false

  sol.timer.start(450, function()

    function quick_movement:on_finished()
      set_stop_aniamtion = true
    end

    if set_stop_aniamtion == true then
     sol.audio.play_sound(finish_jump_sound)
self:get_sprite():set_animation(finish_animation) --finish_animation
   return false
    end

   return true
  end)

  function self:on_interaction()
    local distance_check = hero:get_distance(self)

    if distance_check <= dialog_activation_distance then -- dialog_activation_distance
      if hero:get_direction() == 0 then
        self:set_direction(2)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 1 then
        self:set_direction(3)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 2 then
        self:set_direction(0)
        map:get_game():start_dialog(dialog) -- dialog
      end
      if hero:get_direction() == 3 then
        self:set_direction(1)
        map:get_game():start_dialog(dialog) -- dialog
      end
    end
  end

  function self:on_movement_changed()
     self:set_direction(quick_movement:get_direction4())   
  end
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.

Diarandor

  • Hero Member
  • *****
  • Posts: 1061
  • 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) [Select]
self:set_direction((hero:get_direction() + 2) % 4)
game:start_dialog(dialog)
(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) [Select]
  if speed == nil then
    speed = 40
  end
can be also written (in a shorter way) like
Code: (Lua) [Select]
  speed = speed or 40
(Check the "or" and "and" operators in the Lua maual for more info.)
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Diarandor

  • Hero Member
  • *****
  • Posts: 1061
  • 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) [Select]
  function self:on_movement_changed()
     print("Movement Direction: " .. quick_movement:get_direction4())
     print("Entity Initial Direction: " .. self:get_direction())
     self:set_direction(quick_movement:get_direction4())
     print("Entity Final Direction: " .. self:get_direction())
  end

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.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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) [Select]
function entity:on_created()
    self:set_drawn_in_y_order(true)
    self:set_traversable_by(traversable)
 
    quick_movement = sol.movement.create("jump")
    self:get_sprite():set_animation(start_animation) -- animation
    quick_movement:set_ignore_obstacles(ignore_obstacles) -- ignore obstacles
    quick_movement:set_direction8(direction8) --direction8
    quick_movement:set_distance(jump_distance) -- distance
    quick_movement:set_speed(speed) -- speed
    quick_movement:start(self)

  function self:on_movement_changed()
     self:set_direction(quick_movement:get_direction4())   
  end
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) [Select]
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.