User Tools

Site Tools



lua_tutorial

Learning Lua

by Kali#3326

This tutorial is designed with beginners in mind, but has topics suitable for advanced users as well. Even if you have no programming experience you should be able to easily follow along and grasp the concepts along the way. As someone who is self taught I understand the struggles of overcoming all the technical jargon traditional guides throw around, and while I understand the importance of that jargon, I think it is best to start simple with obtainable goals in mind.

Getting Started

You don't need any fancy software or an IDE to make scripts for Minion, in fact you already have everything you need, MMOMinion and Notepad. However, you can do a whole lot better than Notepad, so here are some of my recommendations:

There are other options out there, most people tend to go with VSCode or Notepad++. I personally prefer IntelliJ but the most important thing is that you know how to use it comfortably because you don't want to have to end up learning how to write Lua on top of how to install and use the software. This guide will assume you use VSCode and any references I make will refer to that software.

Next are online resources, use these to look up standard Lua functions. Minion uses a modified version of Lua 5.1 with some standard functions removed/modified for security purposes. Keep that in mind when using these and other resources.

Now that you have everything you need, lets get started with the traditional Hello, World!, and we don't even need any of the above to do it. We're going to need Minion's console for this, and if you've never used it before or don't even know what I'm talking about, you can access it by clicking on the MMOMinion button and selecting Console.

The console is one of your most important tools when developing, debugging, or even reporting an issue to other developers. At first glance it might seem like a bunch of nonsense, but everything in the console has been determined by that developer that it is important information and to output it into the console. The reason for this is if that information is not correct or not intended that we have a record of what Minion's Lua Interpreter executed and in what order if we have enough information.

Now with Minion's Lua instead of using the function print to “print to the console” like you normally would use, we simply use the function d, and to stick with tradition we can output to the console with our Hello World message:

d("Hello, World!")

Dev Tools and Lua Source Files

Next we're going to introduce Dev Tools and our Lua source files that come by default with MMOMinion. To access Dev Tools open the MMOMinion menu and hover your mouse over FFXIVMinion to expand the menu right and click Dev Tools. Dev Tools, aka Dev-Monitor, is our single greatest resource in Minion as it contains most of our API in one single script.

To show this off target something like a minion or any NPC, then in Dev-Tools expand the Target tree and expand Core Data and Position Data. Lets go over what this information means.

  • Ignore Ptr, this information is only used by developers reversing the game to update minion for new patches.
  • ID, not to be confused with ContentID, is an identifier generated by the servers so the client knows what entity is what. Imagine if you had 5 Ifrit's on the same battlefield, all with the same name, same ContentID, how does the game tell them apart otherwise? ID's are not static/reliable information, a new ID is given every time you zone or load an instance.
  • Name is entity name, character name, etc.
  • Content ID is an identifier given to each model. Where 5 Ifrit's would all have the same ContentID, imagine how many types of Mandragora there are in the game, each one has a different Content ID. Every time they reuse a creature with the same enemy model but recolor it it will have a new ContentID with it.
  • Type is entity type, in this case a minion aka minipet.
  • Status is server status, it is mainly used only for the Player and in niche cases as well. It's good to know this information is here but you may never use it.
  • ChocoboState is the Player's current state on a chocobo.
  • CharType is Character Type, in this case an NPC.
  • TargetID is the ID of the target that entity is currently targeting, in this case a minion doesn't target anything so it's 0.
  • OwnerID is used for entities like your companion chocobo, egi's and pets, sadly not minions.
  • Claimed By ID is pretty useful when you want to know who claimed an enemy, even if that player isn't the enemy's current target.
  • Fate ID only applies to FATE enemies, and will let you know what fate ID that entity is apart of.
  • Icon ID is the icon above NPCs, like quest icons.
  • Position is the position that entity is in the world, different from MeshPosition.
  • Radius is the size of the ring when you target them. For example all Players have a radius of 0.5 yalms, while large bosses will have huge rings to accommodate for melee jobs doing mechanics while still being in attack range.
  • Distance is the distance between you, and in this case, your target. This is exact distance between two points in a 3D space.
  • Distance2D is the distance between two points in a 2D space, removing height from the equation and removing the Player's radius and, in this case, the target's radius.
  • PathDistance is a calculated distance used by the Navigation Manager, aka the distance it would take to travel from your position to, in this case, your target's position.
  • LoS and LoS2, aka Line of Sight, are used to calculate if the los between two positions is clear or blocked by something. Sometimes can be unreliable/finicky.
  • OnMesh is determined by the Navigation Manager if the entity is on the navigation mesh or not.
  • IsReachable is determined by the Navigation Manager if the entity can be reached by the Player or not.
  • MeshPosition is the position on the mesh, not the same as game position.

*Deep Breath*, now that was a lot of information wasn't it? And to imagine that's not even 1% of all the information Minion provides about the game. That might sound intimidating, and it can be, but it's always important to remember that you don't need to know what everything is. You can find your way around by context and some documentation about what something is here and there. I encourage you to look through everything Dev Tools has to offer as you learn and experiment with Lua. There is so much you can do, you just need to know what is available and how to apply that knowledge.

In order to apply this information, we're going to need to access the source file for Dev Tools, which is located in Minion's Lua Mods folder. The MINIONAPP folder is located where you installed Minion, by default the C:\ drive.

..\MINIONAPP\Bots\FFXIVMinion64\LuaMods\Dev

The LuaMods folder contains every Lua script that runs with Minion including addons from the store. When you begin making your own modules, which is very soon, you'll maintain all your projects within LuaMods. Now open the Dev folder and open dev.lua in the text editor you chose to use. From this point forward I'm going to assume you are using VSCode, so I'll be referencing how to do things from there this point forward.

Now there is a lot of information that we aren't ready to talk about in this file, so press CTRL+F to search for Core Data, there will only be one entry in the file so it will take you right to it.

This is the same thing you interacted with before in Dev Tools, just the source of it. We're just going to borrow some information from this to help demonstrate how you can use this information in the future, but also it helps you get use to using these files to learn from them. In this image on line 2459 for Name, at the very end of the line you see c.name. What is c you may ask? Well, to not overcomplicate it too much the script is using a template and passing a table into the function so it doesn't have to write everything multiple times and will adapt by displaying information only needed for that target type. But to explain what c is, we're going to need to manually grab the table used in our example before, which was our player's target. To do that search for “Target” with quotes, and you should see this:

local c = Player:GetTarget()

Lets break this line down, local c is making c a local variable, which we'll talk about later, c = Player:GetTarget() is setting c, Player is a global table for your character, : is calling a function from the Player table, GetTarget is the function within the Player table, and () is executing that function.

We'll talk about all that and more later, but we just need to use Player:GetTarget() for our example, so go back to the game and open your console and lets combine the two pieces of information we pulled and apply them together.

In this example we have 5 Striking Dummies, you can target any entity for this but for this example I'll be targeting these. Target something and in your console, execute this:

d(Player:GetTarget().name)

Now as you rotate between different targets execute it again to have it print the target's name. You can also mix things up and print your target's .id, or .type, or .chartype, .distance, etc.

local target = Player:GetTarget() d("["..target.id.."] "..target.name.." is "..string.format("%.2f",target.distance).." yalms away")

Now what did I do there? I simply utilized the information we learned with 3 variables and concatenated them together in the same string. In one simple line it demonstrates that 5 entities all have the same name, but all have different id's and distances from the Player. local target = Player:GetTarget() is defining target as the Player's current target, “[”..target.id..“] ” is concatenating brackets on each end of the target id, ..target.name..“ is ”..string.format(“%.2f”,target.distance)..“ yalms away”) is formatting the number from distance and truncating it to two decimals. It's a bit more advanced, but it should be easy enough to understand I think, but I think it's time we take a few steps back and introduce basic Lua terms so you can be on the same page.

Introducing Basic Lua Terms

  1. explain what a variable is
  2. comments
  3. identifiers, and how they are case sensitive
  4. the 21 reserved keywords
  5. whitespace and readability
  6. variable definition, lists, and declaration
  7. the 8 “types”
    1. nil, boolean, number, string, function, userdata, thread, and table
  8. the type function
  9. arithmetic operators
  10. relational operators
  11. logical operators
  12. miscellaneous operators
  13. operator precedence
  14. loops, break, and infinite loops
  15. if statements
  16. functions
    1. defining
    2. arguments
    3. calling
    4. assigning and passing
    5. variable arguments
  17. strings and string manipulation
  18. replacing a substring, find and match
  19. string formatting
  20. character and byte representation
  21. arrays and multi-dimensional arrays
  22. iterators
  23. tables and table manipulation

Making our First Module

Common Functions and Standard Libraries

Error Handling and Debugging

Garbage Collection and Performance Impact

Object Oriented and Inheritance

lua_tutorial.txt · Last modified: 2023/04/29 07:52 by kali