Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(5 edits) (+1)

Is it possible for a contraption to change its own size? I'm creating an animated sprite contraption that gets the sprite width and height from its attributes, and then shows an animation taken from a sprite sheet card.

I tried to do

me.size:(sprite_width.text,sprite_height.text)

on the view event, but it doesn't do anything when the contraption is running on a card (it does change its size when running inside the prototype view). I'm saving the state in hidden fields inside the prototype. Here's the whole script:

on get_spritesheet do sprite_sheet_card.text end
on set_spritesheet x do sprite_sheet_card.text: x end
on get_frames do frames.text+0 end
on set_frames x do frames.text:x end
on get_sprite_width do sprite_width.text+0 end
on set_sprite_width x do sprite_width.text:x end
on get_sprite_height do sprite_height.text+0 end
on set_sprite_height x do sprite_height.text:x end
on get_fps do fps.text+0 end
on set_fps x do fps.text:x end
on get_loop do loop.text+0 end
on set_loop x do loop.text:x end
on get_current_frame do current_frame.text+0 end
on set_current_frame x do current_frame.text:x end
on view do
 me.size:(sprite_width.text,sprite_height.text)
 c:deck.cards[sprite_sheet_card.text]
 w:c.image.size[0]
 h:c.image.size[1]
 f:current_frame.text
 i:c.image
 me.image.paste[i.copy[(w%sprite_width.text*floor f,floor sprite_width.text*floor f / w) (sprite_width.text,sprite_height.text)]]
 current_frame.text:frames.text%f+fps.text*1/60
end

The only thing I'm missing is for the contraption to resize itself to fit the configured sprite size.

EDIT: I made the prototype resizable, and it's working now. The problem is that the state is shared between different instances of the same prototype, so I'm obviously doing something very wrong here.

(+1)

Don't worry- it's quite normal to stash contraption state in auxiliary hidden widgets. The contents of such widgets is distinct between contraption instances, but the background image of contraptions is shared among all instances; pasting directly onto the contraption background will cause issues if you have more than one sprite. The conventional solution would be to add a canvas widget to the contraption and draw on that instead. You'll also need to configure non-zero margins for the contraption to ensure that the canvas automatically stretches and resizes properly along with it. Both the contraption instance and the canvas will need to be set to "show transparent" if you want objects behind the sprite to show through.

Since you intend to redraw the contents of the canvas frequently, you may be able to mark it as volatile, which will ensure that having many sprite instances won't bloat deck size with unnecessary copies of sprite frames. If you aren't already doing something similar, you may find it useful to mark the canvas as "animated" so that it will automatically bubble view[] events to the contraption instance at 60fps and "locked" so that it doesn't inherit the default click-and-drag-to-scribble behavior of canvases. (Be aware that "me" will be bound to the original target of the view[] event, not necessarily the contraption itself!)

The Sokoban example deck contains a contraption called "mover" which works similarly in some ways to your "sprite" design, and the Path example deck has another variant called "follower". You might find these useful to reference.

Does any of this point you in the right direction?

(+1)

Yes, this is all very useful. Thank you. I will probably end up using zazz for animated sprites (unless I end up needing non-looping sprites, or fancy events that report when animations end, and things like that). But it's very useful to be able to understand the subtleties of working with contraptions. Decker is awesome, but a bit overwhelming at the beginning.