A few quick questions about some of the code i've started

Post Reply
Jarrin
Posts: 20
Joined: Sat Nov 21, 2009 10:14 pm

A few quick questions about some of the code i've started

Post by Jarrin »

Ok, so I'm attempting to write a script that will help to speed up filling of vials in achaea. I've decided to store the ingredients for each type of concoction in a table called "concoctons" and then create a sub table for each type of vial.

Table:
Code: [show] | [select all] lua
concoctions = {

health = {
	"valerian",
	"goldenseal",
	"ginseng",
	"myrrh",
},

mana = {
	"slipper",
	"bellwort",
	"hawthorn",
	"bloodroot",
},

immunity = {
	"sac",
	"ash",
	"echinacea",
        "echinacea",
},

epidermal = {
	"kuzu",
        "kuzu",
	"bloodroot",
	"hawthorn",
	"ginseng",
}
The next thing that I've done is to create some functions that will be necessary. The first is a function to take the herbs out of my rift:
Code: [show] | [select all] lua
outr() 
for k,v in pairs(concoctions.type) do
		send("outr " ..(v))
end
I think this code will make it so that it runs through the subtable "type",which I'm going to have to be more specific on, until it runs out of elements and will then outr each element. Am I approaching this correctly?

I don't want to move much further ahead until I know if what I'm doing is sound. Thanks!

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: A few quick questions about some of the code i've starte

Post by Vadi »

Data is sound, function not quite. Here's a fixed up version:
Code: [show] | [select all] lua
function outr(type) 
for k,v in ipairs(concoctions[type]) do
                send("outr " ..(v))
end

Jarrin
Posts: 20
Joined: Sat Nov 21, 2009 10:14 pm

Re: A few quick questions about some of the code i've starte

Post by Jarrin »

Thanks Vadi. Seeing it that way makes more sense now.

Jarrin
Posts: 20
Joined: Sat Nov 21, 2009 10:14 pm

Re: A few quick questions about some of the code i've starte

Post by Jarrin »

Ok, I've finished my refiller and it seems to be working really well! Now I'm trying my hand at a harvester but this is more challenging for me.

Tables
Code: [show] | [select all] lua
toharvest = {
	["ginseng"] = false,
	["ash"] = false,
	["echinacea"] = false,
	["ginger"] = false,
	["myrrh"] = false,
	["bloodroot"] = false,
	["hawthorn"] = false,
	["kuzu"] = false,
	["skullcap"] = false,
	["slipper"] = false,
	["goldenseal"] = false,
	["valerian"] = false,
	["bayberry"] = false,
	["cohosh"] = false,
	["lobelia"] = false,
	["pear"] = false,
	["elm"] = false,
}

balance = {
	["hasbalance"] = true,
}
Here is the function that I've written. I'm not sure if it is all correct, but I've tried to build on the skills I learned. I want it to move through the table toharvest and check to see if the value is true and I have balance then harvest the key(herb).
Code: [show] | [select all] lua
function harvest(hasbalance,herb)
	for k,v in ipairs(toharvest[herb]) do
				if v== true and balance[hasbalance] == true then
					send("harvest "..(k))
				else
				cecho("CANNOT HARVEST")
				end
	end
end
Edit:
I also have triggers set up to set the values in the tables:
Code: [show] | [select all] lua
A low-slung myrrh bush is here.       EXACT MATCH
excecutes: toharvest["myrrh"] = true
And to set balance off:
Code: [show] | [select all] lua
You reach out and carefully harvest BEGINNING OF LINE SUBSTRING
executes: balance["hasbalance"] = false
Then there is also a trigger for the regain of balance.

Any help or suggestions? I'm not sure what to do next.

Iocun
Posts: 174
Joined: Wed Dec 02, 2009 1:45 am

Re: A few quick questions about some of the code i've starte

Post by Iocun »

There seems to be some confusion about the correct way to address table entries in some of your code snippets.

If we have the table: mytable = {firstentry = "foo", secondentry = "bar", thirdentry = "foobar"} then there are two different ways of addressing individual entries.

Variant 1: mytable.firstentry
Variant 2: mytable["firstentry"]

As you might know, these two are equivalent. They will both get you "foo" in our example here.

In those two cases we always knew what they keys of the table entries were called. That's why we used literally "firstentry" with quotation marks in Variant 2. The dot notation in Variant 1 also always assumes you are using literal key names.

If, however, you want to address an entry in a table of which you don't know the name of the key in advance, you can't use this. An example here would be the concoctions[type] in your snippet above. Note that we don't have quotation marks around "type" in this example, because you aren't literally using the table entry called "type", but the key name that is -contained- in the variable named "type". That's why Vadi corrected your original variant: "concoctions.type".

concoctions.type is, as explained above, the same as concoctions["type"], with quotation marks. That's why you can't use it here if you aren't literally looking for the key "type" in the "concoctions" table. While you have corrected this now in your newer version, the issue still seems not to be entirely clear to you yet, since now you used: balance[hasbalance] == true.
You -didn't- use quotation marks here, which means this doesn't check for the value "hasbalance" in the "balance" table, but expects "hasbalance" to be a -variable- that -contains- the name of a key in the balance table. But since you don't actually have such a variable, balance[hasbalance] will always evaluate as false.

The correct syntax here would be either balance["hasbalance"] or balance.hasbalance.

I hope this cleared this up somewhat.

User avatar
Oneymus
Posts: 321
Joined: Thu Sep 17, 2009 5:24 am

Re: A few quick questions about some of the code i've starte

Post by Oneymus »

Should also be noted:

You're trying to iterate through a table that doesn't exist. ipairs(toharvest[herb]) is looking for the table contained in the herb index of the toharvest table. However, the herb indexes of the toharvest table, in this case, contain boolean values.

To rectify, simply iterate through ipairs(toharvest); v will then contain the boolean value (true, false) you want, and k will contain the herb ("ginseng", "ash", etc).

Jarrin
Posts: 20
Joined: Sat Nov 21, 2009 10:14 pm

Re: A few quick questions about some of the code i've starte

Post by Jarrin »

Ok, my variables are updating correctly in the tables now that I've gone through and made the changes that needed to be changed. I think I have a better grasp on the syntax for referring to table entries now.

I still have a problem with my main harvest function. The function relies on the value of the herb variable being set to true (which it is doing correctly) and that the hasbalance variable is true (which it is doing correctly).

I know that the function as written is not getting any values passed to it because if it were they would be in the function declaration line inside of my parenthesis.

My thought is that I need to pass the variable hasbalance from the balance table during the function declaration, is that correct or will my function be able to pull the values of hasbalance and the toharvest table without needing to be passed?

Here is the function as it is written now:
Code: [show] | [select all] lua
function harvest()
        for k,v in ipairs(toharvest) do
            if v==true  and balance["hasbalance"]==true then
                send("harvest "..(k))
            else
                cecho("CANNOT HARVEST")
            end
        end
end
Thanks to Sohl for helping me to understand that I can't use ipairs for this instance because the values that I have aren't indexed as numbers they are indexed as strings. I also didn't realize that in Lua you dont use == to evaluate a boolean against true, the variable automatically assesses against true without the ==.

I also cut out the else statement that had the CECHO because it evaluated against all of the values of the table and would return "CANNOT HARVEST" for every false value, which I didn't realize until I saw it happen, but it makes sense sice it iterates through ever instance of the table.

Fixed up code:
Code: [show] | [select all] lua
function harvest()
        for k,v in pairs(toharvest) do
            if v and balance["hasbalance"] then
                send("harvest "..(k))
        end
end

Jarrin
Posts: 20
Joined: Sat Nov 21, 2009 10:14 pm

Re: A few quick questions about some of the code i've starte

Post by Jarrin »

Ok, the above function harvest() was partially working last night. It would attempt to harvest the next plant in the table with a true value each time I entered the harvest alias.

However, I logged in today and for some reason the function isn't getting past my if statement. I checked the values of the tables in game using display() and hasbalance is returning as true and there are some of the plant values set to true. Why might my if statement be failing?

Edit: Problem solved I had a dual naming issue between another variable and the table name.

Post Reply