Thread Tools Display Modes
12-15-13, 04:50 PM   #1
CrazyCactuaR
A Deviate Faerie Dragon
Join Date: Oct 2011
Posts: 13
Unable to see the problem with my function

Code:
function IAmStill()
	currentSpeed, runSpeed, flightSpeed, swimSpeed = GetUnitSpeed("Player")
	if currentSpeed == 0 then
		return true
	else
		return false
	end
end
iam = {
	still = IAmStill(),
}
Hi all, Hopefully someone can guide my blindness and show me the error of my ways. For some unknown reason i am unable to see why this is incorrectly displaying "true" even when its called when i am moving.

This is called by doing /script print(iam.still())

If i were to do print(IAmMoving()) this would show the correct result. (false when moving, true when still)

Thank you in advance to any whole can solve this annoying problem i have ran into.

Last edited by CrazyCactuaR : 12-15-13 at 04:53 PM.
  Reply With Quote
12-15-13, 05:22 PM   #2
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Lua Code:
  1. local function IAmStill()
  2.     local currentSpeed = GetUnitSpeed("player")
  3.  
  4.     if (currentSpeed == 0) then
  5.         return true
  6.     else
  7.         return false
  8.     end
  9. end
  10.  
  11. IAm = {
  12.     still = IAmStill
  13. }

1) "Player" isn't a valid UnitID, "player" is.
2) Make IAmStill local.
3) IAm.still needs to be a function reference, not the return of a function (denoted by ()).
4) I don't condone making IAm global, nor do I condone the way you are calling the function, but I am hoping that it is a crude test and that you intend to localize the table and call it in a better way.

/run print(IAm.still()) should work.

Last edited by Clamsoda : 12-15-13 at 05:25 PM.
  Reply With Quote
12-15-13, 05:24 PM   #3
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Code:
function IAmStill()
	return GetUnitSpeed("player") == 0
end
iam = {
	still = IAmStill,
}
  Reply With Quote
12-15-13, 05:57 PM   #4
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Just for fun:

Code:
local IAm = {}

function IAm:Still()
  return GetUnitSpeed("player") == 0
end

If IAm:Still() then ... end
If you want to keep your functions in a table, then this might be the proper way to go!
__________________
Profile: Curse | Wowhead
  Reply With Quote
12-16-13, 01:29 AM   #5
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
I always thought that the double-point notation could not be used on a table object. Nice.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 12-16-13 at 01:32 AM.
  Reply With Quote
12-16-13, 03:02 AM   #6
CrazyCactuaR
A Deviate Faerie Dragon
Join Date: Oct 2011
Posts: 13
Thank you all for your variations on the solution.

Your right it was a way of toying with a new method. I had hopes i could make life easier upon myself by having a list of table values such as my.Level, my.Race, ami.Moving, ami.Dead and so on.

What really confuses me was the use of (), if i were to put;

/script print(iam.still())

and the table calls it by still = IAmStill, then i see i am returned the correct value when i am moving/idle.

But having print(iam.still) and still = IAmStill(), i simply get a true return regardless of what im doing.

Confusing for myself but at least you helped me find a working solution. Again, thank you all for your help you provided.
  Reply With Quote
12-16-13, 03:24 AM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by zork View Post
I always thought that the double-point notation could not be used on a table object. Nice.
o_O

tbl:func() is nothing but syntatctic sugar for tbl.func(tbl) and you can use either calling style regardless of whether you defined your function using tbl.func(self) or tbl:func()
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
12-16-13, 08:33 AM   #8
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Just to try and explain the difference between . and : when calling a table function:

Code:
local t = {
  a = 1,
}

function t:test(X)
  -- "self" is referring to the "t" object (automatically assigned)
  self.a = self.a + X -- increment t.a by X
end

function t.test(self, X)
  -- like before, "self" is referring to the "t" object
  -- notice how the argument "self" must be declared
  -- you could even change it to something entirely different,
  -- keep in mind that "self" would be nil if we didn't declare it like this
end

function t.test()
  -- "self" is nil
end
So the main issue would be, if you want to pass something down to a widget, for example:
Code:
local t = {}
function t.test(self, elapsed)
  -- "self" is actually "frame" and not "t" because of the way it is being called by the frame itself
end
frame:SetScript("OnUpdate", t.test)

-- note that you could manually call "t:test(5)" that way "self" would refer to "t" and elapsed would have the value 5
This can be confusing at first, but play a bit with it and you will figure out how it works. Note that you can't pass a function reference using "t:test" because : would indicate calling the function where "t" would be assigned to the "self" variable, while . is simply referencing it.
__________________
Profile: Curse | Wowhead
  Reply With Quote
12-16-13, 08:59 AM   #9
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by CrazyCactuaR View Post
What really confuses me was the use of (), if i were to put;

/script print(iam.still())

and the table calls it by still = IAmStill, then i see i am returned the correct value when i am moving/idle.

But having print(iam.still) and still = IAmStill(), i simply get a true return regardless of what im doing.
Code:
iam.still = IAmStill
Saves a reference to the function IAmStill into iam.still
Code:
print(iam.still())
Evaluates if you are moving or not based on current conditions since it is the same as calling IAmStill.
Code:
iam.still = IAmStill()
Saves whether you were moving or not when IAmStill was called into iam.still.
Code:
print(iam.still)
Retrieves the previously saved state from IAmStill, does not re-evaluate the current state.
  Reply With Quote
12-17-13, 07:00 AM   #10
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
If you have a function named "still" then you do something like print(still) then you see "function 0x0492..." i.e. the reference of that function. But if you do print(still()) then you call that function and it returns what ever it returns, then that value is being printed.
__________________
Profile: Curse | Wowhead
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Unable to see the problem with my function


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