0 votes

So I am using a randomize() function to shuffle a music playlist in my game and am also using randomize() to shuffle a "treasure array" so that items picked up are never in the same spot. Problem is my music is shuffling anytime I shuffle the array. Pretty sure the RNG seeding is conflicting, causing my tracks to shuffle whenever my event is called - how would I go about fixing this? I have tried also creating a separate random number generator and am getting the same result.

Code for universal sound object:

    extends Node


## get nodes!
onready var _calm = get_node("Calm")
onready var _pursuit = get_node("Pursuit")
onready var _hidden = get_node("Hidden")
onready var _escape = get_node("Escape")
onready var _chance = get_node("ChanceMusic")
onready var _death = get_node("DeathMusic")
onready var _sfx = get_node("SoundEffects")

## rng containers
var _calmTrack = 0
var _chaseTrack = 0
var _nowPlaying = true
var _myrng = RandomNumberGenerator.new()

## resource enumerations
const _calmTracks = [
    "res://music/calm/Attic_Springs.ogg",
    "res://music/calm/NoHome.ogg",
    "res://music/calm/Seeking.ogg",
    "res://music/calm/VoidSoul.ogg",
    "res://music/calm/Wave.ogg",
    "res://music/calm/Doors.ogg"
]

const _pursuitLoops = [
    "res://music/tense/hurt_loop1.ogg",
    "res://music/tense/mono_loop2.ogg",
    "res://music/tense/noesc_loop2.ogg",
    "res://music/tense/reven_loop1.ogg",
    "res://music/tense/run_loop.ogg"
]

const _hiddenLoops = [
    "res://music/tense/hurt_loop2.ogg",
    "res://music/tense/mono_loop1.ogg",
    "res://music/tense/noesc_loop3.ogg",
    "res://music/tense/reven_loop2.ogg",
    "res://music/tense/run_loop2.ogg"
]

const _escapeShots = [
    "res://music/tense/hurt_end.ogg",
    "res://music/tense/mono_end.ogg",
    "res://music/tense/noesc_end.ogg",
    "res://music/tense/reven_end.ogg",
    "res://music/tense/run_end.ogg"
] ### one shots not loops!!!

const _sceneBGM = [
    "res://music/calm/Doors.ogg",
    "res://music/calm/Vessel.ogg",
    "res://music/tense/springs_end.ogg",
    "res://music/tense/doomed_loop.ogg",
    "res://music/tense/dead_loop.ogg"
]

const _soundFX = [
    "res://music/sfx/ambient_hit.ogg",
    "res://music/sfx/chugfare.ogg"
]


## connect calm loop at ready
func _ready():
    var _reset_ = _escape.connect("finished",self,"_calm_music")
    var _calmLoop = _calm.connect("finished",self,"_calm_music")


## update music state each frame
func _process(_delta):
    if _nowPlaying:
        if Director._inPursuit == false and _calm.playing == false:
            _calm_music()

        if Director._inPursuit == true and Director._playerIsHidden == false and _pursuit.playing == false:
            if Director._nowDead == false:
                _chance.stop()
                _pursuit_music()
            else:
                pass

        if Director._enemyHere == true and Director._playerIsHidden == true and _hidden.playing == false:
            _hidden_music()

        if Director._chanceEvent == true and _chance.playing == false:
            _pursuit.stop()
            _chance_music()

        if Director._nowDead == true and _death.playing == false:
            _chance.stop()
            _dead_music()


    if _nowPlaying == false:
        _stop_bgm()

    if Director._eventFlag == true:
        _pursuit.stop()
        _hidden.stop()



## calm or default music
func _calm_music():
    _stop_bgm()

    if _calm.playing == false and _escape.playing == false:
        var _myCalm = _calmTracks[_calmTrack]
        _calm.stream = load(_myCalm)
        _calm.play()
    if _calm.playing == false and _escape.playing == true:
        yield(_escape,"finished")
        var _myCalm = _calmTracks[_calmTrack]
        _calm.stream = load(_myCalm)
        _calm.play()

    if Director._inPursuit == true:
        _pursuit_music()

    if _calm.playing == false:
        _shuffle_bgm()

## pursuit music
func _pursuit_music():
    _stop_bgm()
    if Director._chanceEvent == false:
        _shuffle_bgm()
    else:
        pass


    if _pursuit.playing == false:
        var _myStress = _pursuitLoops[_chaseTrack]
        _pursuit.stream = load(_myStress)
        _pursuit.play()

    if Director._playerIsHidden == true:
        _hidden_music()


## hidden music
func _hidden_music():
    _stop_bgm()

    if _hidden.playing == false:
        var _mySuit = _hiddenLoops[_chaseTrack]
        _hidden.stream = load(_mySuit)
        _hidden.play()

    if Director._playerIsHidden == false:
        _pursuit.play()
        _hidden.stop()


## escape function, one shot that connects back to calm loop
func _escape_music():
    _shuffle_bgm()
    _stop_bgm()
    var _myEsc = _escapeShots[_chaseTrack]
    _escape.stream = load(_myEsc)
    _escape.play()


## last chance bgm
func _chance_music():
    _stop_bgm()
    var _myChance = _sceneBGM[3]
    _chance.stream = load(_myChance)
    _chance.play()


## dead music, death screen
func _dead_music():
    _dead_noise()
    var _myDeath = _sceneBGM[4]
    _death.stream = load(_myDeath)
    _death.play()


### clunk at death screen (transition noise one-shot)
func _dead_noise():
    var _deathsound_ = "res://music/sfx/ambient_hit.ogg"
    _sfx.stream = load(_deathsound_)
    _sfx.play()


## title theme, continues into game on start then loops back to start
func _title_music():
    var _myTitle = _sceneBGM[0]
    _calm.stream = load(_myTitle)
    _calm.play()


## stop all music
func _stop_bgm():
    _chance.stop()
    _death.stop()
    _pursuit.stop()
    _hidden.stop()
    _calm.stop()



## rng function to change song pull
func _shuffle_bgm():
    _myrng.randomize()
    _calmTrack = _myrng.randi_range(0,5)## this method avoids errors
    _chaseTrack = _myrng.randi_range(0,4) 

Then my "treasure" function.

    extends "res://events/BasicEventShell.gd"

onready var _look = $CanLook
onready var _text = $TextBox
export var _myKeyName : String ## set in editor or events consume each other. Gross.
export var _myText : String ## set dialogue message


# set key name and signals, remove prompt
func _ready():
    _myEventKey = _myKeyName
    var _p_on = self.connect("body_entered",self,"_prompt_on")
    var _p_off = self.connect("body_exited",self,"_prompt_off")
    _look.visible = false


## hide prompt when consumed
func _process(_delta):
    if _eventToggle:
        _look.visible = false


# randomize seed, pull treasure and toggle off
func _thisEvent():
    Director._eventFlag = true
    randomize()
    StageHand._treasureArray.shuffle()
    var _loot = StageHand._treasureArray.pop_front()
    _text._update_(str(_myText) + str(_loot) + "...")
    _text._show_()
    _eventToggle = true


## turning prompts on and off, and staying off when consumed
func _prompt_on(body):
    if body.name == 'Lydia' and _eventToggle == false:
        _look.visible = true


func _prompt_off(body):
    if body.name == 'Lydia':
        _look.visible = false


## remove text on second button press, do not allow until text is full
func _input(_event):
    if _eventToggle == true and _text._text_display == true:
        if Input.is_action_just_pressed("ui_accept"):
            _text._vanish_()
        if Input.is_action_just_pressed("ui_left"):
            _text._vanish_()
        if Input.is_action_just_pressed("ui_right"):
            _text._vanish_()
Godot version v3.4.3.stable.official [242c05d12]
in Engine by (548 points)

1 Answer

0 votes

Nevermind on that, I decided on a different format for my music. Since all of Godot's RNG uses a seed generator, I am giving myself too much work trying to segregate these. I also just noticed the randomize is in the process function, which is probably the cause.

by (548 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.