Jump to content

How to add 2 songs in the same event and both of them having a 50/50 chance of playing?


Recommended Posts

I guess one method you can do is to store your strings in a table:

then call them using a math.random style function:

function GLOBAL.randomindex(t) --Selects a random item from a table
    local keys = {}
    for key, value in pairs(t) do
        keys[#keys+1] = key --Store keys in another table
    end
    local index = keys[math.random(1, #keys)]
    return t[index]
end

you can place this in modmain then call it anywhere as randomindex(table) it'll return 1 key at random from your table

--make a table accessable to your soundemitter:
local playlist = {
	"Doukutsu no Oku",
	"Kemonotachi no Ikidukai",
}
--I am not 100% sure how music is called but this is how you can do it at least for sounds
SoundEmitter:PlaySound("kemonosoundtrack/music/"..randomindex(playlist), ourmusicvariable)

Edit:
I also suggest to use simpler variable names for your songs etc, as it makes it much less likely for typos or other human errors when coding.

Edited by IronHunter
Link to comment
Share on other sites

8 hours ago, IronHunter said:

I guess one method you can do is to store your strings in a table:

then call them using a math.random style function:


function GLOBAL.randomindex(t) --Selects a random item from a table
    local keys = {}
    for key, value in pairs(t) do
        keys[#keys+1] = key --Store keys in another table
    end
    local index = keys[math.random(1, #keys)]
    return t[index]
end

you can place this in modmain then call it anywhere as randomindex(table) it'll return 1 key at random from your table


--make a table accessable to your soundemitter:
local playlist = {
	"Doukutsu no Oku",
	"Kemonotachi no Ikidukai",
}
--I am not 100% sure how music is called but this is how you can do it at least for sounds
SoundEmitter:PlaySound("kemonosoundtrack/music/"..randomindex(playlist), ourmusicvariable)

Edit:
I also suggest to use simpler variable names for your songs etc, as it makes it much less likely for typos or other human errors when coding.

Ahm, i don't know if this is right or not, i'm trying to add the code of the randomindex in the dynamicmusic.lua

5bce8ba1e1e0a_ScreenShot_20181022214338-copia.png.1e328ff47fc7b6f181c3ccf366bee2fb.png

And in the client_log appears this after it instantly crashes

5bce8ba292eb3_ScreenShot_20181022214635-copia.thumb.png.6ae8ec2912b644df2ab95af9d0bc2457.png

Sorry for that, but i'm not so experienced at this nor know if it can be added in the dynamic music or it has to be yes or yes a remap sound event or sound emitter

Edited by Puka_Picante
More info added
Link to comment
Share on other sites

if _innightmare then
  	_soundemitter:PlaySound("dontstarve/music/music_work_ruins")
elseif _iscave then
	_soundemitter:PlaySound("kemonosoundtrack/music/"..randomindex(playlist), "ourmusicvariable")
else
  	_soundemitter:PlaySound(SEASON_BUSY_MUSIC[phase][season],"busy")
end

I think a better method is to use if statements instead, this is really cluttered a quick check of the dynamicmusic shows them doing this method.
I mean you can try using the other method, but it is so much more legible this way.

I can't make heads or tails of your error log, as I don't know what that line 170 is referencing, but it looks like one of your brackets is in the wrong spot based on the error.

Edited by IronHunter
Fixed typo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...