Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: KyriakosCH on Fri 24/06/2016 13:49:25

Title: Simple scripting question regarding opening and closing animation of object...!
Post by: KyriakosCH on Fri 24/06/2016 13:49:25
Hi!

I made a new animation to open an object (a closet), and scripted that in using the setview command. I wanted to ask how i can make it so that the object loops backwards (i know the command for backward animating of a loop) IF you interact with the object when it is already Open :)

I know this is a very basic question, but spending a little time on the dynamic help in AGS i wasn't able to find an easy answer. Tried a bit with IF command but am way too much a novice in scripting currently... :D

(my script lines are just two currently, object(x)setview(y); object(x)animate(a,b); The animation works fine in game, and just need the way to do what i described above :) )
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: Mandle on Fri 24/06/2016 14:07:09
There are many ways to do this but the easiest to understand and manage is probably to just set a bool variable: "DoorIsOpen" (or whatever you want to name it), set it initially to "false" (if door is closed at start of game)...(Maybe it's best to do this in Global Variables if you are as new to scripting as you say)...

Then you just do something like:


if (DoorIsOpen==false)
{
//Animate object forwards
DoorIsOpen=true;
}
else
{
//Animate object backwards
DoorIsOpen=false;
}


And put this code in the function for "Interact door".

(I didn't include the actual code to animate the object, but you know how to do this: It fits in where my comments show)
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: KyriakosCH on Fri 24/06/2016 14:08:46
Awesome! Thanks!!! :D
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: KyriakosCH on Fri 24/06/2016 14:59:53
Given the bool being some parameter defined (opencloset etc), and i still do not know how to do that (will soon find out :D ) i did this my own novice scripting way, so if you want to see something funny...:

(the interact line is just a relic, the other object is named Closet, and uses view 3, the sequence to open ;) )

(https://s32.postimg.org/srwj9emhh/closet_stuff.png)

At least it already works!!!
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: Mandle on Fri 24/06/2016 15:21:37
Quote from: KyriakosCH on Fri 24/06/2016 14:59:53
Given the bool being some parameter defined (opencloset etc), and i still do not know how to do that (will soon find out :D )

Open the Global Variables window in the main menu.

Right-click on the menu.

Create your variable.
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: KyriakosCH on Fri 24/06/2016 15:51:30
Thanks!!! :D
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: Mandle on Fri 24/06/2016 23:55:22
No worries! I had a look at your code: This is also a viable solution. Good on you for working it out!
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: Khris on Mon 27/06/2016 14:03:17
As was pointed out there are many ways to do this, and my preferred one is to use the available information about the game's state to decide what's going to happen, in this case the frame of the object.

Which means one can do this:
function Closet_Interact() {
  if (Closet.Frame == 0) {
    // closet is closed
    ...
  }
  else {
    // closet is open
  }
}

This will even work if opening the closet for the first time is what sets the view, because Object.Frame returns is 0 if no view was set.
Title: Re: Simple scripting question regarding opening and closing animation of object...!
Post by: KyriakosCH on Mon 27/06/2016 14:24:43
^Thanks! :D

Helps to have more elegant options, despite the 'just write the same commands 1000 times' working too :D