I want to add a simple cam animation at the start of a room. Start at the very top (0,0), move down to the normal position. No modules. The steps seemed easy. Use camera setat to disable autotracking and set the starting position, set the new position, animate between.
As usual, the simple stuff stumps me. Whatever I tried on my own, either it didn't animate or it got stuck in a loop. I found a thread where Crimson posted:
int new_x = Game.Camera.X + Game.Camera.Width;
while (Game.Camera.X < new_x) {
Game.Camera.X += DISTANCE_PER_GAME_TICK;
if (Game.Camera.X > new_x) {
Game.Camera.X = new_x; // snap it to make sure it does not go any further
}
Wait(1); // makes engine update the screen
// you may also play with the speed/delay numbers to find a good combination
}
I'm not doing what that OP did but it's a similar base, so I started with this.
int mycamy = Game.Camera.Y + Game.Camera.Width;
while (Game.Camera.Y < mycamy)
{
Game.Camera.Y += 2;
if (Game.Camera.Y > mycamy)
{
Game.Camera.Y = mycamy;
}
Wait(2);
}
It does exactly what I want at the speed I want, but the scene doesn't continue after the cam reaches its final position. It stays stuck on wait. How do I fix that? Btw I tried using if statements in the room's repeat function instead in case the problem was using while, but that went worse, so let's stick with this. (laugh)
Other questions:
1) Why add width? Changing to height performed the same, removing didn't animate. What's that doing?
2) How do I enable tracking again after? I thought I'd put "Game.Camera.AutoTracking = true" after the code but can't tell if it works since the code is stuck in wait.
Thanks!
Bookmark:
https://www.adventuregamestudio.co.uk/forums/index.php?topic=59379.0
https://github.com/adventuregamestudio/ags-manual/wiki/UpgradeTo35#new-viewportcamera-system
https://adventuregamestudio.github.io/ags-manual/Camera.html
It looks like the sample code is written to scroll one camera-width to the right (like a scrolling room that moves when you get to the edge). That's why it has Game.Camera.Width. Since you're scrolling in the Y-direction, you should use Game.Camera.Height for the equivalent effect, but more to the point, the value of mycamy should be whatever coordinate you actually want to scroll to.
I believe the only situation where your code would get stuck on the Wait() is if the loop never terminates, because you always have Game.Camera.Y < mycamy. And since you keep increasing Game.Camera.Y, I can only think that it's because the camera can't actually move to the coordinate specified. In other words, that you've gone beyond the edge of the room. You need to make sure that mycamy is a value that can actually be reached.
As another, minor point: You're moving the camera 2 pixels and waiting 2 loops. You would get the same movement speed but twice as smooth movement by moving 1 pixel and waiting 1 loop.
Thanks! The OP wanted the camera to scroll right, then go up in the last third. I don't need that but like I said, switching it to height changed nothing anyway, and removing it stopped the animation from working entirely, so I just left it for now.
When I tried to write a script for this on my own, I knew I wanted the cam to end at whatever the Y coordinate would normally be when the room starts with tracking on. So before I did anything, I used display to return that Y coordinate and that's what I set my variable to.
I couldn't get my code to work no matter how I tweaked it. That's why I scrapped it and looked for the simplest code I could find that does what I need to work backwards from there. Crimson's code fit the bill. I just don't know how to adjust it to my use without breaking it.
And the "snap" comment sounded like that bit was meant to prevent the thing that I suspected was the problem with it being stuck in wait. The conditions causing the cam to keep trying to go down because they're always met. But I don't know.
For the speed, I set those to whatever I felt like to visually confirm what's happening. It was originally 20 pixels and 5 cycles and I played around decreasing/increasing them to compare. 2/2 was just the first pairing that's exactly the speed and movement I want so I kept it. I just tried using 1 for both instead and it looks exactly the same to me, not smoother.
I'm not worried about that now though. I'm using a placeholder bg and don't know how tall the final bg will be because I haven't drawn it yet, so I may end up tweaking the speed anyway down the line. I'm just trying to nail how to script this movement.
Quote from: skooperstooper on Sun 28/08/2022 22:46:03
And the "snap" comment sounded like that bit was meant to prevent the thing that I suspected was the problem with it being stuck in wait. The conditions causing the cam to keep trying to go down because they're always met.
No, the "snap" is to make sure it doesn't overshoot. Like, if it's set to scroll 5 pixels per loop to the target coordinate Camera.X=800, and in the second-to-last loop Camera.X=798, then next loop it will be increased to Camera.X=803. This condition catches that and makes sure it stops at 800, not 803.
OK, so to be very clear: Check how tall your test room is, and subtract the height of the screen. Then try a different value in the first line, making sure it is less than the number you have calculated. For example:
int mycamy = Game.Camera.Y + 40;
Ah, I see! The snap part stops it physically overshooting but doesn't stop the actual checks from running once it does. I just assumed it did both. Oops.
The second part goes back to why I asked about width being added there. I didn't get why moving the camera by XY needs to include the size of the camera if that size is inherent unless you change it.
Or is it only needed in Crimson's code because it didn't specify the XY coordinates, so AGS has to calculate what they would be using the cam's width/height?
At any rate, what you described is what I tried to do originally. The cam's XY coordinates control the upper left corner and its height is equal to the screen, so I wanted the final Y to match the very bottom of the room minus that height. I guess I just messed it up somehow.
Coding isn't second nature to me. I'm not a developer. I only know syntax because I did web design and fill in the gaps with the AGS manual. I can see what a code says and guess why it's being applied but don't immediately know. I have to talk through it. I'll play with it when I'm back at the laptop and post what worked!
Okay, this is what I did and it's not stuck in wait anymore, so all is well. Thanks, Snarky!
int mycamy = Game.Camera.YÂ 72;
while (Game.Camera.Y < mycamy)
{
 Game.Camera.Y = 2;
 if (Game.Camera.Y > mycamy)
 {
  Game.Camera.Y = mycamy;
 }
 Wait(2);
}
My main issue troubleshooting code I write myself is I usually end up changing something that wasn't the problem. I thought using 72 was the issue before (that's the Y the game returned, same as what I calculated now), so I tried to use something else that made it worse.
You confirming it's about factoring in the screen height plus seeing Crimson's code helped me figure out that I was right with the approach. I just messed up the loop I wrote in a way that kept it from animating.
ETA: I tried changing it to 1/1 again now that the code works and it does look smoother now. Maybe something about how the code was before was messing up the scroll.