Thread Tools Display Modes
08-01-06, 05:02 AM   #1
Ultroman
A Murloc Raider
Join Date: Nov 2005
Posts: 6
Smile Parsing a chatcommand to do my bidding

Hi.
Well, I've made a few small addons without options at all. They didn't need any.
Now I've thrown myself at a project, inspired by my girlfriend. I'm trying to make an addon to change the standard backpack icon to any icon you might wish for. As I'm using BibToolbars, it is their frame I'm customizing, and it's really not that hard.

What I need help for, is to get the slashcommand /ubp [number] to set my variable UltroBackpackConfig_bagicon to a value between 1 and 43. And to check that I haven't done something totally out of order I just followed WoWiki's HOWTO's, but in between two of them, I got really confused. If there is a much easier way to change the icon used by BibToolbars, than raping their code, I would love to know that too.
Thanks in advance. I really want to get this addon programming going right.

My lua:
Code:
-- Globals
   -- so we know when our configuration is loaded
 UltroBackpack_variablesLoaded = false;
   -- for configuration saving
 UltroBackpackRealm = GetCVar("realmName");
 UltroBackpackChar = UnitName("player");

   -- details used by myAddOns
 UltroBackpack_details = {
	name = "UltroBackpack",
	version = "1.0",
	releaseDate = "August 1, 2006",
	author = "Ultroman the Tacoman",
	email = "[email protected]",
	website = "http://www.alternativementalfeedback.dk/ultropack/",
	category = MYADDONS_CATEGORY_OTHERS,
 };

   -- default config settings
 local UltroBackpackConfig_bagicon = 14; 	-- use Mooncloth bag icon per default


function UltroBackpack_OnLoad()
	SLASH_ULTROBACKPACKSLASH1 = "/ubp";
	SLASH_ULTROBACKPACKSLASH2 = "/ultrobackpack";
	SlashCmdList["ULTROBACKPACKSLASH"] = UltroBackpackCommands;

	-- register events
	this:RegisterEvent("VARIABLES_LOADED"); -- eventually will call OnEvent
       ...
 end

-- OnEvent
 function UltroBackpack_OnEvent()
	-- VARIABLES_LOADED event
	if ( event == "VARIABLES_LOADED" ) then
		-- execute event code in this function
		UltroBackpack_VARIABLES_LOADED();
	end
 end

	<OnLoad>

	</OnLoad>

function UltroBackpack_VARIABLES_LOADED()
	-- initialize our SavedVariable
	if ( not UltroBackpackConfig ) then 
	 	UltroBackpackConfig = {}; 
	end
	if ( not UltroBackpackConfig[UltroBackpackRealm] ) then 
	 	UltroBackpackConfig[UltroBackpackRealm] = {}; 
	end
	if ( not UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar] ) then 
	 	UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar] = {}; 
	end
	-- load each option, set default if not there
	if ( not UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar].bagicon ) then 
		UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar].bagicon = UltroBackpackConfig_bagicon; 
	end

	-- record that we have been loaded
	UltroBackpack_variablesLoaded = true;
	-- we know other addons have been "loaded" now
	   -- optional dependance on myAddOns, leads to our config panel
	if( myAddOnsFrame_Register ) then
		myAddOnsFrame_Register( UltroBackpack_details );
	end
 end
My changes to the BibToolbar XML OnLoad script:
Code:
<OnLoad>
MainMenuBarBackpackButton:Hide();
MainMenuBarBackpackButton:ClearAllPoints();
MainMenuBarBackpackButton:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -100);
MainMenuBarBackpackButton = BibMainMenuBarBackpackButton;
this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	if ( UltroBackpack ) then
		if ( UltroBackpackConfig_bagicon==1 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INV_Misc_Bag_01");
		elseif ( UltroBackpackConfig_bagicon==2 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INV_Misc_Bag_02");
		elseif ( UltroBackpackConfig_bagicon==3 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INV_Misc_Bag_03");
		elseif 
		...............
		<<just changing the icon all the way up to 43, else use the default>>
		...............
		elseif ( UltroBackpackConfig_bagicon==43 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INV_Misc_Bag_Soulbag");
		else
		BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\Buttons\\Button-Backpack-Up");
	end
</OnLoad>
The .toc:
Code:
## Interface: 11100
## Title: UltroBackpack v1.0
## Notes: Replaces the standard backpack texture with the one of your choice
## Author: Ultroman the Tacoman
## DefaultState: Enabled
## SavedVariablesPerCharacter: UltroBackpackConfig_bagicon
## RequiredDeps: BibToolbars
## OptionalDeps: myAddons
UltroBackpack.xml

Last edited by Ultroman : 08-01-06 at 05:05 AM.
  Reply With Quote
08-01-06, 09:18 AM   #2
Tigon
A Fallenroot Satyr
 
Tigon's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 28
SlashCmdList["ULTROBACKPACKSLASH"] = UltroBackpackCommands;
Can I see the UltroBackpackCommands function?
__________________
Tigon (n): A Tigon is the artificially bred hybrid of a male tiger and a female lion. Male tigons are sterile while the females are generally fertile.
http://www.myspace.com/jay_lach
  Reply With Quote
08-01-06, 09:48 AM   #3
MoonWolf
A Murloc Raider
 
MoonWolf's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 7
Looking at that one piece of icon changing code made my head go please god no, so let me give you a small hint.

Code:
if ( UltroBackpack ) then
		if ( UltroBackpackConfig_bagicon==1 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\AddOns\\UltroBackpack\\textures\\INV_Mi  sc_Bag_01");
		elseif ( UltroBackpackConfig_bagicon==2 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\AddOns\\UltroBackpack\\textures\\INV_Mi  sc_Bag_02");
		elseif ( UltroBackpackConfig_bagicon==3 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\AddOns\\UltroBackpack\\textures\\INV_Mi  sc_Bag_03");
		elseif 
		...............
		<<just changing the icon all the way up to 43, else use the default>>
		...............
		elseif ( UltroBackpackConfig_bagicon==43 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\AddOns\\UltroBackpack\\textures\\INV_Mi  sc_Bag_Soulbag");
		else
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\Buttons\\Button-Backpack-Up");
	end
Can be changed to
Code:
if ( UltroBackpack ) then
	if ( UltroBackpackConfig_bagicon < 10 )
          BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\AddOns\\UltroBackpack\\textures\\INV_Mi  sc_Bag_0"..UltroBackpackConfig_bagicon);
	elseif( UltroBackpackConfig_bagicon < 43 )
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\AddOns\\UltroBackpack\\textures\\INV_Mi  sc_Bag_"..UltroBackpackConfig_bagicon);
	elseif( UltroBackpackConfig_bagicon == 43
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\AddOns\\UltroBackpack\\textures\\INV_Mi  sc_Bag_Soulbag");
	else
		BibMainMenuBarBackpackButtonIconTexture:SetTexture  ("Interface\\Buttons\\Button-Backpack-Up");
	end
end
(note, I just wrote that in the editbox there might be a few errors on my part but the idea is valid. )

Have fun and good luck.

The .. operator combines two strings, I assume you did not know this, so now you know.
__________________

Last edited by MoonWolf : 08-01-06 at 09:52 AM.
  Reply With Quote
08-01-06, 10:55 AM   #4
Ultroman
A Murloc Raider
Join Date: Nov 2005
Posts: 6
Post Hi

Thanks for the replies.

@Moonwolf:
Well, the code snippet I submitted was not exact. The many ..... means I've removed a lot of lines doing essentially the same thing, only with different filenames. I can see your point, and maybe I should just rename the files so I would have a nice string of numbers, instead of 07_blue, 07_black and so on. But hey, shouldn't eat too much processing-power.

@Tigon:
Nope, because it isn't written. Actually, all I want is the slash-command to set a variable to a number, resembling a bag-icon in the BibToolbars mod. Per example, number 14 is a Mooncloth Bag icon. So if I type in "/ubp 14" it should save a variable for that character, choosing a Mooncloth Bag as the icon.
I have no idea what is supposed to go in that function, or if I'm missing something totally critical. I'm lacking the missing piece that links this addon together, and I'm sure if I get this working, it should be easier to approach more complex addons.
I guess I'm asking is: How do I make the slashcommands scan for a number typed after them, and save it per character, as a variable I can read from an XML file.

I can explain it easier in C code:
void main()
{
int UltroBackpackConfig_bagicon;
UltroBackpackConfig_bagicon = 1; //set default
printf("Choose your bag-icon:");
scanf("%d",&UltroBackpackConfig_bagicon);
if(UltroBackpackConfig_bagicon==14)
printf("Congratulations, you've chosen a Mooncloth Bag to be your icon");
}

Last edited by Ultroman : 08-01-06 at 11:24 AM.
  Reply With Quote
08-01-06, 05:20 PM   #5
Tigon
A Fallenroot Satyr
 
Tigon's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 28
Oooh, that's easy then =P

Code:
SLASH_ULTROBACKPACKSLASH1 = "/ubp";
SLASH_ULTROBACKPACKSLASH2 = "/ultrobackpack";
SlashCmdList["ULTROBACKPACKSLASH"] = function(msg)
	-- Call to function that handles that icon changing
end
Where it says "-- Call to function.." just call the function that you wrote to change the icon and pass the 'msg' variable. Blizzard will strip one command from a slash command for you, by default =) Here's a simple slash command I wrote for the addon I'm working on.. it might help ya out

Code:
SLASH_JABBERNIXECHO1 = "/echo";
SLASH_JABBERNIXECHO2 = "/print";
SlashCmdList["JABBERNIXECHO"] = function(msg)
	Jabbernix_SlashCommand_Echo(msg);
end

-- The Jabbernix_SlashCommand_Echo function
function Jabbernix_SlashCommand_Echo(msg)
	if ( Jabbernix_StringIsValid(msg) ) then
		Jabbernix_PutChatMsg("|cffffff00>> |r|cffffffff" .. msg .. "|r", SELECTED_CHAT_FRAME);
	end
end
__________________
Tigon (n): A Tigon is the artificially bred hybrid of a male tiger and a female lion. Male tigons are sterile while the females are generally fertile.
http://www.myspace.com/jay_lach
  Reply With Quote
08-02-06, 07:48 AM   #6
Ultroman
A Murloc Raider
Join Date: Nov 2005
Posts: 6
Hi

OK. I think I've done some things totally wrong. I'm having three problems.
1. The slashcommands do not work
2, The variable does not get saved, even though it gets a default setting
3. The icon I'm trying, but failing, to change, moves to it's anchor, instead of getting offset right.

I'm reposting the code I have up until now. I really hope you guys can have a good laugh, and then after that, help me get this right
It is greatly appreciated! You guys are definately a boundless abyss of programming experience. Please do explain my errors in leighman's terms, because I'm really not understanding what is wrong here...And I don't know how I get it to save that darned variable. Oh, and do I use the if to check if my UltroBackpack is loaded right? Is it really just "if( UltroBackpack ) then"?
I have ONE variable. Only one!: UltroBackpackConfig_bagicon
And I want /ubp [number between 1-43] and /ultrobackpack [number between 1-43] to set this variable to the [number between 1-43].

My lua:
Code:
-- Globals
   -- so we know when our configuration is loaded
 UltroBackpack_variablesLoaded = false;
   -- for configuration saving
 UltroBackpackRealm = GetCVar("realmName");
 UltroBackpackChar = UnitName("player");

   -- details used by myAddOns
 UltroBackpack_details = {
	name = "UltroBackpack",
	version = "1.0",
	releaseDate = "August 2, 2006",
	author = "Ultroman the Tacoman",
	email = "[email protected]",
	website = "http://www.alternativementalfeedback.dk/ultropack/",
	category = MYADDONS_CATEGORY_OTHERS,
 };

   -- default config settings
 local UltroBackpackConfig_bagicon = 14;   -- use Mooncloth bag (number 14 in BibToolbars.xml) icon per default

-- I have no idea what I'm supposed to make this function do.
-- 
function UltroBackpackConfig_bagicon(msg)
	if ( UltroBackpackConfig_bagicon_StringIsValid(msg) ) then
		-- ????
	end

-- Create the slashcommands and...I don't know what the function(msg) does to UltroBackpack(msg)
-- Is the name UltroBackpack(msg) supposed to be "myvariable(msg)"? Because then it's wrong as it is :(
function UltroBackpack_OnLoad()
	SLASH_ULTROBACKPACKSLASH1 = "/ubp";
	SLASH_ULTROBACKPACKSLASH2 = "/ultrobackpack";
	SlashCmdList["ULTROBACKPACKSLASH"] = function(msg)
		UltroBackpack(msg);
	end

	-- register events
	this:RegisterEvent("VARIABLES_LOADED"); -- eventually will call OnEvent
       ...
 end

-- OnEvent
 function UltroBackpack_OnEvent()
	-- VARIABLES_LOADED event
	if ( event == "VARIABLES_LOADED" ) then
		-- execute event code in this function
		UltroBackpack_VARIABLES_LOADED();
	end
 end

	<OnLoad>

	</OnLoad>

function UltroBackpack_VARIABLES_LOADED()
	-- initialize our SavedVariable
	if ( not UltroBackpackConfig ) then 
	 	UltroBackpackConfig = {}; 
	end
	if ( not UltroBackpackConfig[UltroBackpackRealm] ) then 
	 	UltroBackpackConfig[UltroBackpackRealm] = {}; 
	end
	if ( not UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar] ) then 
	 	UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar] = {}; 
	end
	-- load each option, set default if not there
	if ( not UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar].bagicon ) then 
		UltroBackpackConfig[UltroBackpackRealm][UltroBackpackChar].bagicon = UltroBackpackConfig_bagicon; 
	end

	-- record that we have been loaded
	UltroBackpack_variablesLoaded = true;
	-- we know other addons have been "loaded" now
	   -- optional dependance on myAddOns, leads to our config panel
	if( myAddOnsFrame_Register ) then
		myAddOnsFrame_Register( UltroBackpack_details );
	end
 end
The change in the BibToolbars.xml. First how it originally is:
Code:
<OnLoad>
MainMenuBarBackpackButton:Hide();
MainMenuBarBackpackButton:ClearAllPoints();
MainMenuBarBackpackButton:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -100);
MainMenuBarBackpackButton = BibMainMenuBarBackpackButton;
this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
-- changing the SetTexture path to one of my own works fine.
-- When I try to use my code you can see in the next section of xml-code,
-- to check for my addon being loaded, it doesn't work anymore
BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\Buttons\\Button-Backpack-Up");
</OnLoad>
This is what my Biboolbars.xml looks like, shortened a bit:
Code:
<OnLoad>
MainMenuBarBackpackButton:Hide();
MainMenuBarBackpackButton:ClearAllPoints();
MainMenuBarBackpackButton:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -100);
MainMenuBarBackpackButton = BibMainMenuBarBackpackButton;
this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	if( UltroBackpack ) then
	if ( UltroBackpackConfig_bagicon==1 )
	BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INVMiscBag_01");
	elseif ( UltroBackpackConfig_bagicon==2 )
	BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INVMiscBag_02");
	elseif ( UltroBackpackConfig_bagicon==3 )
	BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INVMiscBag_03");
-- In between here it just continues to change the number and texture. The filenames are not all the same,
-- with a different number. Some are labelled _Black and such, so just forget that it looks like crap :)
-- If none of the first 42 numbers are right, then "else the 43rd icon texture" here below
	else
	BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\AddOns\\UltroBackpack\\textures\\INVMiscBagSoulbag");
	end
-- if UltroBackpack is not loaded, use the standard BibToolbars texture
else
BibMainMenuBarBackpackButtonIconTexture:SetTexture("Interface\\Buttons\\Button-Backpack-Up");
end
</OnLoad>

Last edited by Ultroman : 08-02-06 at 07:58 AM.
  Reply With Quote
08-02-06, 08:08 AM   #7
Tigon
A Fallenroot Satyr
 
Tigon's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 28
I don't know if that is your complete code, but I see you don't call your OnLoad and Variabes_Loaded functions anywhere =P Also, why is there <OnLoad></OnLoad> inside your lua file? Typo?

Code:
	SLASH_ULTROBACKPACKSLASH1 = "/ubp";
	SLASH_ULTROBACKPACKSLASH2 = "/ultrobackpack";
	SlashCmdList["ULTROBACKPACKSLASH"] = function(msg)
		UltroBackpack(msg);
	end
The SlashCmdList line is creating a function out of that variable... that's what the function(msg) is. Whenever WoW sees /ubp it will run that function, which you have set to run another function named UltroBackpack. Then, inside your UltroBackpack function you can begin to set your new icons =P
__________________
Tigon (n): A Tigon is the artificially bred hybrid of a male tiger and a female lion. Male tigons are sterile while the females are generally fertile.
http://www.myspace.com/jay_lach
  Reply With Quote
08-02-06, 08:51 AM   #8
Ultroman
A Murloc Raider
Join Date: Nov 2005
Posts: 6
Hi

But, the icons are being changed in the BibToolbars XML file. They're listed in there. Is that wrong? Should the whole change to the XML file actually be in the lua function? And will that override the texture setting in the original BibToolbars.xml?

How do I make this happen:
UltroBackpackConfig_bagicon = the variable we set with the slash command

Last edited by Ultroman : 08-02-06 at 09:02 AM.
  Reply With Quote
08-02-06, 11:58 AM   #9
Tigon
A Fallenroot Satyr
 
Tigon's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 28
OOohhh... That, sadly, I don't know that much about.. I've never really worked with textures and such. I reckon you could edit the Bibmod config variable tho by access the table in your lua file..

for actually reading the number that was given with the slash, you can do something like this...
Code:
	SLASH_ULTROBACKPACKSLASH1 = "/ubp";
	SLASH_ULTROBACKPACKSLASH2 = "/ultrobackpack";
	SlashCmdList["ULTROBACKPACKSLASH"] = function(msg)
		UltroBackpack_ChangeIcon(msg);
	end

	function UltroBackpack_ChangeIcon(msg)
		UltroBackpackConfig_bagicon = msg;
		-- Your code to change the icon
	end
The variable "msg" will be the number that the user gave. For example: If you do /ubp 13 the msg variable will, by default, be 13. Also, with that said if you did /ubp 13 somethingElse the msg variable would be "13 somethingElse" and you could then parse that message furter to split "13" and "somethingElse"
__________________
Tigon (n): A Tigon is the artificially bred hybrid of a male tiger and a female lion. Male tigons are sterile while the females are generally fertile.
http://www.myspace.com/jay_lach
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Parsing a chatcommand to do my bidding


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off