Author Topic: Need help with a lua code  (Read 2143 times)

Hi, today I wanted to try to make a RPG game out of pure lua.
Everything was fine until I wanted to make a fight system. I got all the code ffor the fight done, but exiting out of it is a pain. I have a function on top of my code and I prompt it by approach(mobname, mobhp, mobdmg)
Now it works just fine, the fight is cool but when I finish the fight I have no idea how to make it go back to where I was in the world.
I tried the goto command it's very important for the cycles in the fight like ::w:: then the code for the whole fight and when the fight is done and player and monster have some hp i just goto w
But when I try do that with my fight system like my idea was to make approach(mobname, mobhp, mobdmg, label) but you can't put a variable in a goto I think so my idea wouldn't work all I am left with is copying the whole wall of code everytime I want a fight to happen which is what I don't want to do
Can anybody help me?


I barely know lua, you'd be a lot better off asking this is a lua based forum, just as a heads up.

I barely know lua, you'd be a lot better off asking this is a lua based forum, just as a heads up.
yes sorry but i thought some people on this forum know lua from a to z and can help me like what command to use with this because this is really important to me because my friend gives me $15 if i make a rpg game :D

use python and tell him it is lua

use python and tell him it is lua
B-b-but Blockguy. Th-th-that's chea-cheating.

B-b-but Blockguy. Th-th-that's chea-cheating.
does it matter if it is cheating

does it matter if it is cheating
Yes! I-it's not like I've done st-st-st-steroids b-before!

Keep the state of your RPG either using a global table, then use functions to alter it (such as exiting out of a fight)? That's just one way you could do it.
« Last Edit: July 03, 2013, 04:02:57 PM by Port »

You're being kinda vague. It'd be more helpful if you posted your code on pastebin and linked it here.

You're being kinda vague. It'd be more helpful if you posted your code on pastebin and linked it here.

Yeah, there's not much to say when we have no idea what you're /actually/ doing. Only vague suggestions of general ways to manage everything.

Here is the code. The fight function part is indicated by a hole forgetload of @@@s.
If you want to run the code, just tell me, you require two files
http://pastebin.com/DLtH3df6

Alright, this entire section:
Code: [Select]
        ::w::
        print("What do you do?")
        print("ATTACK")
       
        while true do
                pump = io.read()
                if pump == "attack" then
                        wait(0.5)
                        print("You use ATTACK!")
                        monhp = monhp - curweapondmg
                        print("You deal "..curweapondmg.." damage to "..mon)
                        print(mon.."'s HP is now "..monhp)
                                if monhp <= 0 then
                                        print(mon.." has fainted!")
                                        break
                                elseif monhp > 0 then
                                        wait(1)
                                        goto w
                                end
                        wait(1)
                        print(mon.." uses ATTACK!")
                        if plyhp < mondmg then
                                print("It's super effective! :D")
                                print(plyname.." is DEAD!")
                                print("You are dead.")
                                plydead = 1
       
       
                        elseif plyhp > mondmg then
                                plyhp = plyhp - mondmg
                                print(mon.." deals "..mondmg.." damage to "..plyname)
                                print(plyname.."'s HP is now "..plyhp)
                        end
               
         
 
                end
        end

Has a pointless "goto w" loop. After you check if the monster's hp is greater than or less than or equal to 0, the code after that in the loop will never be executed.

Code: [Select]
        while true do
                print("What do you do?")
                print("ATTACK")

                pump = io.read()
                if pump == "attack" then
                        wait(0.5)
                        print("You use ATTACK!")
                        monhp = monhp - curweapondmg
                        print("You deal "..curweapondmg.." damage to "..mon)
                        print(mon.."'s HP is now "..monhp)
                        if monhp <= 0 then
                                print(mon.." has fainted!")
                                break
                        end
                        wait(1)
                        print(mon.." uses ATTACK!")
                        if plyhp < mondmg then
                                print("It's super effective! :D")
                                print(plyname.." is DEAD!")
                                print("You are dead.")
                                plydead = 1
       
       
                        elseif plyhp > mondmg then
                                plyhp = plyhp - mondmg
                                print(mon.." deals "..mondmg.." damage to "..plyname)
                                print(plyname.."'s HP is now "..plyhp)
                        end
               
         
 
                end
        end

I would try to refrain from using goto and ::labels:: whenever possible, unless absolutely necessary and is not able to be implemented with a simple loop.

Alright, this entire section:
Code: [Select]
        ::w::
        print("What do you do?")
        print("ATTACK")
       
        while true do
                pump = io.read()
                if pump == "attack" then
                        wait(0.5)
                        print("You use ATTACK!")
                        monhp = monhp - curweapondmg
                        print("You deal "..curweapondmg.." damage to "..mon)
                        print(mon.."'s HP is now "..monhp)
                                if monhp <= 0 then
                                        print(mon.." has fainted!")
                                        break
                                elseif monhp > 0 then
                                        wait(1)
                                        goto w
                                end
                        wait(1)
                        print(mon.." uses ATTACK!")
                        if plyhp < mondmg then
                                print("It's super effective! :D")
                                print(plyname.." is DEAD!")
                                print("You are dead.")
                                plydead = 1
       
       
                        elseif plyhp > mondmg then
                                plyhp = plyhp - mondmg
                                print(mon.." deals "..mondmg.." damage to "..plyname)
                                print(plyname.."'s HP is now "..plyhp)
                        end
               
         
 
                end
        end

Has a pointless "goto w" loop. After you check if the monster's hp is greater than or less than or equal to 0, the code after that in the loop will never be executed.

Code: [Select]
        while true do
                print("What do you do?")
                print("ATTACK")

                pump = io.read()
                if pump == "attack" then
                        wait(0.5)
                        print("You use ATTACK!")
                        monhp = monhp - curweapondmg
                        print("You deal "..curweapondmg.." damage to "..mon)
                        print(mon.."'s HP is now "..monhp)
                        if monhp <= 0 then
                                print(mon.." has fainted!")
                                break
                        end
                        wait(1)
                        print(mon.." uses ATTACK!")
                        if plyhp < mondmg then
                                print("It's super effective! :D")
                                print(plyname.." is DEAD!")
                                print("You are dead.")
                                plydead = 1
       
       
                        elseif plyhp > mondmg then
                                plyhp = plyhp - mondmg
                                print(mon.." deals "..mondmg.." damage to "..plyname)
                                print(plyname.."'s HP is now "..plyhp)
                        end
               
         
 
                end
        end

I would try to refrain from using goto and ::labels:: whenever possible, unless absolutely necessary and is not able to be implemented with a simple loop.

It goes to w if player's HP is over zero AND the monster's HP is over zero, if the player's hp is equal to/less than zero it kills the player with plydead = 1 and it goto's the beggining of the code, if the monster dies though the code faints the monster but I have no idea how to make it exit out of the function and go back to the world

That's my point, it shouldn't "goto w" at all, it should just restart the loop, asking the player to attack again. (I assume you have other actions planned aside from "attack", otherwise asking the player to type it is pointless, and would be annoying in the case of a typo.)

Also, why do you have multiple loops? Your game should be running under one loop, calling multiple functions to do different tasks, such as going to the store, fighting a monster, etc.
Code: [Select]
-- variables in such
while true do
  -- walking around i suppose
  if iCanEncounterAMonster then
    approach(someRandomMonster)
  end
  if iCanGoToTheScore then
    gotoTheStore()
  end
end

That should be the extent of your program. Using gotos to hop to and from different loops is extremely bad logic.