Author Topic: [Solved]Bomb overlap assistance  (Read 332 times)

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
[Solved]Bomb overlap assistance
« on: July 21, 2018, 09:58:18 am »
Okay, I want an entity to be disabled when a bomb is placed on it. I am guessing that it is not working because the bomb is not created before the start of the map.
Code: Lua
  1. --bomb test
  2. sol.timer.start(1000, function()
  3.     if bomb_1:overlaps(test) then
  4.       test:set_enabled(false)
  5.     end
  6.   return true  -- To call the timer again (with the same delay).
  7. end)

Code: [Select]
Error: In on_started: [string "maps/soulia_forest/soulia_forest.lua"]:57: attempt to index local 'bomb_1' (a nil value)


Max

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Bomb overlap assistance
« Reply #1 on: July 21, 2018, 05:46:04 pm »
Yeah, I wouldn't put this test in map:on_started() necessarily. Is this something that only happens in this map? If so, maybe having a timer check over and over works, but it might make more sense to run the check code whenever you place a bomb.

Either way, what I would do is run some code that checks to see if both entities bomb and test both exist before trying to disable one. Try the method map:has_entities(), and if you get nil back, don't do the code.

If map:has_entitiy("bomb_1") and bomb_1:overlaps(test) then
...

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Bomb overlap assistance
« Reply #2 on: July 21, 2018, 08:37:48 pm »
I actually tried map:has_entities() and creating the bomb beforehand. I thought it was the timer, but that was not the case after some more testing.

Code: Lua
  1. local x, y, layer = hero:get_position()
  2.   local direction = hero:get_direction()
  3.   if direction == 0 then
  4.     x = x + 16
  5.   elseif direction == 1 then
  6.     y = y - 16
  7.   elseif direction == 2 then
  8.     x = x - 16
  9.   elseif direction == 3 then
  10.     y = y + 16
  11.   end
  12.  
  13.   map:create_bomb{
  14.     x = x,
  15.     y = y,
  16.     layer = layer
  17.   }
  18.  
  19. --bomb test
  20. sol.timer.start(1000, function()
  21.     if map:has_entitiy("bomb_1") and bomb_1:overlaps(test) then
  22.       test:set_enabled(false)
  23.     end
  24.   return true
  25. end)

Code: [Select]
Error: In on_started: [string "maps/soulia_forest/soulia_forest.lua"]:80: attempt to call method 'has_entitiy' (a nil value)
I also tried:

Code: Lua
  1. sol.timer.start(1000, function()
  2. for bomb in map:get_entities("bomb") do
  3.   if bomb_1:overlaps(test) then
  4.       test:set_enabled(false)
  5.   end
  6. end
  7.   return true
  8. end)

As well as...

Code: Lua
  1. sol.timer.start(1000, function()
  2.   if map:has_entities("bomb") and bomb_1:overlaps(test) then
  3.     test:set_enabled(false)
  4.   end
  5.   return true
  6. end)

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: [Solved]Bomb overlap assistance
« Reply #3 on: July 21, 2018, 11:38:01 pm »
Oh, this is a little hilarious. I forgot to add a name to the created entity. It works now.

Code: Lua
  1.   local x, y, layer = hero:get_position()
  2.   local direction = hero:get_direction()
  3.   if direction == 0 then
  4.     x = x + 16
  5.   elseif direction == 1 then
  6.     y = y - 16
  7.   elseif direction == 2 then
  8.     x = x - 16
  9.   elseif direction == 3 then
  10.     y = y + 16
  11.   end
  12.  
  13.   map:create_bomb{
  14.     name = "bomb",
  15.     x = x,
  16.     y = y,
  17.     layer = layer
  18.   }
  19.  
  20. --bomb test
  21. sol.timer.start(1000, function()
  22. if map:has_entities("bomb") and bomb:overlaps(test) then
  23.       test:set_enabled(false)
  24.     end
  25.   return true
  26. end)




Diarandor

  • Hero Member
  • *****
  • Posts: 1026
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: [Solved]Bomb overlap assistance
« Reply #4 on: July 22, 2018, 12:38:56 am »
An easy way to do this is to allow to define a custom event map.on_bomb_created(bomb_entity). You can call it, if it exists, from the bomb metatable (just define the function bomb_metatable.on_created).
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”