FFXIAH.com

Language: JP EN DE FR
New Items
2023-11-19
users online
Gearswap_Academia_3
Class #3

Master thread



Lesson 6 - Making rules in context

Alright, alright, alright, you got this. We're now going to make a rule so that our midcast set (which at the moment we only have a cure set) only actually swap when were using a cure spell.

Most of the rules we'll do in gearswap are going to be based on either these two things:

* If - then - else rules
* variables defined rules

We'll get to cover both styles eventually, but for now we'll cover the "If - then - else" style. This is a very broadly use style of programming you'll see this a lot if you look at code. the basis of it is that IF something happens, THEN do something about it, and if it doesn't happen, then do something ELSE about it. Got it? Alright!

Remember this link we bookmarked and never got to in the first lesson? Click it in a new tab we'll need this guy up from now on. In that page check the side bar on the right and go to spell. This is an important part, you don't need to read it all right now, but I do suggest it.

Let's look at our midcast function:
Code
function midcast(spell)
	equip(sets.midcast.cure) 
end


Notice that it receives spell in the parenthesis. That means that when gearswaps runs this, it gives that function the info on the spell you cast. In gearswap terms a spell is a spell, but also a JA, or a WS or pet ability. So on that page it says all the info that "spell" contains. For example, if I am casting Cure, then spell.name will be the same thing as "Cure" and spell.type will be equal to "WhiteMagic". Note that the types of those variable are set as "string". A string means that the type of data in that variable is just regular text. For example, spell.mp_cost is a number type. That mean we can do things like spell.mp_cost * 2 or something mathematical and it makes sense. But we would not be able to do spell.name *2 because multiplying a name makes no sense.

So now that we know all sorts of info we can get from spell (see the gearswap page link we openned) we'll be about to make a rule to do something about it. So let's go back into our midcast function. And we'll add an if - then - else type rule to check if our spell is white magic. We write this this way:
Code
	if spell.type == "WhiteMagic" then 
		-- Code here what you want to happen if the above is true
	end


Every time you do a if, you need to compare, and end the line with then. Under that you put the code you want to execute and under that you put a line that only has end on it. The "end" means that all that is between "if" and "end" only happens if the condition after the if, is true.

Also you can put a else line between the if and end lines to add a space for code to execute ONLY if the if condition is false like this:
Code
	if spell.type == "WhiteMagic" then 
		-- do something for when I cast white magic spell
	else
		-- do something for when I cast non whitemagic spell
	end



So now, if I write that rule into my midcast so that my cure set only comes when I cast a white magic spell it would look like this:
Code
function midcast(spell)
	if spell.type == "WhiteMagic" then 
		equip(sets.midcast.cure)
	else
		-- do something for when I cast non whitemagic spell
	end
end


But here's a problem, if I cast Dia, that's also white magic, and with the above rule, that would mean that I would cast dia in my cure set. So when need to be more precise, and make sure that I ignore white magic that's not cure. So I'll add another rule in there! We can add an if within an if. That way the second if check only happen if the first if is true. So for now, I'll do this: If I have white magic, then check if I have healing magic. (we can know this looking at the gearswap user script reference page we openned)
Code
function midcast(spell)
	if spell.type == "WhiteMagic" then
		if spell.skill == "Healing Magic" then
			equip(sets.midcast.cure)
		end
	else
		-- do something for when I cast non whitemagic spell
	end
end


Now that's also cool, but -na spells are also Healing Magic skill spells. And I know i don't wanna cast Cursna in my cure set... Oooh boy. We can check for the name!

"But Liz, shouldn't we just check for the name first instead of doing all these checks?" We could, but shush, you're learning. Before we make the proper rule for the cure spell, we'll get into a bit of code logic, and symbols.

Lesson 7 - Making better rules, with actual logic!

You can put the following after ifs, if what you are testing are numbers.

== means it is equal
~= means not equal
> means greater than
< means smaller than
>= means greater or equal than
<= means smaller of equal than

for example: "if spell.mp_cost >= 100 then" would test if the cost of mp of your spells is greater or equal to 100. But in the case of strings, like the name of the spell you can only use == or ~= from the above list. That's cause a name's a name's there is no name that are greater or lesser than other names. Code is woke af.

So, what we can do is "if spell.name == "Cure" then" but that would only be true if you cast Cure. When you cast Cure II then that rule would not work because "Cure II" is not the same name as "Cure". So one thing we can do is add logic operators. You can google "logic operators lua" and you'll get a bunch of them but for now we'll focus on 2 of them "and" and "or". You can put those between mutliple ifs conditions to be able to do things like IF this AND that THEN or IF this OR that THEN. Make sense? You can add many so for now we can do this:
Code
	if	spell.name == "Cure" or 
		spell.name == "Cure II" or 
		spell.name == "Cure III" or 
		spell.name == "Cure IV" or
		spell.name == "Cure V" or
		spell.name == "Cure VI" or
		spell.name == "Cura" or
		spell.name == "Curaga" or
		spell.name == "Curaga II" or
		spell.name == "Curaga III" or
		spell.name == "Curaga IV" or
		spell.name == "Curaga V" then
			equip(sets.midcast.cure)
	else
		-- do something for when I cast non whitemagic spell
	end



OK, so that should work for only cure spells! But hold up right there. There is a better way to do this, in fact, this next way is future proof so if we ever get Cure twelve. It'll still work. For that we'll use something called methods. A method is kinda like a function but that works within the variable we're dealing with. Basically it's like asking our variable for more info. Currently the variable we are dealing with is "spell.name" so we know that this is going to be the name of our spell. Sadly gearswap didn't document all those methods because they mostly come from Windower (I think someone plz correct me here), so it's a bit of trial and error and googling and looking into the meat of gearswap/windower files to find out what you can do here, and that's a bit out of the scope for this class here. So for that one, I'll give you the answer straight up.

After going into the spell.name notice the : that means we're asking a method for more info, and we're asking the "match" method. so that will check inside spell.name if we have a match for "Cure". If we do the method will return "true" if we cannot find a match, then the method will return false. So for example in "Cure IV" we can match "Cure".
Code
if spell.name:match('Cure') then


So once the check happens the above code would read "if true then" or "if false then" that is why we do not need to code it like "if spell.name:match('Cure') == true then"

Now the above is not enough because it'll skip Cura and Curaga. But if we try to match only "Cur" then it'll catch Cursna.

So the final rule for our cure set will end up looking like this:
Code
function midcast(spell)
	if spell.name:match('Cure') or spell.name:match('Cura') then
			equip(sets.midcast.cure)
	else
		-- do something for when I cast non cure spells
	end
end


In the above, it will check for a match for cure or cura, if either one matches, the condition will be true and we'll equip the cure set.

Go ahead and reload (//gs r) and test it out! Remember to check using //gs showswaps

Ooof That was a heavy one! I hope you're still with me here. The rest will get a bit easier because you'll be a bit more familiar with the IF and conditions.

Next class, we'll take a look at adding JA and WS sets!

Cheerios~
Author: Elizabet
Date Created: 2020-01-21 15:10:32
Date Last Modified: 2020-01-21 21:40:45
Updates: 6
Bytes: 8541