Hero Walkabout Sprite Layering

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
splendidland
Red Slime
Posts: 42
Joined: Tue Feb 19, 2013 11:06 am
Contact:

Hero Walkabout Sprite Layering

Post by splendidland »

Good evening!
I'm hoping to make a short game over the course of a week, and I'd like to implement a system that'll allow the players appearance to be customized. I'd be using the Walktall script as well.
I'd like to make it so that the hero's overworld sprite is composed of 3 sprites: a body, head, and face, and allow for them and their palettes to be changed by other scripts.
If anyone would know how to create such a system it would be much appreciated!
Thank you in advance!
User avatar
kylekrack
Liquid Metal Slime
Posts: 1240
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Hi!! <3<3<3

First of all, using the walktall script is all but deprecated at this point, since in the recent nightly builds, you can create walkabouts (and other sprite types) of any size.

It sounds like you'll need to use load walkabout sprite() two separate times and call set parent() on each one with the hero slice to lock them into the right place. (be sure to use put slice(handle, x, y) to make sure the loaded sprites are in the right place relative to the hero slice)

Next you'll have to animate them correctly, which is a little more complicated. I know there's a way to do it, but I'll have to do a little research to find out how. There are some quirks that get in the way.

EDIT: I'm looking at these scripts for guidance. The eachtick script is set up in a similar way to what you'll need. The specifics aren't quite the same, but accessing slice handles and stuff can be tricky, and this does it the right way.
Last edited by kylekrack on Thu Jan 24, 2019 8:24 pm, edited 1 time in total.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Here are some scripts to do this.
If you try to set the direction of the head and face sprites to be the same as the walkabout (body) sprite, then the direction will lag behind for one tick, because scripts run before the engine processes user input and updates the hero sprites. There are some scripts on the wiki here which can be used to work around that (but they will still lag when you use the mouse to move the hero, or use the "walk hero" command, and some other corner cases). (This is something I really need to fix in the engine).
I tried out these scripts with and without making use of "hero will move", and actually that one-tick lag isn't noticeable, at least for the hero sprites I was using.

If you want to use the "hero will move" scripts, copy in the scripts from the top section of that wiki article.
If you don't want to use them, delete the "next leader direction" script and replace "next leader direction" with "hero direction(me)".

Also, even when making use of "hero will move", the A/B frame of the head and face will be one tick out of sync with the body sprite. It probably doesn't matter, but if it's really a problem it is possible to prevent that with more work.

Code: Select all

define constant&#40;0, timer&#58;hero sprites&#41;

# This is just the leader's walkabout sprite
script, body sprite, begin
  return &#40;lookup slice&#40;sl&#58;walkabout sprite component, get hero slice&#40;0&#41;&#41;&#41;
end

script, head sprite, begin
  return &#40;slice child&#40;body sprite, 0&#41;&#41;
end

script, face sprite, begin
  return &#40;slice child&#40;body sprite, 1&#41;&#41;
end

plotscript, setup hero sprites, begin
  # Create the head sprite
  set parent&#40;load walkabout sprite&#40;1&#41;, body sprite&#41;
  # Create the face sprite
  set parent&#40;load walkabout sprite&#40;2&#41;, body sprite&#41;

  update hero sprites eachtick
end

script, update hero sprites eachtick, begin
  # Set direction based on what the body sprite *will be* and A/B frame
  # from what the body sprite *is* &#40;so will lag the body by one tick&#41;
  variable&#40;frame&#41;
  frame &#58;= &#40;get sprite frame&#40;body sprite&#41;, and, 1&#41; + 2 * next leader direction  #hero direction&#40;0&#41;

  set sprite frame&#40;head sprite, frame&#41;
  set sprite frame&#40;face sprite, frame&#41;

  # This schedules the script to run again next tick, continuing forever
  set timer &#40;timer&#58;hero sprites, 0, 1, @update hero sprites eachtick&#41;
end


# Determine the direction that the leader will be facing this tick, after
# user input is accounted for
script, next leader direction, begin
  if &#40;hero is walking&#40;0&#41;&#41; then &#40;
    return &#40;hero direction&#40;0&#41;&#41;
  &#41; else if &#40;hero will move&#41; then &#40;
    return &#40;trying to move direction&#41;
  &#41; else &#40;
    return &#40;hero direction&#40;0&#41;&#41;
  &#41;
end
Last edited by TMC on Fri Jan 25, 2019 1:31 am, edited 1 time in total.
splendidland
Red Slime
Posts: 42
Joined: Tue Feb 19, 2013 11:06 am
Contact:

Post by splendidland »

ah, it all works perfectly! the slight lag makes the head of the character wiggle slightly when they walk which is cute.
I am wondering is there a way to refresh the script? so that parts can be changed without having to leave the map
User avatar
kylekrack
Liquid Metal Slime
Posts: 1240
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

If you set "setup hero sprites" to be activated by an NPC or menu item or something, you should be able to do that. Although to change which sprites are loaded, you'd probably have to give "setup hero sprites" two arguments that are then passed into "load walkabout sprite(argument)". If I'm reading this correctly, that should work. You might also have to stop the timer in "setup hero sprites" just to make sure it doesn't get called on the same tick twice.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Actually the head & face slices should remain when you change map. You would need to recreate them only if you change the party or save and reload the game.

You will want to delete the old slices before you create new ones! Like so

Code: Select all

plotscript, setup hero sprites, begin
  if &#40;face sprite&#41; then &#40;free slice &#40;face sprite&#41;&#41;
  if &#40;head sprite&#41; then &#40;free slice &#40;head sprite&#41;&#41;

  # Create the head sprite
  set parent&#40;load walkabout sprite&#40;1&#41;, body sprite&#41;
  # Create the face sprite
  set parent&#40;load walkabout sprite&#40;2&#41;, body sprite&#41;

  update hero sprites eachtick
end 
There's no need to worry about the timer, as it doesn't matter if "update hero sprites eachtick" gets called twice.
Last edited by TMC on Tue Jan 29, 2019 4:24 am, edited 2 times in total.
splendidland
Red Slime
Posts: 42
Joined: Tue Feb 19, 2013 11:06 am
Contact:

Post by splendidland »

It mostly works perfectly, though I've run into an error!
https://i.imgur.com/HNS494d.png
https://i.imgur.com/0rV5s7B.png
It works very well for walking around, but upon talking to an NPC, the head and face look upwards for some reason, which will obviously look strange a lot of the time! This also sometimes happens for a single frame after walking off diagonal stairs.
Thank you very much for the help by the way!!
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Wow, that doesn't look like TCoD ... a new game?

I see that this is because you are using the "next leader direction" script, and it turns out there is a bug in that. I had thought you weren't going to use that script.

The fix is to change the line in "trying to move direction"

Code: Select all

  if &#40;current textbox > -1 || player is suspended&#41; then &#40;exit returning&#40;false&#41;&#41;
to

Code: Select all

  if &#40;current textbox > -1 || player is suspended&#41; then &#40;exit script&#41;
Also, to not use "next leader direction" (at the cost of common one-tick delays), you can simply change the line

Code: Select all

frame &#58;= &#40;get sprite frame&#40;body sprite&#41;, and, 1&#41; + 2 * next leader direction
to

Code: Select all

frame &#58;= get sprite frame&#40;body sprite&#41;
I'm not sure about the diagonal stairs problem since I don't know which script you're using, but if the stairs script contains "suspend player" this probably fixes it too.
Last edited by TMC on Thu Feb 14, 2019 3:11 am, edited 3 times in total.
splendidland
Red Slime
Posts: 42
Joined: Tue Feb 19, 2013 11:06 am
Contact:

Post by splendidland »

Ah that seems to have fixed everything, thank you so much for your help!!

And yes, it's a new game called Amazing Treasure Valley! TCoD is something i care about a lot but it's also quite ambitious, and working on something like that for so long without being able to let people play it is hard, so I'm making a smaller exploration game that I can finish in about a month! Look forward to playing it soon!
Post Reply