diff --git a/src/scripting/lua_audio.cpp b/src/scripting/lua_audio.cpp index 8df985b3bf3..f8ede652b7d 100644 --- a/src/scripting/lua_audio.cpp +++ b/src/scripting/lua_audio.cpp @@ -96,11 +96,11 @@ static int impl_music_get(lua_State* L) { } if(strcmp(m, "current_i") == 0) { - size_t i = sound::get_current_track_index(); - if(i >= sound::get_num_tracks()) { - lua_pushnil(L); + auto current_index = sound::get_current_track_index(); + if(current_index) { + lua_pushinteger(L, current_index.value() + 1); } else { - lua_pushinteger(L, i + 1); + lua_pushnil(L); } return 1; } diff --git a/src/sound.cpp b/src/sound.cpp index 1f6314aa548..1d0dd601eaa 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -191,8 +191,11 @@ std::vector>::const_iterator find_track(cons namespace sound { -unsigned int get_current_track_index() +boost::optional get_current_track_index() { + if(current_track_index >= current_track_list.size()){ + return {}; + } return current_track_index; } std::shared_ptr get_current_track() diff --git a/src/sound.hpp b/src/sound.hpp index c1707c5e9ff..fd98c982c56 100644 --- a/src/sound.hpp +++ b/src/sound.hpp @@ -17,6 +17,7 @@ #include "events.hpp" #include "sound_music_track.hpp" +#include #include class config; @@ -104,7 +105,7 @@ void set_sound_volume(int vol); void set_bell_volume(int vol); void set_UI_volume(int vol); -unsigned int get_current_track_index(); // This function may return a value >= get_num_tracks(). Use with caution +boost::optional get_current_track_index(); std::shared_ptr get_current_track(); std::shared_ptr get_previous_music_track(); void set_previous_track(std::shared_ptr);