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...).
Alright I have a basic code with the collision test
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:
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
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:
entity:set_modified_ground("deep_water")
for deep water, or
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.
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.
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