This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
minecrash's profile picture

[lua]if state problem

Started by minecrash, 12 November 2012 - 03:49 AM
minecrash #1
Posted 12 November 2012 - 04:49 AM
my friend is trying to make a simple pokemon battle but for some reason is there a problem at line 63. even when M0000 is set to "-" at the begin of the script does the if state disagree. any idea whats wrong?

the script:

M0000 = "-"
M0001 = "Tackle"
M0002 = "Tail Whip"
M0003 = "Growl"
function yN()
  selection=1
	while true do
	  term.clear()
	  term.setCursorPos(1,1)
	  local x, y=term.getCursorPos()
	  term.clearLine()
	  if selection==1
		then write(">Attack< Exit ")
		else write (" Attack >Exit<") end
	  term.setCursorPos(x, y)
	  a, b=os.pullEvent()
	  while a~="key"
	  do a, b=os.pullEvent() end
	if b==203 and selection==2 then selection=1 end
	if b==205 and selection==1 then selection=2 end
	if b==28 then print ("") break end
	end
  if selection==1 then return true end
  if selection==2 then return false end
  return false
end
function atk(m)
  attack=1
  l=#m
  while true do
	term.clear()
	term.setCursorPos(1,1)
	print("Select your move:")
	for i=1, l, 1 do
	  if i==attack then print("["..m[i].."]") else print(" ", m[i]) end
	  end
	a, b= os.pullEventRaw()
	if a== "key" then
	if b==200 and attack>1 then attack=attack-1 end
	if b==208 and attack<l then attack=attack+1 end
	if b==28 then break end
	end
  end
  term.clear()
  term.setCursorPos(1,1)
  return attack
end

while yN() == true do
  if selection == 1 then
	local moves={
	M0001,
	M0002,
	M0003,
	M0000,
	"Back"
	}
	local attack=atk(moves)
	if attack == 5 then
	if M0000 == "-" then
	  print("No Attack!")
	  sleep(1)
	  end
	else
	  print(attack)
	  sleep(1)
	end
  else
	if selection == 2 then
	  break
	end
  end
end
Fiery Arcanine #2
Posted 12 November 2012 - 04:50 AM
Guys, dont worry.
He did not steal this.
I'm his friend FYI
Sammich Lord #3
Posted 12 November 2012 - 05:30 AM
I don't see the problem. I just tried the code and everything seemed to work fine. Can you explain the error in detail?
minecrash #4
Posted 12 November 2012 - 05:35 AM
the goal is to make the script display "no attack" when the player selects the empty attack (-) but instead it just shows ''4''.
Matt21 #5
Posted 12 November 2012 - 05:50 AM
Replace


  if selection==1 then return true end
  if selection==2 then return false end

by


  if selection==1 then
   return true
  end
  if selection==2 then
   return false
  end

It's work?
ardera #6
Posted 12 November 2012 - 05:54 AM
Replace


  if selection==1 then return true end
  if selection==2 then return false end

by


  if selection==1 then
   return true
  end
  if selection==2 then
   return false
  end

It's work?
Thats the same…
remiX #7
Posted 12 November 2012 - 06:07 AM
Edit: Nevermind, I was wrong :unsure:/>/>

Edit #2: Try make

a, b= os.pullEventRaw()
into
a, b = os.pullEvent() -- using raw will prevent os.pullEvent to not function at all (I think...)
Lyqyd #8
Posted 12 November 2012 - 06:38 AM
Check out your if placement and ordering. Try selecting "Back" and see what it says. You've just got your code in an order that it's doing things you don't expect it to. Specifically, check out the if attack == 5 then just before your M0000 comparison.
Espen #9
Posted 12 November 2012 - 06:41 AM
the goal is to make the script display "no attack" when the player selects the empty attack (-) but instead it just shows ''4''.
That's because you only print "No Attack!" when attack == 5, but the "-" option is attack == 4.

EDIT: Whoops, ninja'd before refresh. :unsure:/>/>
Edited on 12 November 2012 - 05:42 AM
Fiery Arcanine #10
Posted 12 November 2012 - 06:44 AM
I have fixed that. My friend made a mistake with that.
Now, the change to pullEventRaw to pullEvent didn't work.

RIght know it is this:
Spoiler

M0000 = "-"
M0001 = "Tackle"
M0002 = "Tail Whip"
M0003 = "Growl"

function yN()
  selection=1
	while true do
	  term.clear()
	  term.setCursorPos(1,1)
	  local x, y=term.getCursorPos()
	  term.clearLine()
	  if selection==1
		then write(">Attack< Exit ")
		else write (" Attack >Exit<") end
	  term.setCursorPos(x, y)
	  a, b=os.pullEvent()
	  while a~="key"
	  do a, b=os.pullEvent() end
	if b==203 and selection==2 then selection=1 end
	if b==205 and selection==1 then selection=2 end
	if b==28 then print ("") break end
	end
  if selection==1 then return true end
  if selection==2 then return false end
  return false
end

function atk(m)
  attack=1
  l=#m
  while true do
	term.clear()
	term.setCursorPos(1,1)
	print("Select your move:")
	for i=1, l, 1 do
	  if i==attack then print("["..m[i].."]") else print(" ", m[i]) end
	  end
	a, b= os.pullEvent()
	if a== "key" then
	if b==200 and attack>1 then attack=attack-1 end
	if b==208 and attack<l then attack=attack+1 end
	if b==28 then break end
	end
  end
  term.clear()
  term.setCursorPos(1,1)
  return attack
end


while yN() == true do
  if selection == 1 then
	local moves={
	M0001,
	M0002,
	M0003,
	M0000,
	"Back"
	}
	local attack=atk(moves)
	if attack == 4 then
	if M0000 == "-" then
	  print("No Attack!")
	  sleep(1)
	  end
	elseif attack == 5 then
	else
	  print(attack)
	  sleep(1)
	end
  else
	if selection == 2 then
	break
	end
  end
end

It is now set to if attack == 4 then but that takes over if M0000 == "-" then.
If I removed the first one. It gives this error:
bios:338: [string "pbsselect"]:72: 'end' expected (to close 'while' at line 52)

Let's say that is fixed. What after that?

Edit: If I put another end at Line 72 (and put the current line 72 to 73) I get:
bios:338: [string "pbsselect"]:73: '<eof>' expected

Edit2: The problem is, we think that a table doesn't accept strings when asked for a statement. I might be wrong (and I probably am) but we tried a lot, and we broke and fixed it a lot. But everytime it just says 4 instead of No Attack! It needs to know that M0000 is not a move. As it needs to be used alot.
remiX #11
Posted 12 November 2012 - 07:58 AM
Where you have

        if M0000 == "-" then
          print("No Attack!")
          sleep(1)
          end
Do this to check if it prints "Failed" .. maybe the comparison is failing somehow?


if M0000 == "-" then
  print("No Attack!")
  sleep(1)
else
  print("Failed")
  sleep(10)
end
Lyqyd #12
Posted 12 November 2012 - 08:14 AM
I cleaned up the code a little bit. Give this a try:


M0000 = "-"
M0001 = "Tackle"
M0002 = "Tail Whip"
M0003 = "Growl"

function yN()
  selection=1
        while true do
            term.clear()
            term.setCursorPos(1,1)
            local x, y=term.getCursorPos()
            term.clearLine()
            if selection==1    then
                write(">Attack< Exit ")
            else
                write(" Attack >Exit<")
            end
            term.setCursorPos(x, y)
            a, b=os.pullEvent("key") --fixed odd loop filtering; only pulls key events now.
            if b==203 and selection==2 then --fixed casing to use elseif
                selection=1
            elseif b==205 and selection==1 then
                selection=2
            elseif b==28 then
                print("")
                break
            end
	    end
    if selection==1 then --fixed casing to use elseif
        return true
    elseif selection==2 then
        return false
    end
    return false
end

function atk(m)
    attack=1
    l=#m
    while true do
        term.clear()
        term.setCursorPos(1,1)
        print("Select your move:")
        for i=1, l, 1 do
            if i==attack then print("["..m[i].."]") else print(" ", m[i]) end
        end
        a, b= os.pullEvent("key") --fixed event filtering.
        if b==200 and attack>1 then --fixed casing to use elseif
            attack=attack-1
        elseif b==208 and attack<l then
            attack=attack+1
        elseif b==28 then
            break
        end
    end
    term.clear()
    term.setCursorPos(1,1)
    return attack
end


while yN() == true do
    if selection == 1 then
        local moves={
        M0001,
        M0002,
        M0003,
        M0000,
        "Back",
        }
        local attack=atk(moves)
        if moves[attack] == "-" then --fixed attack check to index table, rather than rely on only fourth slot being a non-attack.
            print("No Attack!")
            sleep(1)
        elseif attack == 5 then
        else
            print(attack)
            sleep(1)
        end
    elseif selection == 2 then
        break
    end
end
Fiery Arcanine #13
Posted 12 November 2012 - 08:21 AM
Can you please state the changes you made?
I'd like to know that's changed for further errors
Lyqyd #14
Posted 12 November 2012 - 08:30 AM
Edited code to add comments.
Fiery Arcanine #15
Posted 12 November 2012 - 08:35 AM
Edited code to add comments.
Thanks.
I assume this works?
I'm gonna try this tomorrow.

I'm gonna report if it works or not but I will already thank you for fixing the code.
Unless Minecrash tests and reports, I do tomorrow.

Edit: no posts until report please!
Lyqyd #16
Posted 12 November 2012 - 09:19 AM
I didn't personally test the code, but I don't think I would have broken anything by rearranging things like I did. It should also fix the "No Attack!" not showing up bug.
Fiery Arcanine #17
Posted 13 November 2012 - 03:18 AM
I didn't personally test the code, but I don't think I would have broken anything by rearranging things like I did. It should also fix the "No Attack!" not showing up bug.

Well, it worked perfectly as it should be!
Thanks alot.