Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: RenatoDias on Wed 28/11/2012 03:36:17

Title: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 03:36:17
Hey guys, here is my question:
I have a certain loop that I want it to rely on a variable to play, for example:
If variable1=1, play View1's Loop 0; if variable1=2, play View1's Loop 1 and so on.
So, long story short, I would like to play the loops through scripting. Is it doable with AGS 3.2.1.111?(The build I have)
Also, when walking, will the Loops above 8 be played automatically?
PS: I know it may have been asked before, but, if you guys can answer this for me. If I'm asking on the wrong forum thread, please move and warn me by PM.
Thanks...
Title: Re: Playing View Loop according to variables
Post by: Icey on Wed 28/11/2012 04:47:21
Code (AGS) Select
If(variable1 == 0){
SoandSo.Animate(...);
}
else if(variable1 == 1){
SoandSo.Animate(...);
}


You never said what it is(character, object, button, etc...) that you wanna animate.
Title: Re: Playing View Loop according to variables
Post by: Crimson Wizard on Wed 28/11/2012 07:19:21
Quote from: icey games on Wed 28/11/2012 04:47:21
Code (AGS) Select
If(variable1 == 0){
SoandSo.Animate(...);
}
else if(variable1 == 1){
SoandSo.Animate(...);
}
And if there are 100 options, there will be 100 else ifs. :)

This is indeed a generic question, in the meaning it does not apply to loops in particular. It is about using parameters. In the case described above, you want to use a function based on parameter to calculate loop. Basically Loop = (param - 1).
If everything else is always the same, you may just write this:

Code (ags) Select

Character.Animate(param - 1, .............);

If that's some kind of standard for your game and used a lot, then it would probably make sense to put this in an extender function (http://www.adventuregamestudio.co.uk/wiki/?title=Extender_functions):
Code (ags) Select

function AnimateLoop(this Character*, int param)
{
   this.Animate(param - 1, ...............);
}

Then call it simply like
Code (ags) Select

   cEgo.AnimateLoop(param);

You can use more parameters, of course.

Quote from: RenatoDias
Also, when walking, will the Loops above 8 be played automatically?
Not sure what you mean. Do you mean, will the character be animated with loops > 8 at the same time as he walks if you call Animate?.
Title: Re: Playing View Loop according to variables
Post by: Khris on Wed 28/11/2012 11:39:01
The first 8 (4) loops of the normal view are used for walking animations, the rest is ignored unless you use them manually.

Do you mean can you add additional directions for really smooth movement? Not using the built-in walking mechanisms.
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 14:10:58
Code (AGS) Select
function AnimateLoop(this Character*, int param)
{
this.Animate(param - 1, ...............);
}


From above, the this keyword after the open bracket line would be the loop #, for example, Loop9.Animate(param -1)?
Also, in this function, what is the param - 1 thing?
Can you clarify this for me, Crimsom Wizard?
Title: Re: Playing View Loop according to variables
Post by: Khris on Wed 28/11/2012 14:46:57
That function is an extender function, extending the available methods for the Character object.
If you have a character called "cSomeGuy", you can now use cSomeGuy.AnimateLoop(5);

The word "this" inside the function will now point to the character "cSomeGuy", in other words the actual function call will be:

  cSomeGuy.Animate(4, ...);

(Since the parameter I used in the example is 5, param will have a value of 5, thus param - 1 equals 4.)
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 15:39:38
So, param is the loop number?

I would do it like below?

Code (AGS) Select
if (variable1==1){
cEgo.Animate(8);
}
else if (variable1==2{
cEgo.Animate(9);
}

Please correct this if wrong. 
Title: Re: Playing View Loop according to variables
Post by: Crimson Wizard on Wed 28/11/2012 16:05:08
RenatoDias, I was trying to explain, but maybe not well enough.

If you will do this:
Code (AGS) Select
if (variable1==1){
cEgo.Animate(8);
}
else if (variable1==2{
cEgo.Animate(9);
}

You will have to add block of "else if" for every possible variable value. And what if you have like 100 possible options?

What I suggest you to do is to think of formula. How do you get Loop from variable? In the example you wrote above you use Loop number which is equal to variable1 + 7: when variable is 1, Loop is 8, when variable is 2, Loop is 9 (do you see the trend here?)

In other words, instead of writing dozens of if/else if, you may write simply:
Code (ags) Select

cEgo.Animate(variable1 + 7);



Quote from: RenatoDias on Wed 28/11/2012 14:10:58
Code (AGS) Select
function AnimateLoop(this Character*, int param)
{
this.Animate(param - 1, ...............);
}

From above, the this keyword after the open bracket line would be the loop #, for example, Loop9.Animate(param -1)?
No, not at all. What I wrote should be understood literally. "This" is "this". "Param" is "param".
That's a function, that takes a Character and an integer value as parameters.

A topic about functions: http://www.adventuregamestudio.co.uk/wiki/?title=Scripting_tutorial_part_1#Functions_Explained
A topic about user-defined functions: http://www.adventuregamestudio.co.uk/wiki/?title=Scripting_tutorial_part_2#Your_own_functions
A topic about extender functions (that explains "this" word): http://www.adventuregamestudio.co.uk/wiki/?title=Extender_functions
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 16:32:21
So, let's say I've already done the scripting for that, will it execute automatically? or I have to call the event from the script, if yes, how?

PS: Sorry for asking a seemingly unrelated question, but, I would like to use this topic instead of creating another one just for the question above.
Title: Re: Playing View Loop according to variables
Post by: Crimson Wizard on Wed 28/11/2012 16:35:49
Quote from: RenatoDias on Wed 28/11/2012 16:32:21
So, let's say I've already done the scripting for that, will it execute automatically? or I have to call the event from the script, if yes, how?
I think I am starting to realize what you are asking about...

Do you mean you want to make some OTHER loops (not 0-7) play automatically when character WALKS like if they were his walking animations?
Title: Re: Playing View Loop according to variables
Post by: Khris on Wed 28/11/2012 16:57:58
Yeah, please tell us what you want to do as a designer would put it who hasn't a clue about AGS script.

Do you want to change the walking loops? As in, the character changes clothing or swims through a river, then continues walking on the other side?
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 17:27:05
QuoteDo you mean you want to make some OTHER loops (not 0-7) play automatically when character WALKS like if they were his walking animations?
Not exactly. I want to know if the script is executed automatically by the game engine itself or I have to call the script using a object-oriented command, like we did back in AGS 2.7 for dialogs(which I never fully mastered)

Also, about my first post, here is what I want to do with all this scripting and variables:
Quote-Use those loops for other activities, such as only when the character is idle(thinking), fixing some broken object, using house appliances and such.
Refer to The Sims game's Fixing Broken Appliance scene. That would be hilarious in AGS.
Title: Re: Playing View Loop according to variables
Post by: Crimson Wizard on Wed 28/11/2012 17:47:50
Quote from: RenatoDias on Wed 28/11/2012 17:27:05
Not exactly. I want to know if the script is executed automatically by the game engine itself or I have to call the script using a object-oriented command, like we did back in AGS 2.7 for dialogs(which I never fully mastered)
Oh, sorry, I understand now.
If you mean animation in particular, only Walking, Speaking and Idle animations are activated automatically, if you setup corresponding  properties for the character. Other kinds of animations should be run from script by calling command.

Quote-Use those loops for other activities, such as only when the character is idle(thinking), fixing some broken object, using house appliances and such.
Except for idle animation, which could be pre-set, you should call commands from script on your own at corresponding times.
There are techniques that let you make this easier for yourself, such as writing your own functions for each type of activity and calling them, so that you won't need to remember loop numbers and other related params all the time (see links I gave above).
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 18:14:33
Quote from: Crimson Wizard on Wed 28/11/2012 17:47:50
Quote from: RenatoDias on Wed 28/11/2012 17:27:05
Not exactly. I want to know if the script is executed automatically by the game engine itself or I have to call the script using a object-oriented command, like we did back in AGS 2.7 for dialogs(which I never fully mastered)
Oh, sorry, I understand now.
If you mean animation in particular, only Walking, Speaking and Idle animations are activated automatically, if you setup corresponding  properties for the character. Other kinds of animations should be run from script by calling command.

Quote-Use those loops for other activities, such as only when the character is idle(thinking), fixing some broken object, using house appliances and such.
Except for idle animation, which could be pre-set, you should call commands from script on your own at corresponding times.
There are techniques that let you make this easier for yourself, such as writing your own functions for each type of activity and calling them, so that you won't need to remember loop numbers and other related params all the time (see links I gave above).
Then, how do I call the same code multiple times?
Can I use any of the functions below?
- DO{this}-WHILE(variable==1)?
or
- For(variable==1){this}
Thanks...
PS:The this is an example of function.
Title: Re: Playing View Loop according to variables
Post by: Crimson Wizard on Wed 28/11/2012 19:16:21
Quote from: RenatoDias on Wed 28/11/2012 18:14:33
Then, how do I call the same code multiple times?
Can I use any of the functions below?
- DO{this}-WHILE(variable==1)?
or
- For(variable==1){this}
Thanks...
PS:The this is an example of function.

This is done with loops, similar like you wrote. AGS does not support "FOR" type of loop, only "WHILE".
But, seriously, this, as well as many other basic and advanced things, are covered in the manual.
Here's something about the loops:
http://www.adventuregamestudio.co.uk/wiki/?title=Scripting_tutorial_part_2#Loops
http://www.adventuregamestudio.co.uk/wiki/?title=Script_language_keywords#while
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 20:07:16
I understand, now I ask:
Let's say I have the following function:
Code (AGS) Select

function fix_computer(int compfixed){
compfixed=0;
    while (compfixed==0) do{
        cEgo.Animate(8);
        }
}

On this code, Loop 8 is being used for the fixing objects(appliances) animation.
Now, how would I call this function mid-game? A "Use Inventory Item(screwdriver) on Object(computer)" can call the function? If so, I would just put fix_computer() inside the brackets on the event for that to work?
-After called, will it keep repeating if a variable keeps the same value?
PS: Sorry for the "DO" keyword, I'm used to programming in C/C++.
Title: Re: Playing View Loop according to variables
Post by: Crimson Wizard on Wed 28/11/2012 20:53:08
Quote from: RenatoDias on Wed 28/11/2012 20:07:16
Now, how would I call this function mid-game? A "Use Inventory Item(screwdriver) on Object(computer)" can call the function? If so, I would just put fix_computer() inside the brackets on the event for that to work?
Basically, yes.
You should also check for what inventory item was used on computer like this:
Code (ags) Select

if (player.ActiveInventory == iScrewdriver)
{
   // run actions here
}


Quote from: RenatoDias on Wed 28/11/2012 20:07:16
-After called, will it keep repeating if a variable keeps the same value?
No, it does not work like that. AGS scripts are single-threaded, and everything is executed consecutively. The code you wrote will loop forever.
There are two types of commands in AGS, blocking and non-blocking. Blocking commands make everything else wait until their execution is finished. Non-blocking commands will trigger some lengthy action and continue executing script.

Some functions may have a parameter that tells them if they should block the game or not. For example, making character animate for once while blocking the rest of the game is done like this:
Code (ags) Select

cEgo.Animate(3, 1, 0, eBlock, eForwards);

Making character animate in a non-blocking way (letting game continue to run actions) is done like this:
Code (ags) Select

cEgo.Animate(3, 1, 0, eNoBlock, eForwards);

Blocking is default.

If you want your character play animation once (like, using item on object), use Blocking animation. You do not need any while loops for that.
Simply:
Code (ags) Select

function fix_computer()
{
   cEgo.Animate(8);
}
Title: Re: Playing View Loop according to variables
Post by: Khris on Wed 28/11/2012 21:25:54
It sounds like you want a character to keep animating using a certain loop, while the game continues, right?
Let's solve this straight, without delving into extender functions and whatnot.

Code (ags) Select
function oComputer_Useinv() {
 
  if (player.ActiveInventory == iScrewdriver) {
    player.Walk(140, 120, eBlock);
    player.LockView(player.NormalView);
    player.Animate(8, 4, eRepeat, eNoBlock);
  }
}


Of course this means that as soon as this interaction was called, when the player interacts with something else, the view has to be unlocked. There are a couple of ways to tackle this, but I still don't have the faintest idea of what you're trying to achieve here.

Quote from: RenatoDias on Wed 28/11/2012 20:07:16I'm used to programming in C/C++.
So why would you get completely stumped by "this" and function parameters...?
There's no shame in admitting that you don't know how to program as long as you read the manual and put some effort into solving your problems on your own before posting here.
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 21:52:11
Quote from: RenatoDias on Wed 28/11/2012 20:07:16I'm used to programming in C/C++.
So why would you get completely stumped by "this" and function parameters...?
There's no shame in admitting that you don't know how to program as long as you read the manual and put some effort into solving your problems on your own before posting here.
[/quote]

I know the basics of C/C++, such as loops and normal functions(if, switch...), but never went THAT deep to use function parameters like that. I know how to do a DO-WHILE loop, IF, ELSE, SWITCH and began to learn about functions. Haven't used function parameters and the "this" keyword yet, that's why I was so stumped. Also, I thought AGS had it's own little language, like GML(Game Maker).

Quote from: Khris on Wed 28/11/2012 21:25:54
It sounds like you want a character to keep animating using a certain loop, while the game continues, right?
Let's solve this straight, without delving into extender functions and whatnot.

Code (ags) Select
function oComputer_Useinv() {
 
  if (player.ActiveInventory == iScrewdriver) {
    player.Walk(140, 120, eBlock);
    player.LockView(player.NormalView);
    player.Animate(8, 4, eRepeat, eNoBlock);
  }
}


Of course this means that as soon as this interaction was called, when the player interacts with something else, the view has to be unlocked. There are a couple of ways to tackle this, but I still don't have the faintest idea of what you're trying to achieve here.

Okay, let's a litte deeper. The text below is a bit "different", so be careful:
In my game, there will be scene like the following(read the bold line to know what I really want):
You arrive in a certain room of a ship you are in since the beggining of the game. The room is a prison, when you walk around, you see a girl, she has both her arms and legs tied, and a piece of tape on her mouth. She will be struggling, that is, "shaking" her body, trying to free herself. If you point the cursor on her, the cursor will change to a scissor, which enables the untie action. From then on(once you untie her), the girl will have her normal movement and follow you around.[...]
That "shaking" animation is what I want to loop.
Title: Re: Playing View Loop according to variables
Post by: Khris on Wed 28/11/2012 22:53:14
Strictly speaking, AGS does have its own language. It's based on C++ and Java though.

For what you want, all you need to do is put this in the room's before fadein:

Code (ags) Select
  cGirl.LockView(VIEW_NUMBER);
  cGirl.Animate(8, DELAY, eRepeat, eNoBlock);


In the untie interaction, call
Code (ags) Select
  cGirl.UnlockView();
which will revert her frame handling to automatic standing/walking.
Title: Re: Playing View Loop according to variables
Post by: RenatoDias on Wed 28/11/2012 23:15:25
Quote from: Khris on Wed 28/11/2012 22:53:14
Strictly speaking, AGS does have its own language. It's based on C++ and Java though.

For what you want, all you need to do is put this in the room's before fadein:

Code (ags) Select
  cGirl.LockView(VIEW_NUMBER);
  cGirl.Animate(8, DELAY, eRepeat, eNoBlock);


In the untie interaction, call
Code (ags) Select
  cGirl.UnlockView();
which will revert her frame handling to automatic standing/walking.
Neat. So, this code will keep looping? Nice. But, what if I put it AFTER fadein? Will it have any difference during the game when the player enters that room?
And just a cGirl.UnlockView(); function resets the character to the normal loops? That's really cool. Anyway, thanks to both you(Khris) and Crimson Wizard for helping me there. Thanks...
Title: Re: Playing View Loop according to variables
Post by: Icey on Wed 28/11/2012 23:29:39
I just wanted to throw out there I thought he was making a on/off trigger for an animation. :x
Title: Re: Playing View Loop according to variables
Post by: Crimson Wizard on Thu 29/11/2012 10:12:29
Quote from: RenatoDias on Wed 28/11/2012 23:15:25
Neat. So, this code will keep looping? Nice. But, what if I put it AFTER fadein? Will it have any difference during the game when the player enters that room?
There will be difference in how fast the animation starts. This also depends on Fade-in type you are using in your game. If animation starts before fade-in, then it will play during fade-in (when screen changes from black to room gradually). Sometimes it makes the difference, and sometimes it doesn't.