Table script among other stuff

Post Reply
Guilend
Posts: 36
Joined: Fri Mar 09, 2018 1:21 am

Table script among other stuff

Post by Guilend »

Okay, worked on this for a few hours and still not getting it.
Code: [show] | [select all] lua
local imageA = {p = "Pierce", b = "Bleed", c = "Crush", sh = "shroud", ds = "dullsense", nb = "Nimbus", lo = "Look",  rx = "Relaxation", hm = "Human", bu = "Burn", tc = "truecripple", tw = "twist", db = "dumb", pr = "protection", md = "meld", wr = "whirl", rv = "revive", fp = "fop"}

function Istyle(imageA, cStyle)
 cStyle = cStyle
 local is_far = cStyle:find("p")
 local cmt = ""
 
 display(imageA)
 display(cStyle) 
 


 if table_contains(imageA, abilityI) then
  abilityI = imageA[abilityI]
	 cmt = " "
	  else
		 cmt = " "
		end
	end
	
	if is_far then
	 cmt = "image " .. cmt
	  else
		 print("Not Found")
		 return
		  end
		
		if not is_far then
		 cmt = "unwield right;wield " .. lastwax " right;image " .. cmt .. ";unwield right"
		  else
			 print("Not Found")
			 return
			end
			
			send(cmt)
		end
	end
This is what I have so far and I know it isn't correct. What I am trying to do is make a similar script as the last two and trying to learn how to make this script work for me and better understand tables.


I want to be able to enter ``i sh`` and it will simple unwield right;wield ``lastwax`` right;image shroud;unwield right. Or if I enter ``i sh p`` It will just send image shroud. I really just want someone to walk me through this and explain what's wrong and how to fix it and why so I can learn from it. Thanks

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: Table script among other stuff

Post by Jor'Mox »

So, first, I STRONGLY recommend you take the time to use proper tab-indenting in your code. Your code above has two extra end statements that were very hard to spot because things weren't properly lined up. I'm going to guess that the reason you added them is due to some thought that you need a separate end statement for an else statement, in addition to the one for the if statement, which isn't the case.

Second, you created a table called imageA, then you added an argument to the Istyle function called imageA, which means that for the purposes of that function, your table is overwritten with the first argument you give to the function, which is undoubtedly NOT what you want. You need to remove the argument from your function declaration, at which point the table will be useable.

Third, you have a pair of if statements that, combined, mean your function will never send any commands. Specifically, you first have "if is_far then", which in its else statement prints an error message and returns, followed by "if not is_far then", which does the exact same thing in its else statement. But it is always the case that one of those two things is true, and the else case of each of those if statements is the if case of the other.

Fourth, do you a whole thing with "abilityI"... but never declare it, receive it as an argument, or do anything with it afterward. This is clearly a problem.

Fifth, it seems as if you are trying to include some values from the table into the command you are going to send... only you never do that. The lines where you are concatenating things together to make the command you will send are using " " (the value of cmt at the time) rather than anything pulled from the table.

Here is your code with a number of comments added, the tabs done properly, and your two extra end statements commented out.
Code: [show] | [select all] lua
local imageA = {p = "Pierce", b = "Bleed", c = "Crush", sh = "shroud", ds = "dullsense", nb = "Nimbus",
    lo = "Look",  rx = "Relaxation", hm = "Human", bu = "Burn", tc = "truecripple", tw = "twist",
    db = "dumb", pr = "protection", md = "meld", wr = "whirl", rv = "revive", fp = "fop"}

function Istyle(imageA, cStyle)
    cStyle = cStyle -- cStyle already equals itself... what is this doing?
    local is_far = cStyle:find("p") -- since this is all cStyle is used for, is cStyle just equal to "p" when used?
    -- if so, just use cStyle == "p", rather than cStyle:find("p")
    local cmt = "" -- this starts out as "", but then is made into " " just a few lines down... why not start as " "?

    display(imageA)
    display(cStyle) 
    -- where does abilityI come from?
    if table_contains(imageA, abilityI) then -- you don't seem to do anything with this
        abilityI = imageA[abilityI]
        cmt = " " -- see comment below
    else
        cmt = " " -- if both cases do the same thing, there is no need to put this inside the if statement
    end
    -- end -- commented out because this would have become the end of the function

    -- you seem to be trying to use some value from the table above in cmt here, but that is never set
    if is_far then
        cmt = "image " .. cmt
    else -- == if not is_far then
        print("Not Found")
        return
    end

    if not is_far then -- this case will never happen
        cmt = "unwield right;wield " .. lastwax " right;image " .. cmt .. ";unwield right"
    else -- == if is_far then -- if it makes it this far, this case will always happen
        print("Not Found")
        return
    end
    send(cmt) -- you can't get here, ever, because of the return statements in your else cases
end
-- end -- commented out because you have a dangling end statement

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

Re: Table script among other stuff

Post by Vadi »

Right-click and 'Format All' is your friend!

Guilend
Posts: 36
Joined: Fri Mar 09, 2018 1:21 am

Re: Table script among other stuff

Post by Guilend »

Thanks, I will get on it soon and try and fix it. As always, I love how you word some of your comments, it's both entertaining and instructive. Someone gave me an online book about lua programming, I will try and dive into that and use what you have told me and the book and see what i can come up with.

Post Reply