Check for if player looked at objects once

Started by AndreasBlack, Thu 15/09/2022 11:28:23

Previous topic - Next topic

AndreasBlack

I'm trying to make an achievement for when a player have looked at four different objects once.

I'm thinking there must be an easier way. This is how i do it atm, bools for every "look", is there another more efficient way?

Code: ags

  else if(Verbs.UsedAction(eGA_LookAt)) {
 
player.Say ("That's Coda!");


if (Nukeus_Item_4==false)

{
  All_Nukeus++;
  Nukeus_Item_4=true;

}


if (All_Nukeus==4)
{

       gNukeus.Visible=true;
       gNukeus.TweenTransparency(0.1, eEaseLinearTween, eNoBlockTween);
       aPickupsound.Play();
      
       Wait(120);
       gNukeus.TweenPosition(0.5, 220, 250,eBlockTween);
       Nukeus_Achievement=4;

}





eri0o

You can use a Set, everytime add the item name to the Set, if the Set length is equal to 4, roll the achievement.

Cassiebsg

Or just add to your All_Nukeus int, once the player has looked at the item (use a DoOnceOnly, so it'll only add once). Then check if the int has reached 4.  ;) No need for bools in this case.
There are those who believe that life here began out there...

AndreasBlack

Quote from: Cassiebsg on Thu 15/09/2022 17:28:00
Or just add to your All_Nukeus int, once the player has looked at the item (use a DoOnceOnly, so it'll only add once). Then check if the int has reached 4.  ;) No need for bools in this case.

HAH! I knew it! Thanks! I was thinking about that one! Alltho Set sounds more interesting to me, since i need to learn some more coding then the absolute basics. I knew about DoOnceOnly,  i just didn't remember the exact syntax. I typed Doonce but got no help from pushing f1  (laugh)

Khris

It's a static method, the syntax is  Game.DoOnceOnly("unique string here"). For each string it returns true the first time, then false for each subsequent call.

Also that achievement code looks like you can probably move that into a function.

AndreasBlack

This is what i've tried. I get the error "undefined Symbol "NukeSet". Defining it outside of the four functions feelt right, but no.

Code: ags
  

function room_Load()

{

    Set* NukeSet = Set.Create();  
                             

}



function oCoda_AnyClick()    //One of the four functions that's supposed to "add" a item that's been looked at.

{

  NukeSet.Add("Coda"); 


  if (NukeSet.Contains("Test") && NukeSet.Contains("Whip") && NukeSet.Contains("Coda")) 

{
  Display("Achievement Done Test");
}

}

}

Khris

#6
You need this:

Code: ags
Set* NukeSet;

function CheckAll()
{
  if (NukeSet.Contains("Test") && NukeSet.Contains("Whip") && NukeSet.Contains("Coda"))
  {
    Display("Achievement Done Test");
  }
}

function room_Load()
{
  if (NukeSet == null) NukeSet = Set.Create();
}

function oCoda_AnyClick() //One of the four functions that's supposed to "add" a item that's been looked at.
{
  NukeSet.Add("Coda");
  CheckAll();
}

Edit: fixed room_Load code, thanks CW

Crimson Wizard

#7
I should perhaps mention that the above code creates a new Set each time player enters the room. Is this a correct intention?

EDIT: oh, it's fixed now.

AndreasBlack

#8
and here i'd thought all i needed to add was a bool == false in the if statement and it would work, but unfortunaly that does not work. This is how i did it, but it's not the right way of course. 
Thanks for the help as always, Khris.


Code: ags
function CheckAll()
{

    
    if (NukeSet.ItemCount==4) //For unknown reason when adding && All_Nukeus==false this if statement won't run. Same with "main if statement"
    
    {
       //All_Nukeus=true;    
      
      NukeSet.Add("Extra"); //Without this added item..I can't make it work without the message repeating itself.
                           
                         
           Display("Achievement Done Test");

           
           
            }
   
}

Crimson Wizard

Quote from: AndreasBlack on Sun 02/10/2022 16:02:26and here i'd thought all i needed to add was a bool == false in the if statement and it would work, but unfortunaly that does not work. This is how i did it, but it's not the right way of course.

It's hard to tell why it did not work without seeing the exact code you used.

But besides a bool variable, you could also use DoOnceOnly here.

Khris

Setting the bool is not enough, you also need to check it.

Code: ags
bool nukeus_achieved = false;

function CheckAll() {
  if (NukeSet.ItemCount == 4 && !nukeus_achieved) {
    nukeus_achieved = true;
    // this will only run once
    Display("Achievement Done Test");
  }
}

AndreasBlack

Crimson Wizard, i used the identical code to Khris example, just added the itemcount instead of various && Set.Contains  :)

Khris, Thanks again ever so much!

SMF spam blocked by CleanTalk