Thursday, April 19, 2012

Need a little help....

Getting errors from the following:


Code:
magetaxi.lua
---------------------------------
function MageTaxi_Spam()
currentLoc = GetZoneText();
SendChatMessage(spam, "CHANNEL", nil, trade);
end;


messages.lua
---------------------------------
spam = "WTS portals to anywhere from ".. currentLoc.. ", 1g. 2g for me to come to another city";

Pops up with "Usage: SendChatMessage(text [,type] [,language] [,targetPlayer])"

If I remove the variable from the "spam", it displays the text just fine. I can't figure out how to pass "currentLoc" from magetaxi.lua to messages.lau.|||Not sure of this but the docs say the value is localized. http://www.wowwiki.com/API_GetZoneText


Code:
Returns the localized name of the zone the player is in.
zoneName = GetZoneText();
zoneName - String - zone name (localized).

So maybe put the string from currentLoc into another string that isn't local to just the MageTaxi_Spam() function. Again, this is just a guess.


Code:
magetaxi.lua
---------------------------------
local MyLoc
function MageTaxi_Spam()
currentLoc = GetZoneText();
MyLoc = currentLoc
SendChatMessage(spam, "CHANNEL", nil, trade);
end;

messages.lua
---------------------------------
spam = "WTS portals to anywhere from "..MyLoc..", 1g. 2g for me to come to another city";
|||Never mind, that was stupid. I wish I could delete my post. I was thinking of local in the Lua sense and what was meant was localized in terms of language. BTW, why can't we delete or edit our posts???|||I'm not sure if I've understood the question as the limited amount of code you have shown doesn't reveal in what sequence variables are being updated, or whether you are assigning a default value to 'currentLoc' when the AddOn loads.

However, it "seems" like you are expecting the 'spam' variable to be updated dynamically whenever you update the 'currentLoc' variable - which will not happen. 'spam' as you have defined it will have its value set once, and will then never change, even if you change the value of 'currentLoc'

Also, unless you are defining 'currentLoc' with a default value BEFORE 'spam' is defined, then 'currentLoc' will be nil, and the 'spam' variable will become nil. In fact, I'm surprised that you're not getting "Can't concatenate nil value" errors...

The simplest way to get this working seems to be :


Code:
magetaxi.lua
---------------------------------
function MageTaxi_Spam()
currentLoc = GetZoneText();
SendChatMessage(format(spam, currentLoc), "CHANNEL", nil, trade);
end;


messages.lua
---------------------------------
spam = "WTS portals to anywhere from %s, 1g. 2g for me to come to another city";



i.e. specify a parameter in the 'spam' variable, and use the string formatting function to insert the text you want...

You can include multiple parameters of different kinds, e.g.

complicatedString = "My name is %s, and I am %d years old, and I am %s";

name, age = "George Bush", 62;

outputString = format(complicatedString, name, age, " pretty damn stupid!");



(I think you can get away with passing more variables than are actually required, but you will get errors reported if you supply less parameters than are needed.)



I also second Jumpy's request for the ability to delete your own posts, and to take the time limit off editing them - this would be hugely useful for the large number of times where I have put my foot so far in to my mouth that I kick myself in the butt!|||The edit time thing is for a few reasons such as it stops people removing moderator edits and it stop people changing the first post of a thread entirely for hilarious effects. It also just generally limits the moderation required since a mod only has to check new posts and not watch everything that's ever been posted.|||Quote:








The edit time thing is for a few reasons such as it stops people removing moderator edits and it stop people changing the first post of a thread entirely for hilarious effects. It also just generally limits the moderation required since a mod only has to check new posts and not watch everything that's ever been posted.




That's all fair enough - but how about including a flat delete being allowed within the Edit time window ?

Although this is getting off topic....and 'twas only a jokey aside so nvm ;)

(I guess there are other complications with delete, like if someone had already quoted it, etc.)|||Gah, I'm just trying to keep it simple so people can edit the message settings by seperating them from the main .lua. Not looking like this is gonna be feasible at this rate.

Thx for the help so far guys. If any has any other suggestions, please let me know. Until then, I'm gonna revert back to a previous setup til I can figure out an easy way to do this.

Edit: Figured I'd post the rest of the coding to help out.


Code:

magetaxi.lua
function MageTaxi_OnLoad()
this:RegisterEvent("CHAT_MSG_CHANNEL_NOTICE");
this:RegisterEvent("PLAYER_ENTERING_WORLD");
this:RegisterEvent("VARIABLES_LOADED");
this:RegisterEvent("ADDON_LOADED");
SLASH_MAGETAXI1 = "/magetaxi";
SLASH_MAGETAXI2 = "/mtaxi";
SlashCmdList["MAGETAXI"] = function(msg)
MageTaxi_SlashCommandHandler(msg);
end;
MageTaxi_SetFaction();
end;

function MageTaxi_OnEvent()
if (event == "CHAT_MSG_CHANNEL_NOTICE") or (event == "PLAYER_ENTERING_WORLD") then
trade = GetChannelName("Trade - City");
elseif (event == "VARIABLES_LOADED") then
this:UnregisterEvent("VARIABLES_LOADED");
if (not showButton) then
showButton = true;
elseif (showButton == "false") then
MageTaxi_Button:Hide();
else
MageTaxi_Button:Show();
end;
end;
end;

-- SET CITIES
function MageTaxi_SetFaction()
faction = UnitFactionGroup("player");
if (faction == "Alliance") then
CITIES = {
[1] = "Stormwind",
[2] = "Ironforge",
[3] = "Darnassus",
[4] = "Exodar",
[5] = "Shattrath",
};
else
CITIES = {
[1] = "Undercity",
[2] = "Orgrimmar",
[3] = "Thunder Bluff",
[4] = "Silvermoon",
[5] = "Shattrath",
};
end;
dest = CITIES[1];
end;

-- SPAM
function MageTaxi_Spam()
currentLoc = GetZoneText();
if (debug) then
trade = GetChannelName("modtest");
end;
SendChatMessage(spam, "CHANNEL", nil, trade);
end

-- PORTING -
function MageTaxi_LFM()
SendChatMessage(more, "CHANNEL", nil, trade);
end;

-- LAST CALL
function MageTaxi_LastCall()
SendChatMessage(lastCall, "CHANNEL", nil, trade);
end;

messages.lua
-- MESSAGE FOR "ANNOUNCE" BUTTON
spam = "WTS portals to anywhere from ".. currentLoc.. ", 1g. 2g for me to come to another city"

-- MESSAGE FOR "LFM" BUTTON
more = "Port requested to ".. dest.. " from ".. currentLoc.. ", PST to be included for 1g."

-- MESSAGE FOR "LAST CALL" BUTTON
lastCall = "LAST CALL: Porting from ".. currentLoc.. " to ".. dest.. ", PST to be included for 1g.";
|||I'm not sure what was wrong with the solution I put forward.

If its designed to allow people to manually change the "messages.lua" file themselves then this is what it should look like :


Code:
-- MESSAGE FOR "ANNOUNCE" BUTTON
spam = "WTS portals to anywhere from %s, 1g. 2g for me to come to another city"

-- MESSAGE FOR "LFM" BUTTON
more = "Port requested to %s from %s, PST to be included for 1g."

-- MESSAGE FOR "LAST CALL" BUTTON
lastCall = "LAST CALL: Porting from %s to %s, PST to be included for 1g.";

And you use the format(string, parameter) function...

People just need to know that %s shouldn't be changed and represents the place names that will be filled in by the game.

They would already have to know not to change the ..currentLoc.. and ..dest.. parts of the strings the way that you have set it up.

This is exactly what you want isn't it - just substitue the concatenated variable with '%s'

So "..currentLoc.." becomes %s within the string.

i.e. its a parameterised anonymous variable, rather than an explicitly named and manually concatenated variable.

So the message output calls become :


Code:
-- SPAM
function MageTaxi_Spam()
currentLoc = GetZoneText();
if (debug) then
trade = GetChannelName("modtest");
end;
SendChatMessage(format(spam, currentLoc), "CHANNEL", nil, trade);
end

-- PORTING -
function MageTaxi_LFM()
-- Check valid values for currentLoc / dest ?
SendChatMessage(format(more, currentLoc, dest), "CHANNEL", nil, trade);
end;

-- LAST CALL
function MageTaxi_LastCall()
-- Check valid values for currentLoc / dest ?
SendChatMessage(format(lastCall, currentLoc, dest), "CHANNEL", nil, trade);
end;



The beauty of the format function comes in to its own when used for "constants", where you don't know at which point in a sentence a given word will actually need to be placed. (Especially language localised variables in WoW)

For example, lets say you have an AddOn that can output the phrase "I hate you" OR "I hate them"

You could set up one variable as follows :

iHate = "I hate ";

and then in your code you could do the following


Code:
--  if one person then
-- whoIHate = "you";
-- elseif more than one person
-- whoIHate = "them";

outputString = iHate .. whoIHate;



However, this runs in to problems when it needs to be localised because the sequence of words has been hard coded to put the object of your hate at the end of the sentance.

But in French the phrases translate to "Je vous d?teste" and "Je les d?teste"

(Literally "I you hate", and "I them hate")

This means the code can never work with a French localised version.

BUT if you use the following definition of iHate :

iHate = "I hate %s";

and change the definition of outputString to :

outputString = format(iHate, whoIHate);

then you get exactly the same output on the English client, but now you CAN define a French localised version of the iHate variable :

iHate = "Je %s d?teste";

and this will work.........

[Yes I know this is a useless example as you haven't localised the words "you"/"them", but its still an example ;P]|||Ah, see I was all confused about %s being called twice in one line but somehow getting 2 results. I get it now that's it's been explained a bit. ;D

I'm still all noobish to .lua syntax (contrary to what my friends seem to think....). I'll set up that tonight and give it a run to make sure it works (which I'm sure it will coming from you Telic, you has smarts!). Thanks mate!

No comments:

Post a Comment