Randomized Room Event

Started by cdavenport, Sun 19/05/2019 15:07:43

Previous topic - Next topic

cdavenport

I wanted to try experimenting with a randomized event which would be triggered by the player entering a room, or walking over a region. It would be like a board game mechanic, where the player has to draw a card from a Chance deck, which could be helpful or harmful to them (damages health, gives them inventory item, teleports them, etc.).

So if you had a deck of 25 different cards, 1 of which you wanted to randomly pick and display in the room like a pop-up picture (object/sprite), what might be the easiest path to research doing that for a novice scripter? Does anything come to mind in past forum topics or help, I've been searching under random events but haven't yet found something similar.

I had thought of creating a dummy character, moving it to the player's room, and then changing the view to display 1 of 25 possible outcomes followed by its effect on the player. But it seems like every time you wanted that to happen, you would have to have long, convoluted if/else statements (if random = 1 change characters view #, etc.).

The only way I could think of is by adding this to the Repeated Execute in the Global Script, but I am wondering if this is the best way --

Code: ags


  //RANDOMIZED ITEM
  
  if (RANDOMIZED_ITEM == 1) {
    cRandomItem.ChangeRoom (player.Room);

  cRandomItem.Transparency = 100;
  
    cRandomItem.Move (150, 210, eBlock, eWalkableAreas);

    int ran=Random(2);
if (ran==0) cRandomItem.ChangeView (4);//#1
else if (ran==1) cRandomItem.ChangeView(5);//#2
else cRandomItem.ChangeView(6);//#3

    RANDOMIZED_ITEM = 2;
       
  cRandomItem.Transparency = 0;
  
  }



I don't really understand the more advanced ways of doing something like this with more elegant coding, but I'm hoping I can find some experiment with if anyone has any suggestions or can point me in the right direction. Thanks!

eri0o

#1
Which version of AGS are you using?
You can use an array to store you map of random result->view number - you can write the name of the view that will work too since ags creates a preprocessor entry per view name matching it's number.
Also if you indent your code it won't look so convoluted.
You could also use Switch case statements.

cdavenport

Quote from: eri0o on Sun 19/05/2019 19:22:40
Which version of AGS are you using?

Thanks I'm using AGS Editor .NET (Build 3.4.1.15), I'll look into those if anyone knows of any close examples you can point me too that I could pick up on and experiment with, it would be helpful to see I'm not that good at scripting them from scratch through trial and error. The above example seems to work but I was just wondering if there was a more efficient way of doing it.

Khris

One small thing you can do is replace those three ChangeView lines with
Code: ags
  cRandomItem.ChangeView(ran + 4);

SMF spam blocked by CleanTalk