Random Treasure (Why doesn't this work???)

Started by cdavenport, Sun 02/06/2019 14:11:04

Previous topic - Next topic

cdavenport

I'm trying to have it so if the player opens to box, there's a random chance that there will be an item inside of it, and if they don't already have the item it gives it to them. It seems like this code should work but it doesn't. Any kind of variation I try seems to just return parsing errors. Shouldn't there be simple way of doing this?

Code: ags


int ran=Random(3);
if (OBJECT1_FOUND == 0 && (ran==0) cEgo.AddInventory (iObject1)) {

}
else if (OBJECT2_FOUND == 0 && (ran==1) cEgo.AddInventory (iObject2)) {

}
else if (OBJECT3_FOUND == 0 && (ran==2) cEgo.AddInventory (iObject3)) {
}
else {
  Display ("Nothing was found.");
  
}


Cassiebsg

#1
Quote
Code: ags


int ran=Random(3);
if (OBJECT1_FOUND == 0 && (ran==0)   cEgo.AddInventory (iObject1)) 
  {}
else if (OBJECT2_FOUND == 0 && (ran==1)   cEgo.AddInventory (iObject2)) 
  {}
else if (OBJECT3_FOUND == 0 && (ran==2)    cEgo.AddInventory (iObject3)) 
  {}
else 
 {
  Display ("Nothing was found.");
  }



If that's an exact copy/paste of what you have, I can understand all those parse errors.

Let's start, first your code seems to be lacking any connection to a function... so I'll add that:

Code: ags

function BoxLuckyDip() // giving it a name you can easy remember when you wish to use it.
{ // this starts this block of code
  int ran=Random(3);
  if (OBJECT1_FOUND == 0 && (ran==0)) cEgo.AddInventory (iObject1); // Note and remember to close the parenthesis, then add the command and end it with ;
  else if (OBJECT2_FOUND == 0 && (ran==1)) cEgo.AddInventory (iObject2);
  else if (OBJECT3_FOUND == 0 && (ran==2)) cEgo.AddInventory (iObject3);
  else Display ("Nothing was found.");
} // this ends the function and corresponding block of code


I'm not sure where you changing the OBJECT3_FOUND variable, but it probably would make sense to change it here when you give the item, so you would then have:

Code: ags

function BoxLuckyDip() 
{ 
  int ran=Random(3);
  if (OBJECT1_FOUND == 0 && (ran==0)) 
    { // note start of new block of code
    cEgo.AddInventory (iObject1); 
    OBJECT1_FOUND = 1;
    } // note end of this block of code... 
  else if (OBJECT2_FOUND == 0 && (ran==1)) 
    {
    cEgo.AddInventory (iObject2); 
    OBJECT2_FOUND = 1;
    }
  else if (OBJECT3_FOUND == 0 && (ran==2)) 
    {
    cEgo.AddInventory (iObject3); 
    OBJECT3_FOUND = 1;
    }
  else Display ("Nothing was found.");
} 


Hope this helps.
There are those who believe that life here began out there...

Laura Hunt

#2
Ummm something I can see right off the bat is that your brackets are all messed up, hence the parsing errors. Try this:

Code: ags
if (OBJECT1_FOUND == 0 && ran == 0) cEgo.AddInventory(iObject1);

else if (OBJECT2_FOUND == 0 && ran == 1) cEgo.AddInventory(iObject2);

else if (OBJECT3_FOUND == 0 && ran == 2) cEgo.AddInventory(iObject3);

else Display ("Nothing was found.");


Edit: Cassie beat me to it :) Also, I was assuming this code was inside a function to start with; if it's not, then... well, that explains a lot  :-D

Crimson Wizard

Also you may replace OBJECT1_FOUND variables with Game.DoOnceOnly. That is, instead of
Code: ags

if (OBJECT1_FOUND == 0 && (ran==0)) 
    { // note start of new block of code
    cEgo.AddInventory (iObject1); 
    OBJECT1_FOUND = 1;
    }

do
Code: ags

if (Game.DoOnceOnly("OBJECT1_FOUND") && (ran==0)) 
    { // note start of new block of code
    cEgo.AddInventory (iObject1); 
    }


Then you won't need these extra variables.

cdavenport

Thanks for the help and examples, I'll re-work this, there's times I got it working somewhat but then I changed something and it got messed up again - I was using this script when the player clicks on a generic container in a room, so I want to be able to use it throughout many different rooms and containers during the game repeatedly, while keeping track of what the player has found, so they don't get the same thing twice. I was keeping track of the found items in the global script.

A trickier variation is having this same script show a picture of the item you found - I may have to post the pseudo-code I wrote sometimes it works, and sometimes it doesn't. I haven't figured it out yet. I tried creating a dummy character, and every time the player gets an item, I move the dummy character into the player's room, and change its view to display a picture (in the case of the found item), then I move the dummy character back into an unused room. That's the only way I could think of to be able to show a different picture without stacking layers of objects in the room and turning them on/off according to type.

Cassiebsg

You can assign a sprite via code to an object, so you can change the image when the player gets the item and then turn it visible, wait a bit and turn the object off again.
However, this would mean you would need one object per room assign to do this. So better solution would be to either use a dynamic sprite (their scary at start, but once you try them, you'll notice they're actually easy to use and fun  (nod) ). Or use a GUI with a button to display the image. You can then change the image on the GUI, show the gui, wait a while then turn it off again. You can also animate the button image if you like.  ;)
There are those who believe that life here began out there...

cdavenport

Thanks Cassiebsg, I'm slapping my head wondering how I never thought of switching the view of a single object, I'm going to try that. I can't believe I didn't think of that before.  :-\

Laura Hunt

Quote from: Cassiebsg on Sun 02/06/2019 17:30:38
You can assign a sprite via code to an object, so you can change the image when the player gets the item and then turn it visible, wait a bit and turn the object off again.
However, this would mean you would need one object per room assign to do this. So better solution would be to either use a dynamic sprite (their scary at start, but once you try them, you'll notice they're actually easy to use and fun  (nod) ). Or use a GUI with a button to display the image. You can then change the image on the GUI, show the gui, wait a while then turn it off again. You can also animate the button image if you like.  ;)

Cassie, just out of curiosity, why would you use a GUI with a button rather than simply a GUI using the appropriate image/sprite as its background? Is it just a matter of personal preference, or is there an actual advantage to doing it with a button?

Khris

No advantage I can see.

One can also use  Overlay.CreateGraphical()  which I'd argue is the most suitable way of quickly showing a sprite on screen.

Cassiebsg

Maybe cause I normally use buttons as they can be animated... can the background of a GUI be animated?

And listen to Khris, he's the expert, not me.  (nod)  (laugh)
There are those who believe that life here began out there...


eri0o

Not naturally, but there is a module for that.

SMF spam blocked by CleanTalk