Music Switch Widget not working...

Music Switch Widget not working...

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
url_00
Posts: 163
Joined: 15 Apr 2007, 22:44

Music Switch Widget not working...

Post by url_00 »

Could sombody help me with this?
I'm new to Lua.

Code: Select all

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:   cmd_switchmusic.lua
--  brief:   switches the music.tdf to another file
--  author:  URL_00
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

function widget:GetInfo()
  return {
    name      = "Music Switcher",
    desc      = "Switches the music playlist of AF's Music AI",
    author    = "url_00",
    date      = "04 Aug 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 1,
    enabled   = false  -- loaded by default?
  }
end

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

function widget:Initialize()
	if io.open ("\music\music.tdf", "w") == nil
    then 
        echo("Error loading music.tdf! Check your paths!")
	else
		local music = io.open ("\music\music.tdf", "w")
	end
	if io.open ("\music\gutar.tdf", "r") == nil
    then
        echo("Error loading gutar's music.tdf! Check your paths!")
	else
		local gutar = io.open ("\music\gutar.tdf", "r")
	end
end

-------------------------------

function Say(msg)
	Spring.SendCommands({'say ' .. msg})
end

function echo(msg)
	Spring.SendCommands({"echo " .. msg})
end

-------------------------------

function widget:TextCommand(cmd)
	local choice = ''
	if cmd == 'gutar' then
	    choice = gutar
		Refresh(choice, music)
		echo("Gutar music loaded.")
	else
	    choice = music
	    Refesh(choice, music)
	    echo("Error loading music! Check your paths!")
	end
end
	
function Refresh(file, music)
    for line in file:lines() do
        music:write(line)
    end
    file:close()
end

-------------------------------------------------------------------------------------
It loads 'music.tdf' fine, but it can't load 'gutar.tdf' for some reason.
Any ideas?
User avatar
Neddie
Community Lead
Posts: 9406
Joined: 10 Apr 2006, 05:05

Post by Neddie »

Is gutar.tdf...

A. In the right location?
B. Saved as a .tdf?
User avatar
url_00
Posts: 163
Joined: 15 Apr 2007, 22:44

Post by url_00 »

yes

yes
User avatar
theHive
Posts: 124
Joined: 13 May 2007, 06:54

Post by theHive »

if you try it with the second parameter as a "w" does it help?

apart from not doing what you want, i mean
User avatar
url_00
Posts: 163
Joined: 15 Apr 2007, 22:44

Post by url_00 »

theHive wrote:if you try it with the second parameter as a "w" does it help?

apart from not doing what you want, i mean
Yay! now it almost works! :P
Thx for the help, I think somthing is wrong with my functions...
Post Reply

Return to “Lua Scripts”