Jibble

Author Topic: Random Treasure (Why doesn't this work???)  (Read 275 times)

Random Treasure (Why doesn't this work???)
« on: 02 Jun 2019, 14:11 »
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: Adventure Game Studio
  1.  
  2. int ran=Random(3);
  3. if (OBJECT1_FOUND == 0 && (ran==0) cEgo.AddInventory (iObject1)) {
  4.  
  5. }
  6. else if (OBJECT2_FOUND == 0 && (ran==1) cEgo.AddInventory (iObject2)) {
  7.  
  8. }
  9. else if (OBJECT3_FOUND == 0 && (ran==2) cEgo.AddInventory (iObject3)) {
  10. }
  11. else {
  12.   Display ("Nothing was found.");
  13.  
  14. }
  15.  
  16.  

Cassiebsg

  • Cavefish
  • Fleeing the Cylon tyrrany...
    • Cassiebsg worked on one or more games that won an AGS Award!
    •  
    • Cassiebsg worked on one or more games that was nominated for an AGS Award!
Re: Random Treasure (Why doesn't this work???)
« Reply #1 on: 02 Jun 2019, 14:43 »
Quote
Code: Adventure Game Studio
  1.  
  2. int ran=Random(3);
  3. if (OBJECT1_FOUND == 0 && (ran==0)   cEgo.AddInventory (iObject1))
  4.   {}
  5. else if (OBJECT2_FOUND == 0 && (ran==1)   cEgo.AddInventory (iObject2))
  6.   {}
  7. else if (OBJECT3_FOUND == 0 && (ran==2)    cEgo.AddInventory (iObject3))
  8.   {}
  9. else
  10.  {
  11.   Display ("Nothing was found.");
  12.   }
  13.  
  14.  

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: Adventure Game Studio
  1. function BoxLuckyDip() // giving it a name you can easy remember when you wish to use it.
  2. { // this starts this block of code
  3.   int ran=Random(3);
  4.   if (OBJECT1_FOUND == 0 && (ran==0)) cEgo.AddInventory (iObject1); // Note and remember to close the parenthesis, then add the command and end it with ;
  5.   else if (OBJECT2_FOUND == 0 && (ran==1)) cEgo.AddInventory (iObject2);
  6.   else if (OBJECT3_FOUND == 0 && (ran==2)) cEgo.AddInventory (iObject3);
  7.   else Display ("Nothing was found.");
  8. } // this ends the function and corresponding block of code
  9.  

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: Adventure Game Studio
  1. function BoxLuckyDip()
  2. {
  3.   int ran=Random(3);
  4.   if (OBJECT1_FOUND == 0 && (ran==0))
  5.     { // note start of new block of code
  6.     cEgo.AddInventory (iObject1);
  7.     OBJECT1_FOUND = 1;
  8.     } // note end of this block of code...
  9.   else if (OBJECT2_FOUND == 0 && (ran==1))
  10.     {
  11.     cEgo.AddInventory (iObject2);
  12.     OBJECT2_FOUND = 1;
  13.     }
  14.   else if (OBJECT3_FOUND == 0 && (ran==2))
  15.     {
  16.     cEgo.AddInventory (iObject3);
  17.     OBJECT3_FOUND = 1;
  18.     }
  19.   else Display ("Nothing was found.");
  20. }
  21.  

Hope this helps.
« Last Edit: 02 Jun 2019, 14:45 by Cassiebsg »
There are those who believe that life here began out there...

Re: Random Treasure (Why doesn't this work???)
« Reply #2 on: 02 Jun 2019, 14:44 »
Ummm something I can see right off the bat is that your brackets are all messed up, hence the parsing errors. Try this:

Code: Adventure Game Studio
  1. if (OBJECT1_FOUND == 0 && ran == 0) cEgo.AddInventory(iObject1);
  2.  
  3. else if (OBJECT2_FOUND == 0 && ran == 1) cEgo.AddInventory(iObject2);
  4.  
  5. else if (OBJECT3_FOUND == 0 && ran == 2) cEgo.AddInventory(iObject3);
  6.  
  7. 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
« Last Edit: 02 Jun 2019, 14:48 by notarobotyet »

Re: Random Treasure (Why doesn't this work???)
« Reply #3 on: 02 Jun 2019, 14:54 »
Also you may replace OBJECT1_FOUND variables with Game.DoOnceOnly. That is, instead of
Code: Adventure Game Studio
  1. if (OBJECT1_FOUND == 0 && (ran==0))
  2.     { // note start of new block of code
  3.     cEgo.AddInventory (iObject1);
  4.     OBJECT1_FOUND = 1;
  5.     }
  6.  
do
Code: Adventure Game Studio
  1. if (Game.DoOnceOnly("OBJECT1_FOUND") && (ran==0))
  2.     { // note start of new block of code
  3.     cEgo.AddInventory (iObject1);
  4.     }
  5.  

Then you won't need these extra variables.

Re: Random Treasure (Why doesn't this work???)
« Reply #4 on: 02 Jun 2019, 17:23 »
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

  • Cavefish
  • Fleeing the Cylon tyrrany...
    • Cassiebsg worked on one or more games that won an AGS Award!
    •  
    • Cassiebsg worked on one or more games that was nominated for an AGS Award!
Re: Random Treasure (Why doesn't this work???)
« Reply #5 on: 02 Jun 2019, 17:30 »
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...

Re: Random Treasure (Why doesn't this work???)
« Reply #6 on: 03 Jun 2019, 15:15 »
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.  :-\

Re: Random Treasure (Why doesn't this work???)
« Reply #7 on: 04 Jun 2019, 12:56 »
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

  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on one or more games that was nominated for an AGS Award!
Re: Random Treasure (Why doesn't this work???)
« Reply #8 on: 04 Jun 2019, 13:54 »
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

  • Cavefish
  • Fleeing the Cylon tyrrany...
    • Cassiebsg worked on one or more games that won an AGS Award!
    •  
    • Cassiebsg worked on one or more games that was nominated for an AGS Award!
Re: Random Treasure (Why doesn't this work???)
« Reply #9 on: 04 Jun 2019, 16:50 »
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...

Re: Random Treasure (Why doesn't this work???)
« Reply #10 on: 05 Jun 2019, 15:37 »
Maybe cause I normally use buttons as they can be animated... can the background of a GUI be animated?

Good question... I have no idea!

Re: Random Treasure (Why doesn't this work???)
« Reply #11 on: 05 Jun 2019, 23:06 »
Not naturally, but there is a module for that.