+ Reply to Thread
Results 1 to 10 of 10

Thread: Am I reaching for straws?

  1. #1
    Telaran
    Join Date
    Jun 2011
    Posts
    65

    Default Am I reaching for straws?

    Me being so 'new' to this programming in LUA thing... I'm looking for simplifying a wide spread function.


    Code:
    for oyslop = 2, 8 do
    	oys[oyslop] = UI.CreateFrame("Text","oysFrame"..tostring(oyslop),oys)
    	oys[oyslop]:SetPoint("TOPCENTER",oys[oyslop-1],"BOTTOMCENTER")
    	oys[oyslop]:SetWidth(150)
    	oys[oyslop]:SetHeight(16)
    	oys[oyslop]:SetBackgroundColor(.20,.20,.20,.50)
    	oys[oyslop]:SetFontSize(12)
    	oys[oyslop]:SetText("Frame Number: " ..tostring(oyslop))
    
    	function oys[oyslop].Event:MouseIn()
    		oys[oyslop]:SetFontSize(22)
    		oys[oyslop]:SetHeight(26)
    	end
    	function oys[oyslop].Event:MouseOut() 
    		oys[oyslop]:SetFontSize(12)
    		oys[oyslop]:SetHeight(16)
    	end
    	
    end
    for example.. trying to give 7 frames the same function code. Am I going about this the wrong way or is it simply something I need to repeat the code manually for each frame. I know I could repeat the same code over and over again, but I don't want my addon to take up a lot of code space if I can help it.
    Thuggernaught / Isha / Adracamas

    Troll?
    /grin
    Kill it with Fire

  2. #2
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    13,502

    Thumbs down

    A few things you can do. One... Functions are just things in Lua.

    Code:
    function myMouseOut() 
    	oys[oyslop]:SetFontSize(12)
    	oys[oyslop]:SetHeight(16)
    end
    
    ...
    
    	oys[oyslop].Event:MouseOut = myMouseOut
    Secondly, you could do something like:

    Code:
    function MakeMyFrame(val)
    	x = UI.CreateFrame("Text","oysFrame"..tostring(val),oys)
    	x:SetPoint("TOPCENTER",oys[val-1],"BOTTOMCENTER")
    	x:SetWidth(150)
    	...
    end
    
    for oyslop = 2, 8 do
    	oys[oyslop] = MakeMyFrame(val)
    end
    This help any?
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Got ideas for improving the RIFT community? Feel free to PM me. Just came back? Welcome back! Here's what's changed. (NOTE NEW URL)

  3. #3
    Telaran
    Join Date
    Jun 2011
    Posts
    65

    Default

    Getting 'function arguments expected near '=' '

    :/ ah well.. I'll just hit it old school till I learn better lol
    Last edited by Thuggernaught; 10-10-2011 at 03:06 PM. Reason: Looked like it would work, but in practice did not. :(
    Thuggernaught / Isha / Adracamas

    Troll?
    /grin
    Kill it with Fire

  4. #4
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    13,502

    Default

    This is why you should never trust the details of anything I say about programming languages, I have an astounding ability to misremember syntax.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Got ideas for improving the RIFT community? Feel free to PM me. Just came back? Welcome back! Here's what's changed. (NOTE NEW URL)

  5. #5
    Telaran
    Join Date
    Jun 2011
    Posts
    65

    Default

    It's ok. a pebble in the pond causes ripples that will eventually contact their target, even if you don't hit your target directly.
    Thuggernaught / Isha / Adracamas

    Troll?
    /grin
    Kill it with Fire

  6. #6
    Telaran
    Join Date
    Jun 2011
    Posts
    65

    Default

    Hm..
    Code:
    oys[oyslop].Event = function(MouseIn ) oys[oyslop]:SetFontSize(22); oys[oyslop]:SetHeight(26); end
    Doesn't throw an error... but also doesn't function lol.

    Adding :MouseIn causes an error... either a missing argument without () or unexpected symbol near = with....

    -.- So many symbols to keep track of.
    Thuggernaught / Isha / Adracamas

    Troll?
    /grin
    Kill it with Fire

  7. #7
    Telaran
    Join Date
    Jun 2011
    Posts
    65

    Default

    Egads I got it! I knew it was SO simple too.

    Code:
    oys[oyslop].Event:MouseIn = function( ) oys[oyslop]:SetFontSize(22); oys[oyslop]:SetHeight(26); end
    was wrong, by ONE character!

    Code:
    oys[oyslop].Event.MouseIn = function( ) oys[oyslop]:SetFontSize(22); oys[oyslop]:SetHeight(26); end
    Was correct.. using them in that way makes the : become replaced with a . =3


    Thanks the_real_seebs for the pebble.
    Last edited by Thuggernaught; 10-10-2011 at 03:41 PM.
    Thuggernaught / Isha / Adracamas

    Troll?
    /grin
    Kill it with Fire

  8. #8
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    13,502

    Default

    I don't actually understand the intended structure.

    ... wait. I just realized a thing, which is that I wasn't looking closely. The functions are being defined in terms of oys[oyslop]. But. That's not actually a thing they know about or have access to in my version -- in yours, I think they're what computer science geeks call "closures" where they are able to access the values that were in the surrounding code when they were defined.

    As I recall, Lua has two ways of doing function definitions.

    A named function looks like:

    function function_name_here(args)
    ...
    end

    and an anonymous function looks like

    function (args) .. end

    and has no name. But the entire "function ... end" thing is an expression that yields a "value" which is the function argument, which can be called.

    So if foo.bar is supposed to be a function, you can do:

    foo.bar = function(args) ... end

    or you can do

    function named_function(args)
    ...
    end

    foo.bar = named_function

    and both should work.

    Note that the (args) matter, and the exact value that goes there depends on what the function is supposed to be getting called with.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Got ideas for improving the RIFT community? Feel free to PM me. Just came back? Welcome back! Here's what's changed. (NOTE NEW URL)

  9. #9
    Shadowlander
    Join Date
    Jun 2011
    Posts
    44

    Default

    Conceptually this code should work, but I think there's one small change that will make it actually work. ;)

    Code:
    for oyslop = 2, 8 do
    	oys[oyslop] = UI.CreateFrame("Text","oysFrame"..tostring(oyslop),oys)
    	oys[oyslop]:SetPoint("TOPCENTER",oys[oyslop-1],"BOTTOMCENTER")
    	oys[oyslop]:SetWidth(150)
    	oys[oyslop]:SetHeight(16)
    	oys[oyslop]:SetBackgroundColor(.20,.20,.20,.50)
    	oys[oyslop]:SetFontSize(12)
    	oys[oyslop]:SetText("Frame Number: " ..tostring(oyslop))
    
    	function oys[oyslop].Event:MouseIn()
    		self:SetFontSize(22)
    		self:SetHeight(26)
    	end
    	function oys[oyslop].Event:MouseOut() 
    		self:SetFontSize(12)
    		self:SetHeight(16)
    	end
    	
    end
    Alternatively, as seebs suggested, you can do something like:

    Code:
    function oysMouseIn()
    	self:SetFontSize(22)
    	self:SetHeight(26)
    end
    
    function oysMouseOut() 
    	self:SetFontSize(12)
    	self:SetHeight(16)
    end
    
    for oyslop = 2, 8 do
    	oys[oyslop] = UI.CreateFrame("Text","oysFrame"..tostring(oyslop),oys)
    	oys[oyslop]:SetPoint("TOPCENTER",oys[oyslop-1],"BOTTOMCENTER")
    	oys[oyslop]:SetWidth(150)
    	oys[oyslop]:SetHeight(16)
    	oys[oyslop]:SetBackgroundColor(.20,.20,.20,.50)
    	oys[oyslop]:SetFontSize(12)
    	oys[oyslop]:SetText("Frame Number: " ..tostring(oyslop))
    
    	oys[oyslop].Event.MouseIn = oysMouseIn
    	oys[oyslop].Event.MouseOut = oysMouseOut
    
    end
    One thing to keep in mind is that the ":" is just syntactic sugar in LUA. There's no difference between

    Code:
    function frame.Event:MouseIn()
    ...
    end
    and

    Code:
    frame.Event.MouseIn = function()
    ...
    end

  10. #10
    Shadowlander
    Join Date
    Feb 2011
    Posts
    31

    Default

    Quote Originally Posted by Jorast View Post
    One thing to keep in mind is that the ":" is just syntactic sugar in LUA. There's no difference between

    Code:
    function frame.Event:MouseIn()
    ...
    end
    and

    Code:
    frame.Event.MouseIn = function()
    ...
    end
    There is difference actually. "self" doesn't just magically appear, it's a parameter. so

    Code:
    function frame.Event:MouseIn()
        ...
    end
    is the same as

    Code:
    frame.Event.MouseIn = function(self)
        ...
    end
    This is important to know when function is supposed to have parameters.



    One more thing: I can be wrong, but I believe lua won't accept this syntatic sugar when used on tables indexed with braces. Example:
    Code:
    local test = { };
    test.inner = { };
    
    function test.inner:works() end
    function test["inner"]:doesntwork() end
    Last edited by Pushad; 10-10-2011 at 05:10 PM.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts