Various Beginner questions regarding scripting

Started by Olleh19, Fri 22/02/2019 13:48:13

Previous topic - Next topic

Snarky

So you have a looping animation running that's been set with myObject.Animate(loop,delay,eRepeat); ?

If the frame you want to stop on is either the last or the first frame in the animation, I think you can just write:

Code: ags
  myObject.Animate(myObject.Loop,delay,eOnce);  // Use 2 instead of eOnce to stop at the first frame


I think this will override the current animation, leaving it on the current loop and frame, then start running a new animation from that loop and frame that stops at the last frame. If this doesn't work, try adding myObject.StopAnimating(); right above it and see if that helps.

If it doesn't work (for example because Object.Animate() always starts from the first frame in the animation loop) you will have to do something more complicated.

Olleh19

#21
Code: ags
function oAbmachine_Interact()
{
player.Walk (210, 130, eBlock); 
player.Say ("bla bla bla!");
oAbmachine.SetView(9);
oAbmachine.Animate(0, 2, eRepeat, eNoBlock);
}


Edit: My last attempt for tonight/early morning going to bed now. Still not working.

Code: ags
function oAbmachine_Interact()
{if (Verbs.UsedAction(eGA_Use))

{
player.Walk (210, 130, eBlock); 
player.Say ("Yeah, let's try that!");
oAbmachine.SetView(9);
oAbmachine.Animate(0, 2, eRepeat, eBlock);
}

else if(Verbs.UsedAction(eGA_Use))
{  
ifabmachineisinuse = true; 
oAbmachine.StopAnimating();
}

I'm not sure you understand what i'm trying to do. It should be an endless loop of light until i sorta like a radio or a tv that can be turned on and off when clicked on it (so the animation obviously should stop, or change to another animation)?
I added the stopanimating(); line but it didn't help, in fact it made the animation not work at all. I'm thinking perhaps i'm doing this in the wrong events tab?
This is so common in point and click games would be surprised if it's really difficult to do!  8-0

I'm clicking on the object/interact with object, or should one create a hotspot for this? I'm a bit confused when to use a hotspot and when not to.


Snarky

Quote from: Olleh19 on Wed 27/02/2019 00:34:40
I'm clicking on the object/interact with object, or should one create a hotspot for this? I'm a bit confused when to use a hotspot and when not to.

In point-and-click adventure games, you obviously want some stuff in the room to be "clickable", and (depending on your UI â€" it looks like you're using Tumbleweed Verbs) to display its name when you hover the mouse over it. In AGS, characters and objects are clickable by default (though you can turn it off on a case-by-case basis). But what if the thing you want to be clickable is just some part of the background? That's when you use hotspots. It lets you mark a part of the background as a clickable region and give it a name, without having to add an object or character. Typically you'll use it on stuff that you can't take or that doesn't move, e.g. for a window that you can just look out of, or a bookshelf with stuff you can't take. If you have a clickable object or character, you don't need it.

In this case, if I understand you correctly, you have an object that should start animating (and let you do other things) when you click on it once, and stop when you click on it again. Your first piece of code looks fine for starting it.

The next thing we need to do is figure out how we know whether to start or stop when we click. Actually, the next thing we need to do is format our code correctly, remember? Let's take the code you have and reformat it:

Code: ags
function oAbmachine_Interact()
{
  if (Verbs.UsedAction(eGA_Use))
  {
    player.Walk (210, 130, eBlock); 
    player.Say ("Yeah, let's try that!");
    oAbmachine.SetView(9);
    oAbmachine.Animate(0, 2, eRepeat, eBlock);
  }
  else if(Verbs.UsedAction(eGA_Use))
  {
    ifabmachineisinuse = true; 
    oAbmachine.StopAnimating();
  }
} // This last bracket was missing, which becomes obvious once the code is correctly formatted


You have half of a correct idea here: You do want two code-blocks inside the function, one to start and one to stop. However, what you put in the if-condition has to be the thing that tells you which action to take, and in this case that's "is the machine running?", not "did I click on it with the 'Use' interaction?".

So, given that you're using Tumbleweed Verbs, the code should be structured something like this:

Code: ags
function oAbmachine_Interact()
{
  if (Verbs.UsedAction(eGA_Use)) // Clicked "Use"
  {
    // Always walk up to the machine
    player.Walk (210, 130, eBlock); 

    if( THE MACHINE IS NOT RUNNING )
    {
      // Start the machine
      player.Say ("Yeah, let's try that!");
      oAbmachine.SetView(9);
      oAbmachine.Animate(0, 2, eRepeat, eNoBlock);  // You had this set to eBlock, which would stop you from doing anything else after you started it
    }
    else // The machine *is* running
    {
      // TODO: Stop the machine
    }
  }
  // Here come the other verbs
  else if(Verbs.UsedAction(eGA_Look))  // Clicked "Look"
  {
    // Look
  }
  // Other verbs...
}


So how do we know whether the machine is on or off? I see you've created a variable called "ifabmachineisinuse", which is one valid approach, but (1) you're only setting it, not using it in the if-condition to determine what to do, and (2) you're (trying to) set it to true when you turn the machine off, while it ought to be set to true when you turn the machine on, and false when you turn it off. Let's fix that:

Code: ags
bool abMachineIsInUse=false;

function oAbmachine_Interact()
{
  if (Verbs.UsedAction(eGA_Use)) // Clicked "Use"
  {
    // Always walk up to the machine
    player.Walk (210, 130, eBlock); 

    if(abMachineIsInUse == false)
    {
      // Start the machine
      player.Say ("Yeah, let's try that!");
      oAbmachine.SetView(9);
      oAbmachine.Animate(0, 2, eRepeat, eNoBlock);
      abMachineIsInUse = true;
    }
    else // The machine *is* running
    {
      // TODO: Stop the machine
      abMachineIsInUse = false;
    }
  }
  // The other verbs ...
}


Note that I changed the variable name from ifabmachineisinuse to abMachineIsInUse, for two reasons: (1) It doesn't make sense to have "if" in this name, because then when you use it in an if-condition it becomes doubled-up: if (ifabmachineisinuse == false). (2) I use a capital letter at the start of each new word in the name, apart from the first one (this is called "CamelCase"). You should get in the habit of always marking the word-divisions in your variable names: it makes things much more readable (if I just glance at your version, I might see the string "chineisi" in the middle and wonder if there's something Chinese going on), and otherwise you'll sooner or later run into the "expertsexchange.com" problem of ambiguous names. You can use CamelCase or underscore_naming to break up the words in the name.

I point this out because giving good variable names is another really important "soft" programming skill that I think people neglect far too often.

OK, so now that we have the code structure set up, let's try my suggestion:

Quote from: Snarky on Tue 26/02/2019 22:58:03
If the frame you want to stop on is either the last or the first frame in the animation, I think you can just write:

Code: ags
  myObject.Animate(myObject.Loop,delay,eOnce);  // Use 2 instead of eOnce to stop at the first frame


So:

Code: ags
bool abMachineIsInUse=false;

function oAbmachine_Interact()
{
  if (Verbs.UsedAction(eGA_Use)) // Clicked "Use"
  {
    // Always walk up to the machine
    player.Walk (210, 130, eBlock); 

    if(abMachineIsInUse == false)
    {
      // Start the machine
      player.Say ("Yeah, let's try that!");
      oAbmachine.SetView(9);
      oAbmachine.Animate(0, 2, eRepeat, eNoBlock);
      abMachineIsInUse = true;
    }
    else // The machine *is* running
    {
      // Stop the machine
      oAbmachine.Animate(myObject.Loop, 2, eOnce, eBlock); // We're interrupting the eRepeat animation and just running an eOnce animation
      abMachineIsInUse = false;
    }
  }
  // The other verbs ...
}


So, now the question is, does this work to "smoothly" stop the animation?

Olleh19

Was up till 4am this night trying to get it to work, my copy-paste failed leaving one bracket out. What i meant to say was that the code was "working" (the one i pasted) but the end result still wasn't that the machine "turned on/off" so to speak.

With your modifications and great explanations it now works.
It works perfectly fine now! God, i thought i've learn't how to format the code. I need to look more closely at your examples again.
Thanks so much again!

Snarky

Cool, glad you got it working!

A couple of other tips:

1. If you want the dialog to be different the first time you start the machine vs. other times, you can use Game.DoOnceOnly():

Code: ags
    if(abMachineIsInUse == false)
    {
      //Start the machine
      if(Game.DoOnceOnly("start abMachine"))
        player.Say("Yeah, let's try that!");
      else
        player.Say("Let's try again!");

      oAbmachine.SetView(9);
      oAbmachine.Animate(0, 2, eRepeat, eNoBlock);
      abMachineIsInUse = true;
    }


2. I think one reason why you were having trouble here is that you were trying to do too much at once. In programming, it's very helpful to divide up problems into small tasks, and focus on solving one thing at a time: otherwise you often end up with a bunch of complicated code and no idea why it doesn't work. Here you were trying to figure out two things you didn't know at the same time:

-how to stop an animation "smoothly" (run until it reaches a certain frame)
-how to toggle something on and off so that it turns on when you click it once and back off when you click it again

If you'd focused on solving each problem separately, you could have reduced the complexity a lot on each task, and then once you figured it out you could just have combined the two solutions. I think it would have been a lot easier.

One way to do that is to use placeholders for "stuff goes here", like I did above when I put "TODO: Stop the machine". If you're trying to test that the overall logic of toggling the switch is correct, I'd put a placeholder you can see when you run the game, just a very simple command like Display("The machine turns off");. Then when you know that it runs when it's supposed to, you can add the actual code.


Olleh19

Good to know!  I'm beginning to get beyond frustrated by the Manual tbh. I'm again just sitting here and guessing what "might work", and obviously it don't. My programming career would have ended long ago, if it wasn't for your help, Snarky! V E R Y Thankful. It seems like the character now disappears slightly when walking by the Machine, so what i immediately think about, is that the object probably is like characters set to "solid".
Searching in the manual and looking at "object.solid"  it should indeed be available for objects aswell. So i pick "events after or before entering the room" (Again, just guessing, nowhere to be found in the manual which events tab i should choose for the object other then "common sense". Makes more sense that the object is solid = false; before the actual "interaction" starts, right?! so i added myobject.solid = false;

And ofc, the command seems to run thru but it doesn't do anything, character still get's invisible when passing thru parts of the object. Then in the manual it says before anything it looks like i should have typed an "bool machine.Solid" command? So i try that in the script beginning what happens then.
Well then i get this failure "oAbmachine is already imported". Which also doesn't make sense to me. I used the "oAbmachine" many times in the script. There is a bool set but it's not called oAbmachine. Why won't AGS let me?  :confused:

:cry:

Snarky

The .solid property probably isn't quite what you're looking for. It controls whether you can walk through something or must walk around it, not whether you can see through it.

As long as an object is visible, it's going to cover up anything that is behind it. So it sounds like your problem is that AGS thinks your character is behind the object when actually it's in front of it.

The order in which things are in front of or behind each other are controlled by the baselines. A character's baseline is at its feet (by default), and the rule is that anything with a baseline that is higher up on the screen (smaller Y-coordinate) is behind anything with a baseline lower down. This tends to work out correctly when everything stands on a floor seen from above, but sometimes the baselines will be off, so it is possible to overrule them (in the editor or in scripts).

Quote from: Olleh19 on Wed 27/02/2019 13:29:35Then in the manual it says before anything it looks like i should have typed an "bool machine.Solid" command? So i try that in the script beginning what happens then.

Where are you seeing that? That doesn't really make sense.

Quote from: Olleh19 on Wed 27/02/2019 13:29:35Well then i get this failure "oAbmachine is already imported". Which also doesn't make sense to me. I used the "oAbmachine" many times in the script. There is a bool set but it's not called oAbmachine. Why won't AGS let me?  :confused:

TBH I can't quite follow your explanation about what you've done or why. If you want help on specific errors in your script, you'll need to copy the exact content, otherwise we can only guess.

Olleh19

#28
QuoteWhere are you seeing that? That doesn't really make sense.


from the Manual (Again, remember i am Swedish, maybe i am just reading this "wrong"):

MANUAL : "Solid property (object)
bool Object.Solid

Gets/sets whether the object can be walked through by characters.
If this is set to true, then the object is solid and will block the path of solid characters. If this is set to false, then the object can be walked through by characters.

NOTE: solid objects only block characters, they don't block other objects from moving through them.

Example:

oSmallrock.Solid = true;

will mean that the Smallrock object blocks the path of characters."


I have used forexample with the computer screen similar actions with success. hScreen.Enabled = false; or oOffscreen.Visible = false;
But most likely those commands would not work. I cannot make the object disappear then the function's that you helped me with will not function probably cause the object is not visible by then (i think?). I could easily solve this in the future by just making the object smaller, it's the entire machine, i could just crop the image to only be around the blinking light, that would solve things, so if there is no "easy way out" i will have to do so, i suppose.

my code was this,
Code: ags
function room_Load()
{
oAbmachine.Solid = false; //Shouldn't this make the player be able to just walk over the object without "dissapearing"? That's the command i'm looking for!
}



EDIT: AAAAAAAAH!! I get it now. Walkthrough in this case just means being able to walk onto it. That has nothing to do with weither the character/objects appearance.

EDIT: A picture says more then 1000 words. Perhaps there is some kind of "layer function" similar to photoshop where i can choose the character to go over all the other objects?
[imgzoom]https://i.imgur.com/f21mILa.png[/imgzoom]



Olleh19

#29
Am i pushing wrong all the time? I pushed MODIFY not Quote. Still i get a new post once again. Sorry. Just have Modify, Quoting should be inside the text editing (i think) OR on someone elses text. I hope admin don't mind taking that change into consideration. :smiley:
I think on other forums there is not an option for Quote so i accidently click it everytime i should edit my post  :-[

Khris

#30
You clicked "quote" by accident. Clicking "modify" leads to a textarea containing just the post, without [quote] tags around it, and has a "Save" button below, instead of "Post".

As for the manual's
bool Object.Solid

This means that each Object has a .Solid property of type bool, which means you can set it to either true or false.

When you used  bool oAbmachine  AGS thought you wanted to declare a bool variable called oAbmachine, and informed you that this variable already exists.

As for "events after or before entering the room", please read what it actually says.
The events are called "Enters room after fade-in" and "Enters room before fade-in".
"before fade-in" is where you set up room-specific stuff while the screen is still black (like positioning characters for a cutscene); "after fade-in" is where you put stuff that's supposed to happen as soon as the room is fully visible (like making them walk somewhere).


In general, if you have difficulty understanding the manual, keep in mind that you are learning how to program and how AGS's API works at the same time. The manual assumes basic scripting knowledge, so can be difficult for a beginner programmer to make sense of. That's not the manual's fault though.

Olleh19

#31
Quote
You clicked "quote" by accident. Clicking "modify" leads to a textarea containing just the post, without [quote] tags around it, and has a "Save" button below, instead of "Post".

Ah, yeah makes sense. I have to get used to it. Since my programming knowledge is lacking a aweful lot. I don't know if you have read any of my wall of texts so far, but i actually did ask about advice on where to learn "basic coding" since i just like you pointed out feels like ags manual expects you to "know" programming beforehand, which i don't. But it's funny when it starts to work, and you guys are a HUGE help. I also read things wrong cause of my english reading ability isn't up to par. 

QuoteAs for the manual's
bool Object.Solid

This means that each Object has a .Solid property of type bool, which means you can set it to either true or false.

When you used  bool oAbmachine  AGS thought you wanted to declare a bool variable called oAbmachine, and informed you that this variable already exists.

Ahh, that clarifies that situation!


QuoteAs for "events after or before entering the room", please read what it actually says.
The events are called "Enters room after fade-in" and "Enters room before fade-in".
"before fade-in" is where you set up room-specific stuff while the screen is still black (like positioning characters for a cutscene); "after fade-in" is where you put stuff that's supposed to happen as soon as the room is fully visible (like making them walk somewhere).

Yes, yes ofc i get that. I was just getting frustrated when none of the options seems to do anything. But it was because i "read wrong". Walk through a path is different then "walking thru an object". Which my mind was reading. Probably cause English is not my native language, again................ :-X


QuoteIn general, if you have difficulty understanding the manual, keep in mind that you are learning how to program and how AGS's API works at the same time. The manual assumes basic scripting knowledge, so can be difficult for a beginner programmer to make sense of. That's not the manual's fault though.

If you could be so kindly please direct me to some  webpage where i can learn more about such basic concepts. Because i don't want to be a pain in here. I already feel like guys in here get annoyed cause i keep asking "stupid questions". Even tho it's a beginners forum. I did solve a lot of things myself which i clearly showed in various examples. But it would be good to know what i should "look for". What's relevant programming basics for AGS.



Snarky

Unfortunately there's not really a single good entry point to learning the programming skills you need to use AGS. You'll need to a basic grounding in (imperative) programming concepts, and you'll also need basic knowledge of some of the common programming terms, constructs and syntax used by AGS script.

AGS Script belongs to a family of programming languages that are all written in pretty much the same way, with many of the same conventions and keywords: C-like languages. Popular C-like languages include C, C++, Java, C#, Objective-C, and PHP (JavaScript is a language that borrows a lot of the "look and feel" of C-like languages but actually behaves quite differently). If you learn how to read and use any of these languages, you should be able to understand almost any AGS code.

The language that AGS Script most closely resembles for everyday use is probably C, but it also borrows some things from Java and C# (notably the way Strings work is more like those languages; and also the fact that a lot of the AGS code is event-driven). So I would recommend that you find some good "intro to programming C" tutorial to help you get your bearings.

Olleh19

Quote from: Snarky on Wed 27/02/2019 15:55:30
Unfortunately there's not really a single good entry point to learning the programming skills you need to use AGS. You'll need to a basic grounding in (imperative) programming concepts, and you'll also need basic knowledge of some of the common programming terms, constructs and syntax used by AGS script.

AGS Script belongs to a family of programming languages that are all written in pretty much the same way, with many of the same conventions and keywords: C-like languages. Popular C-like languages include C, C++, Java, C#, Objective-C, and PHP (JavaScript is a language that borrows a lot of the "look and feel" of C-like languages but actually behaves quite differently). If you learn how to read and use any of these languages, you should be able to understand almost any AGS code.

The language that AGS Script most closely resembles for everyday use is probably C, but it also borrows some things from Java and C# (notably the way Strings work is more like those languages; and also the fact that a lot of the AGS code is event-driven). So I would recommend that you find some good "intro to programming C" tutorial to help you get your bearings.

Thanks Snarky, you have been very helpful!
You can forget about those other questions. I was just feeling a little bit of frustration. I had similar issues with the bakgrounds in the beginning. Just because the resolution is 320/200 doesn't mean the image is to be drawn that size, i learnt that "the hard way", you have Gui's and maybe other things in the way. I see it the same way with the object. Next time i'll draw the object in the exact little size for it to be clickable and not any bigger then needed for it to work, so the character won't need to cross it's pixels in any way.


SMF spam blocked by CleanTalk