User Tag List

Page 2 of 2 FirstFirst 1 2
Results 8 to 12 of 12

Thread: Lua tips and tricks

  1. #8
    Original Poster
    Moderator / Eluna Dev <span style='color: #FF0000'><span class='glow_8B0000'>Rochet2</span></span>'s Avatar
    Join Date
    Apr 2013
    Location
    Finland
    Posts
    1,871
    XP
    13,718
    Level
    35
    Thanks
    95
    Thanked 595 Times in 421 Posts
    Here is a new tip for the thread.
    I originally saw @Laurea doing this in his script here : http://pastebin.com/siQHVciK
    I suggest you check it out as well.
    Update: Eluna is now supported, and some general optimizations have been performed. The value backup feature for Eluna isn't implemented yet, but working on it.
    This script allows you to easily attach variables to players, creatures, gameobjects, maps and instances (Eluna also supports items, groups and guilds) without using a table and the tostring method.
    The purpose is to make collision-free scripting easier, especially for beginners, and keep more advanced code clean.
    Anyways. The idea is that Player and Unit and so on are tables that hold the methods for lua.
    This means that you can add in your own methods :)
    And variables and so on .. just like a normal table pretty much.
    When making a method, you use : like in this example code. This enables you to use the function as a method to the player and use "self" variable for the player object using it:
    Code:
    local function OnChat(event, player, msg, lang, typ, misc)
        if (msg == "test") then
            player:Custom()
        end
    end
    
    RegisterPlayerEvent(18, OnChat)
    
    function Player:Custom()
        self:SendAreaTriggerMessage("Hi :)")
    end




    @SkittlesAreFalling
    -- Eluna Function:: Returns nil when the player is changing maps.
    While this is true, you can still use an existing saved player pointer.
    Example:
    Code:
            guid = player:GetGUID()
            CreateLuaEvent(function(a,b,c) player:SendBroadcastMessage("TestPointer") print(player:GetName(), GetPlayerByGUID(guid)) end, 500, 0)
    In this code you commented that GetPlayerByGUID (or by name) returns nil on map change. While it does that, player:GetName() will still return valid name.
    Also the BroadcastMessage will succeed after changing maps (obviously the player cant see the message when loading a new map)
    A code like this should be enough to control players:
    http://pastebin.com/3C7EFm3M
    It of course doesnt do what your script does, but this is what I had in mind when you said about saving the player to var and setting to nil on logout.

    ps. Pretty much the only difference between getting a player with GetPlayerByGUID vs using a system like that are that player isnt available when changing maps.
    This is why if you need to do this, I suggest using GetPlayerByGUID instead. You may need a system if you have special needs like player stats while he is off, like skittles might have had.
    The above system only describes a simple example of storing data for player (or the player). It can be extended to save various information about player.
    Last edited by Rochet2; 01-26-2014 at 05:50 PM.
    Awards:
    Support LOL Scripter Facepalm! Raffle Winner

  2. The Following User Says Thank You to Rochet2 For This Useful Post:


  3. #9
    Original Poster
    Moderator / Eluna Dev <span style='color: #FF0000'><span class='glow_8B0000'>Rochet2</span></span>'s Avatar
    Join Date
    Apr 2013
    Location
    Finland
    Posts
    1,871
    XP
    13,718
    Level
    35
    Thanks
    95
    Thanked 595 Times in 421 Posts
    Lua is fast and C++ is fast.
    The swapping from lua to C++ and C++ to lua is slow.

    When using a variable from C++ twice, for example if you would need the health of an NPC twice,
    save it to a local variable. This will be a lot faster.
    We wrote an extension that automatically does this already for methods that should return static data, like GetGUID().

    If you want to be more efficient, I suggest googling for lua efficiency, even though that will mostly be just fine tuning your lua scripts.
    Optimizing Getting values from C++ is a lot more important.


    As mentioned in another topic, lua automatically casts units etc.
    So if a method would return a world object for example, lets say C++ has a function GetUnit()
    When the return value is Unit, it will in lua be player or creature, since it is casted automatically to the right value.

  4. #10
    Enthusiast
    Join Date
    Jun 2014
    Posts
    30
    XP
    1,954
    Level
    12
    Thanks
    1
    Thanked 9 Times in 6 Posts
    Quote Originally Posted by Rochet2 View Post
    Lua is fast and C++ is fast.
    The swapping from lua to C++ and C++ to lua is slow.
    Comparatively, right?

  5. #11
    Original Poster
    Moderator / Eluna Dev <span style='color: #FF0000'><span class='glow_8B0000'>Rochet2</span></span>'s Avatar
    Join Date
    Apr 2013
    Location
    Finland
    Posts
    1,871
    XP
    13,718
    Level
    35
    Thanks
    95
    Thanked 595 Times in 421 Posts
    Quote Originally Posted by neglected View Post
    Comparatively, right?
    Not entirely sure what that means exactly.
    And no, any bolding you might have added cant be seen in the quote.

  6. #12
    Enthusiast
    Join Date
    Jun 2014
    Posts
    30
    XP
    1,954
    Level
    12
    Thanks
    1
    Thanked 9 Times in 6 Posts
    Quote Originally Posted by Rochet2 View Post
    Not entirely sure what that means exactly.
    And no, any bolding you might have added cant be seen in the quote.
    I was just wondering how much faster/slower it is :P

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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