Animated sprites

Animated sprites

Postby Casimir » Wed Apr 10, 2019 8:41 pm

Is is possible to make animated sprites (for mobs)? The lua_api is confusing and I don't get it to work.
Also what are "LuaEntitySAO"? Can set_sprite/spritesheets still be used or are they broken?
Casimir
Member
 
Posts: 1165
Joined: Fri Aug 03, 2012 4:59 pm
GitHub: CasimirKaPazi

Re: Animated sprites

Postby v-rob » Thu Apr 11, 2019 11:41 pm

This is possible. I experimented around with it quite a while ago.

So, if you have an entity definition table called "my_entity", you would have to use self.object:set_sprite in one of the functions, like on_activate. In lua_api.txt, it is described as set_sprite(p, num_frames, framelength, select_horiz_by_yawpitch)

p is the coordinate of the first animation tile of the entity. Probably {x=0, y=0} in most cases, as this is the top left corner of the image file.
num_frames is the number of animation tiles there are. Obviously more than 1 if you want any animation at all.
framelength is how long each tile is shown.
select_horiz_by_yawpitch is an interesting option. If true, it allows you to make things like the dungeon master in the early games. So, if the player is facing the side of the dungeon master, you will see the vertical tiles a few rows over on the image.

Next, in the object properties, you have to have spritediv = {x=[width of spritesheet], y=[height of spritesheet]}. If you also have initial_sprite_basepos = {x=[number], y = [number]}, this will define the first sprite to be used in the sprite sheet, in case you want them to be animated starting with a different tile.

So, if you want to make an exact clone of the original dungeon master, your code would look something like this (only relevant parts included):

Image

Code: Select all
local my_entity = {
    initial_properties = {
        ...
        visual = "sprite",
        textures = {"dungeon_master.png"},
        spritediv = {x=6, y=5}, -- size of sheet
        initial_sprite_basepos = {x=0, y=0},
        ...
    },
    ...
}

function my_entity:on_activate(staticdata, dtime_s)
    self.object:set_sprite({x=0, y=0}, 3, 0.5, true)
end

minetest.register_entity("my_mod:my_entity", my_entity)


BTW, the background of the sprite has to be transparent, so don't include the red and yellow stuff in the image.

As a last note, this only works with sprites. Things like cubes or upright_sprites will not work with this. I don't know if it's possible to make those have animations.
v-rob
Member
 
Posts: 617
Joined: Thu Mar 24, 2016 3:19 am
GitHub: v-rob



Return to Modding Discussion



Who is online

Users browsing this forum: Yandex Bot [Bot] and 0 guests