Thread Tools Display Modes
09-10-08, 02:52 PM   #1
AnrDaemon
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 156
Scope resolution... I give up...

Modding Cartograph_Notes to add some nice functionality to /note chat command. Just can't understand the general cause of variable scopes...
Subjected code is
Code:
			if text:match("^%d%d?%.%d%d? [^%d].*$") or text:match("^%d%d?%.%d%d?$") then
				text = text:gsub("^(%d%d?)%.(%d%d?)", "%1 %2", 1)
			end
			local x, y, title = (" "):split(text, 3)
			x, y = tonumber(x), tonumber(y)
			if not x or not y then
				self:Print(L["Usage: /note <0-100> <0-100> [title]"])
				return
			end
			x, y = x / 100, y / 100
around line 1338 in Cartographer_Notes/Notes.lua
What i'm doing, is basically that i'm retrieving actual player coords if no right coords were provided to command.
This way:
Code:
			if text:match("^%d%d?%.%d%d? [^%d].*$") or text:match("^%d%d?%.%d%d?$") then
				text = text:gsub("^(%d%d?)%.(%d%d?)", "%1 %2", 1)
				local x, y, title = (" "):split(text, 3)
			else
				local title = text;
				local x, y = GetPlayerMapPosition("player")
				x, y = x * 100, y * 100
				-- Some nice debudding printout
				self:Print("Coords: " .. x .. ";" .. y)
			end
			x, y = tonumber(x), tonumber(y)

			if (0 == x) or (0 == y) then
				self:Print(L["Usage: /note <0-100> <0-100> [title]"])
				return
			end
			x, y = x / 100, y / 100
Now tell me what i'm doing wrong why it every time telling me that i'm doing wrong things to global "y" in the last string?
The "x, y = x / 100, y / 100" one.
  Reply With Quote
09-10-08, 03:45 PM   #2
Xus
A Fallenroot Satyr
 
Xus's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 25
In your new code you're declaring locals x, y, title inside the if-else blocks, and THAT is their scope. You can fix it by declaring them above your if-else block and then only assigning a value to them inside.
  Reply With Quote
09-10-08, 04:08 PM   #3
Mikord
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 61
You've defined x, y, and title to be local to the if block and else block. They cease to exist once you exit the block.

Code:
local x, y, title
if text:match(....
 x, y, title = (" "):split(text, 3)
 x, y = tonumber(x), tonumber(y)
else
 title = text
 x, y = GetPlayerMapPosition("player")
end

-- Do whatever with x, y, and title.
  Reply With Quote
09-10-08, 05:23 PM   #4
AnrDaemon
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 156
Looks pretty silly comparing to the PHP...
And it were working ok' as it was, in first quoted block. Probably i didn't understand that code...

Thanks for your hints, now it works close to what I want.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Scope resolution... I give up...


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