GameMaker Choosing a random event

Roalinn

Member
I want to make game like Oregon Trail so some events will appear randomly.

An example:
- event 1: you saw cring baby.
- event 2: someone give you 10 dollar.
- event 3: you dropped 10 dollar.

and game starts...then walking 10m randomly choose event 1 or 2 or 3.

what should i use for this ? (ds_list ? array ?)
 
I personally would use enums or strings (if you want to use an included file to read the events in) in a switch statement. Then if you want to store your history a ds_list. Enums and strings are more readable and enums easily randomized. Of those two ds_list is my preferred choice.
 

NightFrost

Member
You'll first need a timer that counts down to game event selection, but you probably can do that. The random event selection just needs to randomly choose from amongst your defined game events, so either having them stored in a ds list or an array is fine, your selection process just gets their length and chooses with irandom(Event_List_Length - 1). How you structure your event data depends on your project, but for an Oregon Trail-ish game you need at least a message, that is displayed to player, and effect, which tells the game how to wrangle any numbers if necessary. The effect data would be subdivided to at least two data points: what changes, and by how much it changes. The crying baby event would have no effect (unless you can pick them up for the ride), the money events would both say that the amount of money changes, for the first the amount would be plus ten and for the second, minus ten. The effects should be best listed out as an enum, for ease of use.
 
D

dannyjenn

Guest
There are various ways in which you could do this. If you're ok with the same event coming up twice, then it could be as simple as using a single line of code (no need for arrays or ds_lists or anything):
Code:
// every 10 meters (minutes?):
choose_event(choose("you saw cring baby.","someone give you 10 dollar.","you dropped 10 dollar.")); // <-- where choose_event is your script that displays the message or whatever

But that's awkward and probably not as flexible as you want. A better way would be this:
Code:
// game start:

// first you make an array with all the various messages in it:
events[0] = "you saw cring baby.";
events[1] = "someone give you 10 dollar.";
events[2] = "you dropped 10 dollar.";

// then you make a ds_list to keep track of the deck (think of it as a deck of cards).
deck = ds_list_create();
// Assuming you want every event to be weighted equally, i.e. to have the same probability, you'd do this:
ds_list_add(deck,0);
ds_list_add(deck,1);
ds_list_add(deck,2);
// After doing that, deck[|0] should be 0, deck[|1] should be 1, and deck[|2] should be 2, such that event #0 has a 1/3 chance of coming up, event #1 has a 1/3 chance of coming up, and event #2 has a 1/3 chance of coming up

// Optional: If you want e.g. event #0 to come up twice as frequently, you'd just add another one:
ds_list_add(deck,0);
// After all that, deck[|0] should be 0, deck[|1] should be 1, deck[|2] should be 2, and deck[|3] should be 0, such that event #0 has a 2/4 chance of being chosen, and event #1 and #2 each have a 1/4 chance of being chosen.

ds_list_shuffle(deck); // shuffle the deck
Code:
// every 10 meters (minutes?):
choose_event(events[deck[|0]]); // <-- where choose_event is the script you use for displaying each event
ds_list_shuffle(deck,0); // <-- This is assuming you want the events to be able to come up more than once. But if you don't want the events to be able to come up more than once, you can simply replace that line with: ds_list_delete(deck);
Code:
// game end:
ds_list_destroy(deck);

Again, there are various other ways of doing this sort of thing.
 
Last edited by a moderator:

Roalinn

Member
There are various ways in which you could do this. If you're ok with the same event coming up twice, then it could be as simple as using a single line of code (no need for arrays or ds_lists or anything):
Code:
// every 10 meters (minutes?):
choose_event(choose("you saw cring baby.","someone give you 10 dollar.","you dropped 10 dollar.")); // <-- where choose_event is your script that displays the message or whatever

But that's awkward and probably not as flexible as you want. A better way would be this:
Code:
// game start:

// first you make an array with all the various messages in it:
events[0] = "you saw cring baby.";
events[1] = "someone give you 10 dollar.";
events[2] = "you dropped 10 dollar.";

// then you make a ds_list to keep track of the deck (think of it as a deck of cards).
deck = ds_list_create();
// Assuming you want every event to be weighted equally, i.e. to have the same probability, you'd do this:
ds_list_add(deck,0);
ds_list_add(deck,1);
ds_list_add(deck,2);
// After doing that, deck[|0] should be 0, deck[|1] should be 1, and deck[|2] should be 2, such that event #0 has a 1/3 chance of coming up, event #1 has a 1/3 chance of coming up, and event #2 has a 1/3 chance of coming up

// Optional: If you want e.g. event #0 to come up twice as frequently, you'd just add another one:
ds_list_add(deck,0);
// After all that, deck[|0] should be 0, deck[|1] should be 1, deck[|2] should be 2, and deck[|3] should be 0, such that event #0 has a 2/4 chance of being chosen, and event #1 and #2 each have a 1/4 chance of being chosen.

ds_list_shuffle(deck); // shuffle the deck
Code:
// every 10 meters (minutes?):
choose_event(events[deck[|0]]); // <-- where choose_event is the script you use for displaying each event
ds_list_shuffle(deck,0); // <-- This is assuming you want the events to be able to come up more than once. But if you don't want the events to be able to come up more than once, you can simply replace that line with: ds_list_delete(deck);
Code:
// game end:
ds_list_destroy(deck);

Again, there are various other ways of doing this sort of thing.
Thanks a lot :D
 
Top