Changing variables help!

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
suckerr70
Posts: 2
Joined: Sat Mar 18, 2006 7:46 pm

Changing variables help!

Post by suckerr70 »

Ok, so Im trying to make a simple game (yes this code very based off a tutorial but It's my first program/game i'm working on) and I'm trying to get the "R" trigger button to change weapons and weapon names but when I hit "R", it just flickers "Charged" and goes right back to "RockBuster". Thank you for your help! After I'm going to add sprites and stuff. Please Help me.

This is what I think my problem is:

Code: Select all

if pad:r() and oldpad:r() ~= pad:r() and currentWeapon ==  1 then
currentWeapon = 2 else
currentWeapon = 1
end
Heres the whole thing:

Code: Select all

green = Color.new(0,255,0)
red = Color.new(255,0,0)
blue = Color.new(0,0,150)
white = Color.new(200,200,200)

currentEnemy = 1
currentWeapon = 1
currentPlayer = 1

pad = Controls.read()
oldpad = Controls.read()

Enemy = {}
Enemy[1] = { type = "Fireman" , health = 100 }
Enemy[2] = { type = "Iceman", health = 100 }
Enemy[3] = { type = "Gutsman", health = 100 }
Enemy[4] = { type = "Elecman", health = 100 }
Enemy[5] = { type = "Cutman", health = 100 }

Player = {}
Player[1] = { name = "Rockman" , health = 100 }
Player[2] = { name = "Blues" , health = 100 }

Weapon = {}
Weapon[1] = { weapon = 5 , name = "Rockbuster" }
Weapon[2] = { weapon = 20 , name = "Charged Shot"}

while true do

screen:clear()
screen:print(5,10, Player[currentPlayer].name .. "s Health: " .. Player[currentPlayer].health,green)
screen:print(5,20, Player[currentPlayer].name .. "s Weapon: " .. Weapon[currentWeapon].name,green)

screen:print(250,20,"Enemy Name: " .. Enemy[currentEnemy].type,red)
screen:print(250,10,"Enemy Health: " .. Enemy[currentEnemy].health,red)

screen:print(90, 40,"Press [X] to Attack!!!",white)

if currentEnemy == 5 and Enemy [currentEnemy].health == 0 then
screen:print(100,100,"All RobotMasters have been Killed!",blue)
end

if Enemy&#91;currentEnemy&#93;.health == 0 and currentEnemy <= 4 then
currentEnemy = currentEnemy + 1
end

if pad&#58;cross&#40;&#41; and oldpad&#58;cross&#40;&#41; ~= pad&#58;cross&#40;&#41; and Enemy&#91;currentEnemy&#93;.health > 0 then
Enemy&#91;currentEnemy&#93;.health = Enemy&#91;currentEnemy&#93;.health - Weapon&#91;currentWeapon&#93;.weapon
end

if pad&#58;r&#40;&#41; and oldpad&#58;r&#40;&#41; ~= pad&#58;r&#40;&#41; and currentWeapon ==  1 then
currentWeapon = 2 else
currentWeapon = 1
end

screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;
oldpad = pad

pad = Controls.read&#40;&#41;
if pad&#58;start&#40;&#41; then
break
end
end
Thanks again!

P.S.
Also, If its not to hard, can anyone tell me instead of just making the enemies health go down right when you press "X", that a picture(bullet) moves from your current location straight to the last direction you were facing and then if it hits another picture (enemie) that the "health" variable goes down.? and yes... I do love Rockman (Megaman)... THANKS~!
CT_Bolt
Posts: 14
Joined: Sat Mar 04, 2006 3:21 am
Location: Computer Chair

Code is fixed.

Post by CT_Bolt »

Your code is fixed:

I'm not sure what exactly was wrong with it but, The lines with -- *** next to them are the comments I added(next to code I changed).

Code: Select all

green = Color.new&#40;0,255,0&#41;
red = Color.new&#40;255,0,0&#41; 
blue = Color.new&#40;0,0,150&#41; 
white = Color.new&#40;200,200,200&#41; 

currentEnemy = 1 
currentWeapon = 1 
currentPlayer = 1 

--pad = Controls.read&#40;&#41;     -- *** This goes near the top of the main loop
--oldpad = Controls.read&#40;&#41; -- *** You don't need this here

Enemy = &#123;&#125; 
Enemy&#91;1&#93; = &#123; type = "Fireman" , health = 100 &#125; 
Enemy&#91;2&#93; = &#123; type = "Iceman", health = 100 &#125; 
Enemy&#91;3&#93; = &#123; type = "Gutsman", health = 100 &#125; 
Enemy&#91;4&#93; = &#123; type = "Elecman", health = 100 &#125; 
Enemy&#91;5&#93; = &#123; type = "Cutman", health = 100 &#125; 

Player = &#123;&#125; 
Player&#91;1&#93; = &#123; name = "Rockman" , health = 100 &#125; 
Player&#91;2&#93; = &#123; name = "Blues" , health = 100 &#125; 

Weapon = &#123;&#125; 
Weapon&#91;1&#93; = &#123; weapon = 5 , name = "Rockbuster" &#125;
Weapon&#91;2&#93; = &#123; weapon = 20 , name = "Charged Shot"&#125;

while true do
  
  pad = Controls.read&#40;&#41; -- *** This is where it should read controls

  screen&#58;clear&#40;&#41;
  screen&#58;print&#40;5,10, Player&#91;currentPlayer&#93;.name .. "s Health&#58; " .. Player&#91;currentPlayer&#93;.health,green&#41;
  screen&#58;print&#40;5,20, Player&#91;currentPlayer&#93;.name .. "s Weapon&#58; " .. Weapon&#91;currentWeapon&#93;.name,green&#41;

  screen&#58;print&#40;250,20,"Enemy Name&#58; " .. Enemy&#91;currentEnemy&#93;.type,red&#41;
  screen&#58;print&#40;250,10,"Enemy Health&#58; " .. Enemy&#91;currentEnemy&#93;.health,red&#41;

  screen&#58;print&#40;90, 40,"Press &#91;X&#93; to Attack!!!",white&#41;

  if currentEnemy == 5 and Enemy &#91;currentEnemy&#93;.health == 0 then
     screen&#58;print&#40;100,100,"All RobotMasters have been Killed!",blue&#41;
  end

  if Enemy&#91;currentEnemy&#93;.health == 0 and currentEnemy <= 4 then
     currentEnemy = currentEnemy + 1
  end

  if pad&#58;cross&#40;&#41; and oldpad&#58;cross&#40;&#41; ~= pad&#58;cross&#40;&#41; and Enemy&#91;currentEnemy&#93;.health > 0 then
     Enemy&#91;currentEnemy&#93;.health = Enemy&#91;currentEnemy&#93;.health - Weapon&#91;currentWeapon&#93;.weapon
  end

 -- *** This is a better way of changing weapons&#58;
  if pad&#58;r&#40;&#41; and oldpad&#58;r&#40;&#41; ~= pad&#58;r&#40;&#41; then
     currentWeapon = currentWeapon + 1
     if currentWeapon > table.getn&#40;Weapon&#41; then currentWeapon = 1 end
  end

  screen.waitVblankStart&#40;&#41;
  screen.flip&#40;&#41;

  oldpad = pad

  --pad = Controls.read&#40;&#41; -- ***  This is near the top.

  if pad&#58;start&#40;&#41; then break end -- *** This looks better.

end
You were right about what was wrong but, there were more things I corrected.

By the way I'm here to help, if you have any other problems.
suckerr70
Posts: 2
Joined: Sat Mar 18, 2006 7:46 pm

Post by suckerr70 »

Thank-you very much! I get all the things you shifted around and/or took out but I don't really get what exacly was going on with the actual weapon shifting thing. I get how yours works but what exacly made my change right back to the original weapon? Since I am a brand new at this I would like to understand how my way didn't work so I don't make that mistake again. Like what exacly did the computer "think/do" when it was reading my script.

Thanks!

*EDIT*
One last thing I don't understand. Why should "oldpad = pad" be near the bottem? And is there a difference between "oldpad = pad" and "oldpad = Controls.read()"?
CT_Bolt
Posts: 14
Joined: Sat Mar 04, 2006 3:21 am
Location: Computer Chair

What it means.

Post by CT_Bolt »

Code: Select all

oldpad = pad
This needs to be near the bottom because that is right before you make pad read the controls agian(when it starts looping again).

So that you can compare the controls then to the controls now.

If you make oldpad = Controls.read() then it gets the current controls pressed not what was pressed before.

Code: Select all

if pad&#58;r&#40;&#41; and oldpad&#58;r&#40;&#41; ~= pad&#58;r&#40;&#41; and currentWeapon ==  1 then 
currentWeapon = 2 else 
currentWeapon = 1 
end 
What the above means:

if R is pressed currently and if R that was pressed before(the loop ended) doesn't equal the current R value and the currentWeapon(weapon number) is equal to 1(not anything else) then
set currentWeapon = 2
else if R is not pressed currently and if R that was pressed before(the loop ended) does equal and currentWeapon isn't equal to 1(any number but 1) then
set currentWeapon = 1
end


Remember: pad:r() is a true or false value, and the 'else' statement is the exact opposite of the if statements before it.

If you need more help, post what you need.
Post Reply