Random encounters

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Syl

Greets!

Would someone have an hint about implementin those hazardous occurences on a zone/map? Wich nodes? Area2d? But with what kind of functions in script please?

:bust_in_silhouette: Reply From: BraindeadBZH

Can you better describe what you want to achieve, give a description of the gameplay.

Well, got the overview of a forest map, with a sprite/player movin in, and I’d like those random encounters to happen dependin on the location, changin scene to show the outcome.
See? Actually, don’t know how to script that chance. I know how to change scene from area2D collision, but I’d like it randomly, from a list of NPCs or events, and durin the crossin of the zone, not specially when enterin. A continual check.

Syl | 2018-03-26 17:20

:bust_in_silhouette: Reply From: Bartosz

To make encounters random proceed like with all random stuff e.g. use pseudorandom number generator (PRNG) e.g. randi()

To have some probability in encounter creation check if randi() % 100 < chance_in_percents is true, if it is, use below solution to generate random encounter.

you can use dictionaries as base of rolling out some encounters:

var encounter = {
	"encounter_type": [ "Battle with", "Quest from"],
	"encounter_person": [ "Bandit", "Dragon", "Knight", "Rat", "Talking Sword" ],
	"encounter_person_kind": [ "drunk", "poor", "hungry", "bald", "old" ],
	"encounter_reason": [ "wrong place wrong type", "you looked tasty", "you're not batman", "that's the way thinks work around here" ]
}

then you can use function similar to this one to generate new encounter

func roll(something):
	var result = {}
	for k in something.keys():
		var values = something[k]
		result[k] = values[randi() % values.size()]
	return result
  

here working demo containing above code in my godot-recipes project

To make it “continual” either measure distance player travels through area and after do “chance-check” in predefined intervals. Alternatively split areas in to multiple smaller - more areas will be the more continuous effect.

Man, I ask for a pebble, you give a gem… May you receive the same in your all your requests! :smiley:

Syl | 2018-03-26 22:00

when you finish your game remember to mention in credits really helpful godot community XD

Bartosz | 2018-03-26 22:11

Sure, with special mention for Bartosz! :smiley:

Syl | 2018-03-26 22:25