ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   A bunch of beginner LUA questions (https://www.esoui.com/forums/showthread.php?t=210)

Shinni 03/09/14 07:14 PM

A bunch of beginner LUA questions
 
I wrote a little script to help me add map pins.
However, this is my first time working with LUA and while understanding other scripts works fine, writing "blind" is kinda hard.
I can't test my script without a beta, so there are probably a bunch of errors in it.
There are some sections where I am really unsure whether they work (see bottom of this post).
Feel free to read the whole script, though. There are probably other errors i couldn't find.
(You may also correct my English, if I made some mistakes. I don't mind improving in another language besides LUA :D)

complete script:
Warning: Spoiler


1)
Does this delete the entry with the key pinTag, or will the key be associated with the value nil?
(The former implies the later but will pairs(self.pins[pinType]) yield (pinTag, nil) or is it completely removed from the table?)
Lua Code:
  1. self.pins[pinType][pinTag] = nil

2)
radius = radius gives me headaches...I'm really unsure about this. (Line 42)
Lua Code:
  1. pins[pinTag] = { area = subzone, x = locx, y = locy, radius = radius }

3)
At first, pinTooltipCreator is a string, but then its assigned to a table.
What happens with AddLine(pinTooltipCreator)? What's the application order of LUA?
Will it add the string because when the function was defined, pinTooltipCreator was still a string?
...or will it crash because when the function is executed, pinTooltipCreator is a table?
Lua Code:
  1. if type(pinTooltipCreator) == "string" then
  2.     pinTooltipCreator = { creator = function(pin) InformationTooltip:AddLine(pinTooltipCreator) end, tooltip = InformationTooltip }
  3. end

4)
The function is defined and called inside methods of two different objects. Will self represent the defining or the executing object?
Lua Code:
  1. function( g_mapPinManager )
  2.     for pinTag, pin in pairs(self.pins[pinType]) do
  3.         --only draw pin, if player is on the correct map
  4.         if pin.zone = GetMapName() then
  5.             if pin.radius then
  6.                 g_mapPinManager:CreatePin( pinType, pinTag, pin.x, pin.y, pin.radius )
  7.             else
  8.                 g_mapPinManager:CreatePin( pinType, pinTag, pin.x, pin.y )
  9.             end
  10.         end
  11.     end
  12. end

Pawkette 03/09/14 08:07 PM

You wanna call

Lua Code:
  1. self:CreatePinType

Key being the colon instead of a period, also Create instead of create. Colon signifies self being passed invisibly to the function.

foo:Bar() == foo.Bar( self )

Will keep reading

Shinni 03/10/14 11:39 AM

Thanks, I fixed the mentioned errors.
Using : instead of . is kinda new to me. I've rarely seen a : in other languages.

Wukar 04/03/14 07:35 AM

Lua indeed has some nice benefits.

http://www.lua.org/pil/5.html
Quote:

Lua also offers a special syntax for object-oriented calls, the colon operator. An expression like o:foo(x) is just another way to write o.foo(o, x), that is, to call o.foo adding o as a first extra argument. In Chapter 16 we will discuss such calls (and object-oriented programming) in more detail.


All times are GMT -6. The time now is 11:03 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI