Using timers in custom entitites

Started by Starlock, October 11, 2015, 05:22:37 AM

Previous topic - Next topic
Hey so I was trying to make a custom entity that restores health over time. The code basically works fine but the timer covers the entire map rather than just when traversing the entity. Is there a way to make it so the hero has to be on the entity for the timer to activate?

Also, how do i change the hero animation on the entity because set_animation doesnt seem to be working...

To do that, you can use a collision test or a sensor to start the timer, and then, check on each iteration if the hero is still over the entity (if not, you will need to stop the timer). (Also, be careful so you do not activate the timer twice.)

To change animation of hero, I would either freeze the hero and then change the animation, or change the tunic sprite temporarily (in this way you can walk with a new animation). Maybe you do not need to change the animation at all to show a restoring life effect (you could draw something above the hero instead, although that would not be the same...).
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Alright I have a basic code with the collision test 

Code ( lua) Select
local entity = ...
local map = entity:get_map()
local hero = map:get_entity("hero")
local game = entity:get_game()
local timer

function entity:on_created()
  self:create_sprite("entities/spring")
  self:set_size(16, 16)
  self:set_origin(16, 16)
  self:set_traversable_by(true)
end

entity:add_collision_test("overlapping", function(spring, other)
    if other:get_type() ~= "hero" then
      return
    end
sol.timer.start(1000, function()
  game:add_life(1)
  return true
  end)
end)


but not too sure on what to use for ending the timer when not on the collision

I would try something like this:
Code (lua) Select

local entity = ...

function entity:on_created()
  self:create_sprite("entities/spring")
  self:set_size(16, 16)
  self:set_origin(16, 16)
  self:set_traversable_by(true)
 
  local game = self:get_game()
  local hero = game:get_hero()
 
 
  sol.timer.start(1000, function()
    if entity:overlaps(hero) then
      game:add_life(1)
    end
    return true
  end)
 
end
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

That code works perfect and is way cleaner than mine was. Thanks.  ;D

but for adding an animation would I have to use set animation or get? The custom entity is meant to be for a hot spring so the player would be using a swim animation

If you just want a swimming animation or a shallow water animation, then it suffices to change either the ground of the custom entity, or the ground of the map under the custom entity. To change the ground of the custom entity you just need to add this line to the custom entity script:
Code (lua) Select

entity:set_modified_ground("deep_water")

for deep water, or
Code (lua) Select

entity:set_modified_ground("shallow_water")

for shallow water. If what you wanted is a different effect/animation on the hero, that might not be so simple as this.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

For the spring the player is meant to be able to go in without requiring the flippers so I don't think setting the ground to deep water would work and should just use the animation for swimming but to just be a regular traversable

I would use shallow water, if possible.

If that is not what you want, then you will probably need to enable the swimming ability, at least temporarily. Maybe other people of the forum can help you more.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

October 16, 2015, 07:55:07 PM #8 Last Edit: October 16, 2015, 10:19:05 PM by Starlock
Code ( lua) Select
local entity = ...

function entity:on_created()
  self:create_sprite("entities/spring")
  self:set_size(16, 16)
  self:set_origin(16, 16)
  self:set_traversable_by(true)
 
  local game = self:get_game()
  local hero = game:get_hero()
 
 
  sol.timer.start(1000, function()
    if entity:overlaps(hero) then
      game:add_life(1)
      hero:set_animation("plunging_water")
    end
    return true
  end)
 
end


This sort of does what I want but I'm not sure what needs to be done to make the animation work without being part of the timer function