(0.6.6) OTClient startup.ogg alternate

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
Hello, I am curious if -
Is it possible to make OTClient choose a random startup music from (1 to 5) ?

Have a good day and thanks for reading me.
 

Shadowsong

Game Developer & Graphic Designer
Joined
Feb 23, 2010
Messages
3,345
Reaction score
2,609
Location
Bosnia & Herzegovina
Hello, I am curious if -
Is it possible to make OTClient choose a random startup music from (1 to 5) ?

Have a good day and thanks for reading me.
In modules\client\client.lua, on the start of the script, add:

Lua:
local startupMusicTracks = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4",
    "/sounds/startup5",
}
Now edit it to contain the filenames/locations of all of your various soundtracks (it can have any amount of them).

Then find:
Lua:
local musicFilename = "/sounds/startup"
and change it to:
Lua:
local musicFilename = startupMusicTracks[math.random(1,#startupMusicTracks)]
Then move that line of code below
Lua:
function startup()
  -- Play startup music (The Silver Tree, by Mattias Westlund)
That should do the trick I think.
 
OP
dantexd

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
Shadowsong im your fan !! Thx for taking your time
Have a great day!
 
OP
dantexd

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
@Shadowsong It says im unable to load client module :(

Code:
local startupMusicTracks = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4",
    "/sounds/startup5",
}


 
local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicFilename = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicFilename, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end


function startup()
local musicFilename = startupMusicTracks[math.random(1,#startupMusicTracks)]
  -- Play startup music (The Silver Tree, by Mattias Westlund)
  musicChannel:enqueue(musicFilename, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicChannel:enqueue(musicFilename, 3)
  end })

 
 

  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()
  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  g_sounds.preload(musicFilename)

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
 

Steve Albert

Banned User
Joined
Dec 9, 2018
Messages
267
Reaction score
100
@Shadowsong It says im unable to load client module :(

Code:
local startupMusicTracks = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4",
    "/sounds/startup5",
}



local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicFilename = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicFilename, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end


function startup()
local musicFilename = startupMusicTracks[math.random(1,#startupMusicTracks)]
  -- Play startup music (The Silver Tree, by Mattias Westlund)
  musicChannel:enqueue(musicFilename, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicChannel:enqueue(musicFilename, 3)
  end })




  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()
  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  g_sounds.preload(musicFilename)

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
You forgot to add this below the table
Lua:
local musicFilename = startupMusicTracks[math.random(1,#startupMusicTracks)]
The beginning of the the script should look like this
Lua:
local startupMusicTracks = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4",
    "/sounds/startup5",
}

local musicFilename = startupMusicTracks[math.random(1,#startupMusicTracks)]
local musicChannel = g_sounds.getChannel(1)
 
OP
dantexd

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
Hello @Steve Albert
I tried adding it there where you placed it (loads client normally) but doesnt randomize between songs only plays 1 always,
and after function startup(client error) but still its only playing one music instead of randomizing

No module errors, nothing happens only plays 1 song hehec:

Code:
local startupMusicTracks = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4",
    "/sounds/startup5",
}

local musicFilename = startupMusicTracks[math.random(1,#startupMusicTracks)]
local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicFilename = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicFilename, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end


function startup()
  -- Play startup music (The Silver Tree, by Mattias Westlund)
  musicChannel:enqueue(musicFilename, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicChannel:enqueue(musicFilename, 3)
  end })

 
 

  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()
  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  g_sounds.preload(musicFilename)

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
 

Steve Albert

Banned User
Joined
Dec 9, 2018
Messages
267
Reaction score
100
Do you have 5 songs named startup1, startup2, startup3, startup4, startup5 in the sounds directory?
 
OP
dantexd

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
Yep, but there is something that isnt letting the script choose from 1 to 5 im really curious now


35305
 

Shadowsong

Game Developer & Graphic Designer
Joined
Feb 23, 2010
Messages
3,345
Reaction score
2,609
Location
Bosnia & Herzegovina
Sorry, I hadn't read the script fully so I made a mistake.
Okay you can try like this - revert the changes I made in the 1st post, and when the script is back to default, under
Lua:
function init()
Add:
Lua:
-- Configure initial music. -----------------------------------
local listOfMusic = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4"
}
 math.randomseed(os.time())
 musicFilename = listOfMusic[math.random(1, #listOfMusic)]
--------------------- End music config -----------------------
Make absolutely sure that every soundtrack that is listed in listOfMusic is a file which exists and works, otherwise the client won't run.
 
OP
dantexd

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
Cant manage to randomize still :C I thought it was working because it was playing another song than usual but doesnt does the random pick yet@Shadowsong

Code:
local musicFilename = "/sounds/startup"
local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicFilename = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicFilename, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end

function startup()
  -- Play startup music (The Silver Tree, by Mattias Westlund)
  musicChannel:enqueue(musicFilename, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicChannel:enqueue(musicFilename, 3)
  end })

  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()

-- Configure initial music. -----------------------------------
local listOfMusic = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4",
}
math.randomseed(os.time())
musicFilename = listOfMusic[math.random(1, #listOfMusic)]
--------------------- End music config -----------------------

  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  g_sounds.preload(musicFilename)

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
 
Last edited:

River KA

Advanced OT User
Joined
Feb 1, 2010
Messages
363
Reaction score
110
Cant manage to randomize still :C I thought it was working because it was playing another song than usual but doesnt does the random pick yet@Shadowsong

Code:
local musicFilename = "/sounds/startup"
local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicFilename = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicFilename, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end

function startup()
  -- Play startup music (The Silver Tree, by Mattias Westlund)
  musicChannel:enqueue(musicFilename, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicChannel:enqueue(musicFilename, 3)
  end })

  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()

-- Configure initial music. -----------------------------------
local listOfMusic = {
    "/sounds/startup1",
    "/sounds/startup2",
    "/sounds/startup3",
    "/sounds/startup4",
}
math.randomseed(os.time())
musicFilename = listOfMusic[math.random(1, #listOfMusic)]
--------------------- End music config -----------------------

  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  g_sounds.preload(musicFilename)

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
Lua:
local musicPaths = {
  "/sounds/startup1",
  "/sounds/startup2",
  "/sounds/startup3",
  "/sounds/startup4",
}
local musicPath = musicPaths[1]
local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicPath = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicPath, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end

function startup()
  musicPath = musicPaths[math.random(#musicPaths)]

  -- Play startup music (The Silver Tree, by Mattias Westlund)
  musicChannel:enqueue(musicPath, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicChannel:enqueue(musicPath, 3)
  end })

  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()
  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  -- preloading musics
  for _, path in ipairs(musicPaths) do
    g_sounds.preload(path)
  end

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
 
OP
dantexd

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
Same problem only focuses on 1 song on opening client aint doing the math randomize

This is my music direction though, dont know if it has something to do

C:\Users\Juan\Desktop\NTOWar\data\sounds

Code:
 math.randomseed(os.time())
musicFilename = listOfMusic[math.random(1, #listOfMusic)]

Code:
  musicPath = musicPaths[math.random(#musicPaths)]
 
OP
dantexd

dantexd

Well-Known Member
Joined
Sep 1, 2010
Messages
93
Reaction score
21
So my problem its that #musicPaths or #listOfMusic arent getting linked inside math randomizing
 

Shadowsong

Game Developer & Graphic Designer
Joined
Feb 23, 2010
Messages
3,345
Reaction score
2,609
Location
Bosnia & Herzegovina
So my problem its that #musicPaths or #listOfMusic arent getting linked inside math randomizing
I have tested the randomization thing, running the client 5 different times, I printed the name of which song is going to be run (and tested math.random further running in that instance to ensure that the os.time() seed produces properly different patterns) and this is the output:
Code:
Playing now: /sounds/startup2
 --- /sounds/startup3
 --- /sounds/startup4
 --- /sounds/startup1
 --- /sounds/startup1
 --- /sounds/startup3
 
Playing now: /sounds/startup1
 --- /sounds/startup4
 --- /sounds/startup1
 --- /sounds/startup4
 --- /sounds/startup4
 --- /sounds/startup2
 
Playing now: /sounds/startup2
 --- /sounds/startup4
 --- /sounds/startup3
 --- /sounds/startup4
 --- /sounds/startup2
 --- /sounds/startup3
 
Playing now: /sounds/startup1
 --- /sounds/startup4
 --- /sounds/startup2
 --- /sounds/startup2
 --- /sounds/startup4
 --- /sounds/startup3
 
Playing now: /sounds/startup2
 --- /sounds/startup1
 --- /sounds/startup1
 --- /sounds/startup1
 --- /sounds/startup1
 --- /sounds/startup3

Plus I'm using 4 different soundtracks and I can hear them changing.
This is still running on this code:

Lua:
local musicFilename = "/sounds/startup1"
local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicFilename = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicFilename, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end

function startup()
  -- Play startup music (The Silver Tree, by Mattias Westlund)

  musicChannel:enqueue(musicFilename, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicChannel:enqueue(musicFilename, 3)
  end })

  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()
    -- Configure initial music. -----------------------------------
    local listOfMusic = {
        "/sounds/startup1",
        "/sounds/startup2",
        "/sounds/startup3",
        "/sounds/startup4",
    }
  math.randomseed(os.time())
  musicFilename = listOfMusic[math.random(1, #listOfMusic)]
  print("Playing now: " .. musicFilename)
 
  for i = 1, 5 do
    print(" --- " ..listOfMusic[math.random(1, #listOfMusic)])
  end
  --------------------- End music config -----------------------
 
  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  g_sounds.preload(musicFilename)

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
 

Shadowsong

Game Developer & Graphic Designer
Joined
Feb 23, 2010
Messages
3,345
Reaction score
2,609
Location
Bosnia & Herzegovina
Yeah, the code I posted to you - I tested it further and posted test results there, works fine here. Are you sure you used the math.randomseed(os.time()) function before calling math.random?

There is a thing in output of math.random - is not truly random, there is a pattern which determines which number it will produce and this pattern is always the same if it always uses the same seed.

By using math.randomseed(os.time()), we set a new seed every time the client is run, and this seed will always be an unique number because os.time() is always a unique number (present time never repeats).

Can you take the code from the last post I pasted and run it a few times then copy the outputs from the terminal to make sure they're really not randomizing properly versus just you having weird RNG and getting the same result several times in a row?
 

River KA

Advanced OT User
Joined
Feb 1, 2010
Messages
363
Reaction score
110
Yeah, the code I posted to you - I tested it further and posted test results there, works fine here. Are you sure you used the math.randomseed(os.time()) function before calling math.random?

There is a thing in output of math.random - is not truly random, there is a pattern which determines which number it will produce and this pattern is always the same if it always uses the same seed.

By using math.randomseed(os.time()), we set a new seed every time the client is run, and this seed will always be an unique number because os.time() is always a unique number (present time never repeats).

Can you take the code from the last post I pasted and run it a few times then copy the outputs from the terminal to make sure they're really not randomizing properly versus just you having weird RNG and getting the same result several times in a row?
You were right about randomseed.
But it has a issue that you can fix it forcing a first unused random as the first time you use the random.
That was mine solution:

Lua:
local musicPaths = {
  "/sounds/startup1",
  "/sounds/startup2",
  "/sounds/startup3",
  "/sounds/startup4",
}

math.randomseed(os.time())
local musicPath = musicPaths[math.random(#musicPaths)] -- First random may be not randomized, but we need to force this to avoid this issue when we need
local musicChannel = g_sounds.getChannel(1)

function setMusic(filename)
  musicPath = filename

  if not g_game.isOnline() then
    musicChannel:stop()
    musicChannel:enqueue(musicPath, 3)
  end
end

function reloadScripts()
  g_textures.clearCache()
  g_modules.reloadModules()

  local script = '/' .. g_app.getCompactName() .. 'rc.lua'
  if g_resources.fileExists(script) then
    dofile(script)
  end

  local message = tr('All modules and scripts were reloaded.')

  modules.game_textmessage.displayGameMessage(message)
  print(message)
end

function startup()
  musicPath = musicPaths[math.random(#musicPaths)]

  g_logger.info(string.format("musicPath: %s", musicPath))

  -- Play startup music (The Silver Tree, by Mattias Westlund)
  musicChannel:enqueue(musicPath, 3)
  connect(g_game, { onGameStart = function() musicChannel:stop(3) end })
  connect(g_game, { onGameEnd = function()
      g_sounds.stopAll()
      musicPath = musicPaths[math.random(#musicPaths)] -- Randomizing music again on logout
      musicChannel:enqueue(musicPath, 3)
  end })

  -- Check for startup errors
  local errtitle = nil
  local errmsg = nil

  if g_graphics.getRenderer():lower():match('gdi generic') then
    errtitle = tr('Graphics card driver not detected')
    errmsg = tr('No graphics card detected, everything will be drawn using the CPU,\nthus the performance will be really bad.\nPlease update your graphics driver to have a better performance.')
  end

  -- Show entergame
  if errmsg or errtitle then
    local msgbox = displayErrorBox(errtitle, errmsg)
    msgbox.onOk = function() EnterGame.firstShow() end
  else
    EnterGame.firstShow()
  end
end

function init()
  connect(g_app, { onRun = startup,
                   onExit = exit })

  g_window.setMinimumSize({ width = 600, height = 480 })
  -- preloading musics
  for _, path in ipairs(musicPaths) do
    g_sounds.preload(path)
  end

  -- initialize in fullscreen mode on mobile devices
  if g_window.getPlatformType() == "X11-EGL" then
    g_window.setFullscreen(true)
  else
    -- window size
    local size = { width = 800, height = 600 }
    size = g_settings.getSize('window-size', size)
    g_window.resize(size)

    -- window position, default is the screen center
    local displaySize = g_window.getDisplaySize()
    local defaultPos = { x = (displaySize.width - size.width)/2,
                         y = (displaySize.height - size.height)/2 }
    local pos = g_settings.getPoint('window-pos', defaultPos)
    pos.x = math.max(pos.x, 0)
    pos.y = math.max(pos.y, 0)
    g_window.move(pos)

    -- window maximized?
    local maximized = g_settings.getBoolean('window-maximized', false)
    if maximized then g_window.maximize() end
  end

  g_window.setTitle(g_app.getName())
  g_window.setIcon('/images/clienticon')

  -- poll resize events
  g_window.poll()

  g_keyboard.bindKeyDown('Ctrl+Shift+R', reloadScripts)

  -- generate machine uuid, this is a security measure for storing passwords
  if not g_crypt.setMachineUUID(g_settings.get('uuid')) then
    g_settings.set('uuid', g_crypt.getMachineUUID())
    g_settings.save()
  end
end

function terminate()
  disconnect(g_app, { onRun = startup,
                      onExit = exit })
  -- save window configs
  g_settings.set('window-size', g_window.getUnmaximizedSize())
  g_settings.set('window-pos', g_window.getUnmaximizedPos())
  g_settings.set('window-maximized', g_window.isMaximized())
end

function exit()
  g_logger.info("Exiting application..")
end
 
Top