Author Topic: [Solved]on_removed() warning issue?  (Read 198 times)

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
[Solved]on_removed() warning issue?
« on: July 28, 2018, 12:48:17 am »
To begin with, this issue does not affect the game play at all and everything works. It is just an annoying warning.

I have this code on a different map.
Code: Lua
  1.   function slime:on_removed()
  2.     bookcase:set_enabled(false)
  3.   end

When I leave the map and go to a different map, I get a warning from the previous map: (I checked and "bookcase" does not exist on the new map.)
Code: [Select]
Error: In on_removed: [string "maps/chain_village/zark_house.lua"]:17: attempt to index global 'bookcase' (a nil value)
Please note: I did not remove the bookcase and it does the same thing when I tested it with a chest entity.

Code: Lua
  1.  function slime:on_removed()    
  2.     if not chest:is_open() then
  3.       sol.audio.play_sound("secret")
  4.     end
  5.  end

Code: [Select]
Error: In on_removed: [string "maps/chain_village/zark_house.lua"]:21: attempt to index global 'chest' (a nil value)

I get around the warning by using a save variable.

Code: Lua
  1.   function slime:on_removed()
  2.     if not game:get_value("zark_house_enemy_defeated") == true then
  3.       bookcase:set_enabled(false)
  4.     end
  5.    
  6.     if not game:get_value("zark_house_enemy_defeated") == true then
  7.       if not chest:is_open() then
  8.         sol.audio.play_sound("secret")
  9.       end
  10.     end
  11.  
  12.     game:set_value("zark_house_enemy_defeated",true)
  13.   end

Max

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: on_removed() warning issue?
« Reply #1 on: July 28, 2018, 01:49:43 am »
I've had this one too. When you're going to a new map, every entity on the old map is removed, because the old map doesn't exist anymore. If you define entity:on_removed() for any map entity, it'll be called when you change maps.

I don't know what you're calling on removed for, but it looks like you want the book case to deactivate when the slime is killed? Maybe use slime:on_dead() instead.

Or, you could do
Slime:on_removed
If map:has_entity(bookcase)
Then remove bookcase



Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: on_removed() warning issue?
« Reply #2 on: July 28, 2018, 02:30:00 am »
@Max
I think enemy:on_dead() is a better solution, but I still find it odd.

Diarandor

  • Hero Member
  • *****
  • Posts: 1020
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: on_removed() warning issue?
« Reply #3 on: July 28, 2018, 12:52:00 pm »
You should check if the entity still exists. In on_removed:
Code: Lua
  1. if bookcase ~= nil and bookcase:exists() then
  2.   bookcase:set_enabled(false)
  3. end
  4.  
“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: [Solved]on_removed() warning issue?
« Reply #4 on: July 28, 2018, 07:20:20 pm »
@Diarandor
Ah, I get it now. Thanks for clarifying Diarandor.

Since this is no way a bug. I will mark it as solved.