how to properly use if variable on this script?

Started by mooneon, Mon 27/02/2023 02:47:10

Previous topic - Next topic

mooneon

I am a beginner and I'm trying to do an interaction for when my character interacts with a hotspot, another sequence will occur after the first interaction is done. I don't want the same script/sequence to appear once the player has interacted with the same hotspot again.

This is my attempt at using if variables on my own...

Code: ags
function hFridge_Look()
{
cMara.FaceDirection(eDirectionDown);
cMara.Say("All I know is that there's nothing in it.");
cMara.SpeechView = 4;
cMara.Say("I'm fine not eating anyway.");
{
  if (cMara.Say == ("I'm fine not eating anyway.") 
  {
  aDistant_alarm.Play(eAudioPriorityNormal);
  cMara.FaceDirection(eDirectionUp);
  cMara.Say("That's my alarm clock.");
  cMara.ChangeView = 6;
  cMara.Say("Why is it going off at this time?");
  cMara.ChangeView = 2;
  cMara.Say("Gotta turn it off before it annoys the heck out of me.");

Nahuel

#1
Normally you don't need that if statement.
The player said the text (in line 6) and there no need to question if the speech was done (which I don't think it returns anything...)

If you need that for only the first time then you should use DoOnceOnly

Check out https://adventuregamestudio.github.io/ags-manual/Game.html?highlight=game&case_sensitive=0#gamedoonceonly

And now you enclose all the things that must happen only once and the the rest in the else.

Edit:
Code: ags
function hFridge_Look()
{
cMara.FaceDirection(eDirectionDown);
cMara.Say("All I know is that there's nothing in it.");
{
  if ( Game.DoOnceOnly("mara will say not eating anyway") ) 
  {
  cMara.SpeechView = 4;
  cMara.Say("I'm fine not eating anyway.");
  aDistant_alarm.Play(eAudioPriorityNormal);
  cMara.FaceDirection(eDirectionUp);
  cMara.Say("That's my alarm clock.");
  cMara.ChangeView = 6;
  cMara.Say("Why is it going off at this time?");
  cMara.ChangeView = 2;
  cMara.Say("Gotta turn it off before it annoys the heck out of me.");

Checked the method for Say and does not return anything. So you cannot even do if ( player.Say("ah!") == "ah! ") {}

Code: ags
void Character_Say(CharacterInfo *chaa, const char *text) {
    _DisplaySpeechCore(chaa->index_id, text);
}

The way of doing this is by using Game.DoOnceOnly("This token string MUST be unique each time otherwise will return false")

I suppose cMara is not the current player @mooneon otherwise you can replace that for player.
Life isn't a game. Let's develop a life-like-game.

Khris

#2
You can use Game.DoOnceOnly() here, but in general you can do this:

Code: ags
int fridgeLook = 0; // initial value, variable declared outside and above function so it retains its value

function hFridge_Look()
{
  cMara.FaceDirection(eDirectionDown);
  fridgeLook++; // increment by one

  if (fridgeLook == 1) {
    cMara.Say("All I know is that there's nothing in it.");
  }
  else if (fridgeLook == 2) {
    cMara.SpeechView = 4;
    cMara.Say("I'm fine not eating anyway.");
    aDistant_alarm.Play(eAudioPriorityNormal);
    cMara.FaceDirection(eDirectionUp);
    cMara.Say("That's my alarm clock.");
    cMara.SpeechView = 6;
    cMara.Say("Why is it going off at this time?");
    cMara.SpeechView = 2;
    cMara.Say("Gotta turn it off before it annoys the heck out of me.");
  }
  else {
    cMara.Say("It's the same empty fridge.");
  }
}

edit: fixed example code

Crimson Wizard

I might also note that you are using ChangeView command incorrectly; or maybe your intent was to use SpeechView instead in this case?
Code: ags
cMara.FaceDirection(eDirectionUp);
cMara.Say("That's my alarm clock.");
cMara.ChangeView = 6;

Unlike SpeechView, which is a property, the ChangeView is a function, and should be called with arguments inside bracketed list:
Code: ags
cMara.ChangeView(6);
See: https://adventuregamestudio.github.io/ags-manual/Character.html#characterchangeview

Khris

Damn, I didn't even notice that :P

I automatically read all these as   cMara.SpeechView = 6; because they used the correct command the first time.

Nahuel

Same here, I just focused the code under the If, well spotted!  :grin:
Life isn't a game. Let's develop a life-like-game.

mooneon

#6
How do you a if statement for when a character interacts with a certain hotspot or object? I cannot find out what its called because I ended up using "player.previousroom == 3)


Code: ags
int alarminteract = 0;
function hAlarm_Interact()
{ 
if (alarminteract == 0){
cMara.Walk(500, 636, eBlock, eWalkableAreas);
cMara.Say("My alarm clock.");
cMara.Say("It's 4:12am right now.");
}

else if (cMara.PreviousRoom == 3) {
  aClosealarm.Stop();
  cMara.Say("Ugh.");
  cMara.Say("So annoying.");

  cMara.Say("Huh?");
}

}

Khris

It's the same principle:

1. you declare an integer variable above the function so it retains its value (the name doesn't matter but should reflect the contents)
2. you increment it by one each time the player does the thing (this is currently missing from your code)
3. you give a different reaction depending on the value

The value of player.PreviousRoom is exactly what the name suggests: it's the room number of the room the player was in before the current one. A typical use-case is in the "enters room after fadein" event where you'll want to check by which exit the room was entered. It has nothing to do with interacting with hotspots.

SMF spam blocked by CleanTalk