Music Switcher Widget

Music Switcher Widget

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 Switcher Widget

Post by url_00 »

Well, I don't know how to program in Lua, so this is probley all worng.
That's why im posting it here! :P
So, this should copy one file, and put it in another, right?

Code: Select all

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

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
	    print("Error loading music.tdf! Check your paths!")
	else
		local music = assert(io.open("../music/music.tdf", "w"))
	end
	if io.open("../music/gutar/music.tdf", "r") == nil
	    print("Error loading gutar's music.tdf! Check your paths!")
	else
		local gutar = assert(io.open("../music/gutar/music.tdf", "r"))
	end
	if io.open("../music/def/music.tdf", "r") == nil
	    print("Error loading default music! Check your paths!")
	else
	    local def = assert(io.open("../music/def/music.tdf", "r")
	end
end

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

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

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

-------------------------------
local choice = ''

function widget:TextCommand(cmd)
	if cmd == 'gutar' then
	    choice = cmd
		Refresh(choice)
		echo("Gutar music loaded.")
	else
	    choice = def
	    Refesh(choice)
	    echo("Error loading music! Check your paths!")
	end
end
	
function Refresh(file)
	local count = 1         -- reads the file, line by line
	while true do
		local line = file:io.read()
		if line == nil then break end
		music:io.write(line,"\n")       -- writes the file, line by line
		count = count + 1
	end
end

-------------------------------------------------------------------------------------
Basicly, I'm trying to make playlists for AF's Music AI thingy.
Will it work?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

due to the fail that is the lua <-> AI interface, or rather the lack of one, I dont see how your going to change the playlist of my groupAI.
User avatar
url_00
Posts: 163
Joined: 15 Apr 2007, 22:44

Post by url_00 »

Im going to edit the music.tdf text file (a.k.a. replace it with another one)
Then restart the Music Ai.
It should work, but I don't know how to copy files in LUA,
so right now, I'm more or less reading only. :cry:

Anyways, I just need to know how to copy a file (like "gutar.tdf") into "music.tdf".
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Post by jK »

@url_00: you search the io and os standard lua libraries.

@AF: groupAIs can add special buttons to the unit commandlist or not? (see moho upgrader etc.), so the groupAI can recieve cmds you give them with lua (with giveordertounit). so there is a lua->groupAI interface ... (that's enough for a "next title" button for example)
User avatar
url_00
Posts: 163
Joined: 15 Apr 2007, 22:44

Post by url_00 »

Argh! My Commands are not working! :evil:
Were text commands disabled?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

If you send a comamnd to a unti udner a grouPAI, the unit recieves it, not the groupAI. You need to send the group a command, not the unit.

The command only shows when the group is selected, not individual units.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

-- LuaUI: select a group, and give the group an order
local selUnits = Spring.GetSelectedUnits()
Spring.SendCommands({'group ' .. groupNum .. ' select'})
Spring.GiveOrder(customID, {param1, param2, etc...}, options)
Spring.SelectUnitArray(selUnits)

P.S. A Spring.GiveOrderToGroup(groupID, cmdID, params, options) would be
nice to have, but the current LuaUI calls can pass custom commands to GroupAIs ;-)
User avatar
url_00
Posts: 163
Joined: 15 Apr 2007, 22:44

Post by url_00 »

AF wrote:If you send a comamnd to a unti udner a grouPAI, the unit recieves it, not the groupAI. You need to send the group a command, not the unit.

The command only shows when the group is selected, not individual units.
I ment like the "/luaui gutar" commands, but thanks for the info. :wink:
Post Reply

Return to “Lua Scripts”