Jump to content




RPG Dev. Problems! (elseif, arithmetic, function, etc)


  • You cannot reply to this topic
67 replies to this topic

#1 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 25 November 2013 - 02:04 PM

Hey, ya' all.
First of all, don't mind why this code is for, it's a "beta" for a rpg kinda game that I'm working on, I know there are tons of better ways to this, using tables and stuff, but I don't quite know how to use those, anyways.

Here is the code: pastebin.com/NRqB5Xpy

The error is giving me is: bios:337: [string "battle"]:105: unexpected symbol

And I don't really know why that's happening. I looked up in a bunch of diferent forums, but none of them have a solution for my problem. If any of you could help me, that would be great, and also, this is my first topic, and yes, I read the rules, but if anything is wrong, just tell me.
Any ideas for the code are also very welcome.
Thanks you.

Edited by Strite, 30 November 2013 - 11:48 AM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 25 November 2013 - 02:40 PM

You've got a malformed comparison in your elseif statement. Looks like you missed the "mob" variable on the left side there.

#3 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 25 November 2013 - 03:14 PM

Ok thanks, it really helped me! Sorry for useless topic.

#4 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 25 November 2013 - 03:24 PM

Hey, it's me again.

Sorry for a bothering again, but I got a new problem, and considering that this might help other people's work, I might as well just ask for help, anyways.

Here's the updated code: pastebin.com/JXAWSwcv

The error now is: battle:163: attempt to concatenate nil and string

And sorry, but I have no idea what that is. I looked up in a couple of other topics, but haven't find any that really helped me.

Thanks!

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 25 November 2013 - 05:00 PM

Please stick to one topic for all questions about a given piece of code.

#6 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 25 November 2013 - 05:02 PM

Oh, sorry, at least now I know.
Any help is more than welcome!

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 November 2013 - 05:05 PM

It's complaining because you're trying to combine nil (the value of "first") with some text.

At a glance, it seems you only define "first" in the FirstCollect() function, but you declare it as local to that function - meaning no other functions get to access it.

I see that the other values you declare there have the same issue. Also note that "read()" always returns strings - you may wish to try "tonumber(read())" for when numerals are needed.

#8 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 25 November 2013 - 05:14 PM

Bomb Bloke, is there any way I can make that possible? Make other functions acess other functions information? If not, that's okay, I will figure a solution myself.

Also, I'm not really familiar with this "tonumber(read())", so, I can use it to basicly get an input in form of numerals, insteed of a string? So, something like "local input = tonumber(read())" should work?

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 November 2013 - 05:49 PM

View PostStrite9000, on 25 November 2013 - 05:14 PM, said:

Bomb Bloke, is there any way I can make that possible? Make other functions acess other functions information? If not, that's okay, I will figure a solution myself.
There are a number of ways.

One is to have the function return those values:

local function getStuff()
  local a=read()
  local b=read()
  return a,b
end

local c,d = getStuff()
print( c.." "..d )

Another is to declare the variables as local to the whole script, not just to your function:

local a,b

local function getStuff()
  a=read()
  b=read()
end

getStuff()
print( a.." "..b )

Yet another is to simply not declare the variables as local at all, but this has the side effect of having them remain in RAM when the script ends. Ideally, all variables and functions in your script will be cleared when the script ends.

View PostStrite9000, on 25 November 2013 - 05:14 PM, said:

Also, I'm not really familiar with this "tonumber(read())", so, I can use it to basicly get an input in form of numerals, insteed of a string? So, something like "local input = tonumber(read())" should work?
Yes, that's how it works - the string result of the "read()" function gets passed to the "tonumber()" function, which returns a numerical representation.

Note that if the string can't be converted - say the user typed "cheese" - then "tonumber()" returns nil. Bearing in mind that for the purposes of a conditional test (like for your "if" statements), nil counts as false, you'll typically want to loop until the user co-operates:

local myNumber
repeat
  print("Type a number:")
  myNumber = tonumber(read())
until myNumber


#10 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 25 November 2013 - 06:09 PM

Okay, I think I've got it. I'm gonna try some of these in the code right now, thanks a lot!

And the "tonumber(read())", I see, so, basicly, if a string is giving, when you're asking for a numerical value (using the "tonumber(read())" command), the input it's basicly going to return false, insteed of the string itself. I just don't understand a simple thing, what's the basic diference between getting a number (input) using the "read()" command, and the "tonumber(read())", because, for simple things, It's pretty much the same, I'm guessing.

local number = read()
print ("Number: "..number)

Should work, and:

local number = tonumber(read())
print ("Number: "..number)

Should work as well, I think.
Am I wrong?


EDIT: Now I'm also getting the error: battle:302: attempt to perform arithmetic __sub on number and nil, any ideas why?
Here's the updated code: pastebin.com/HTsWZNN6

Edited by Strite9000, 25 November 2013 - 09:30 PM.


#11 VaNnOrus

  • Members
  • 13 posts

Posted 25 November 2013 - 11:51 PM

View PostStrite9000, on 25 November 2013 - 06:09 PM, said:

EDIT: Now I'm also getting the error: battle:302: attempt to perform arithmetic __sub on number and nil, any ideas why?
Here's the updated code: pastebin.com/HTsWZNN6

local BuckleyH = BuckleyH - firstFinalAttack

You are trying to take away some value from non-existent variable
You must declare a variable before operations with it, like that
local a = 2
a = a - 1 -- now a = 1

Edited by VaNnOrus, 25 November 2013 - 11:51 PM.


#12 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 November 2013 - 05:54 AM

Or rather, you DO declare it, but you declare it as local to a different function. That is to say, it's the exact same issue as the one I already told you how to fix.

Anyway, think of a numerical variable as being intended for numerical storage, and strings being intended for storing words (or any given sets of characters). This mostly matters when you come to use these values, as Lua will take their types as ques as to how to treat them.

Let's say you ask someone to type in a number and they type "potato". Say you don't bother to check that a number was handed to you, and go on to try and add one to what you were given. What do you think happens when Lua tries to divide the word "potato"? ;)

Lua is actually rather forgiving about the matter, and if you store an actual number as a string, many functions will still "work". You can, for example, store "56" as a string and perform division on it. You cannot, however, store a number as a string and try to pass that to something like rednet.send() for the computer ID (as that will insist on having the ID stored as a number, not as a string).

#13 jay5476

  • Members
  • 289 posts

Posted 26 November 2013 - 03:50 PM

View PostStrite9000, on 25 November 2013 - 06:09 PM, said:

Okay, I think I've got it. I'm gonna try some of these in the code right now, thanks a lot!

And the "tonumber(read())", I see, so, basicly, if a string is giving, when you're asking for a numerical value (using the "tonumber(read())" command), the input it's basicly going to return false, insteed of the string itself. I just don't understand a simple thing, what's the basic diference between getting a number (input) using the "read()" command, and the "tonumber(read())", because, for simple things, It's pretty much the same, I'm guessing.

local number = read()
print ("Number: "..number)

Should work, and:

local number = tonumber(read())
print ("Number: "..number)

Should work as well, I think.
Am I wrong?


-snip
The reason you put it as a number is because you can then do maths on it
number = read()
print(number-1)
that will error even if you type a number because its in its string form
number = tonumber(read())
print(number-1)
will not error if you give a valid number but if you don't it returns nil

#14 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 November 2013 - 04:29 PM

View Postjay5476, on 26 November 2013 - 03:50 PM, said:

that will error even if you type a number because its in its string form
You'd think so, but no.

Still, you'd run into problems with it later down the track with other functions.

#15 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 26 November 2013 - 04:42 PM

View PostVaNnOrus, on 25 November 2013 - 11:51 PM, said:

View PostStrite9000, on 25 November 2013 - 06:09 PM, said:

EDIT: Now I'm also getting the error: battle:302: attempt to perform arithmetic __sub on number and nil, any ideas why?
Here's the updated code: pastebin.com/HTsWZNN6

local BuckleyH = BuckleyH - firstFinalAttack

You are trying to take away some value from non-existent variable
You must declare a variable before operations with it, like that
local a = 2
a = a - 1 -- now a = 1

Hm, I see, I just misspelled "finalFirstAttack" for "firstFinalAttack", ok, but now I that I fixed, it's still giving me the same error! And well, BuckleyH is defined at the very begining, and I made that function return the variable "finalFirstAttack", so it is basicly defined, isn't? From what I understood from Bomb Bloke's explanation, a way to make the script, or another function access other functions information is by adding a "return" plus the name of the variable (information) at the end of the function giving the information, I did that.

That's the new code: pastebin.com/5BeHNasK, it's now that diferent, but, anyways.

And even though I added that return at the end of the function "FirstAttack()", the script still can't understand the line 302, it still gives me the arithmetical error. Oh, and by the way, don't mind the super messy code, I was just trying everything to make this work, and the result was a whole bunch of useless lines that literally don't do nothing , so, don't mind them. I even tried to define BuckleyH again at line 310, but none of that nonsense worked.

EDIT: Sorry Bloke. xD

Edited by Strite9000, 26 November 2013 - 05:03 PM.


#16 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 November 2013 - 05:00 PM

View PostStrite9000, on 26 November 2013 - 04:42 PM, said:

Bomber Blake
:|

When you "return" something, you're not so much passing the whole variable back as you're passing the contents of that variable back.

For example, up the top of your script you call "math.random(1,20)". This function returns a number between one and twenty. You're multiplying that value by 350 and storing the result in the "BuckleyS1Dam" variable.

Or perhaps this'll make more sense to you:

local function multiplyByFifty(valuePassedToMe)
  local multipliedByFifty = valuePassedToMe * 50
  return multipliedByFifty
end

local theResult = multiplyByFifty(3)
print(theResult)


#17 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 26 November 2013 - 05:56 PM

I think I've got it, I just don't understand that: "multiplyByFifty(3)", what that "(3)" is doing?

#18 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 November 2013 - 06:02 PM

It gets passed to the "multiplyByFifty" function, which stores it in a variable called "valuePassedToMe". That variable is automatically localised to that function, so it (along with the "multipliedByFifty" variable) gets discarded when the function ends.

All that gets returned is the contents of "multipliedByFifty", which gets captured and stored in "theResult" (a new variable).

#19 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 26 November 2013 - 07:03 PM

Oh, so it pretty much works like a algebraic function, I see!

Bomb Bloke, you basicly made my day! The code is working! (for now...)

The solution was pretty simple:

First of all, I made this function return "finalFirstAttack".

local function FirstAttack()
print ("Attacker: "..first)
print()
print ("Dice (20): "..d20)
if d10>= 5 then
print ("Dice (10): "..d10)
print ("Critical Hit!")
print ()
local finalFirstAttack = d20p10 * firstATK * crit
print ("Final attack: "..finalFirstAttack)
return finalFirstAttack
else
print ("Dice (10): "..d10)
print ()
local finalFirstAttack = d20p10 * firstATK
print ("Final attack: "..finalFirstAttack)
print ()
return finalFirstAttack
end
end

And at the end, just defined finalFirstAttack as the the result of the FirstAttack function.

local finalFirstAttack = FirstAttack()
local BuckleyH = BuckleyH - finalFirstAttack
print ("Buckley HP: "..BuckleyH)

Thanks Bomb Bloke, and all the others as well, of course.

Oh, and yes, of course, I'll probably continue to use this topic for a couple other difficulties I might have in the future, so, admin, please don't close it. haha'

#20 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 28 November 2013 - 02:21 PM

Heyo, I'm back, I'm getting quite a weird error, it's just a visual one, I can run the program and everything will be fine, but for some reason, when the FirstAttack() function is called by the aos() one, the Dice Results (and/or the Critical Hit message) kinda, blinks once or twice.

Here's the updated code: pastebin.com/mDTgNFsW (the FirstAttack() function starts at line 245)

Here's the function's code:

local function FirstAttack()
Head()
print (first.."'s turn!")
print()
sleep(1)
print ("Dice (20): "..d20)
--Critical Hit
for i = 5,9 do
if d10 == i then
print ("Dice (10): "..d10)
print (" ")
sleep(0.5)
print ("Critical Hit!")
print (" ")
sleep(0.3)
local finalFirstAttack = d20p10 * firstATK * crit
print ("Final attack: "..finalFirstAttack)
return finalFirstAttack
--Super Attack
elseif d10 == 10 then
print ("Dice (10): "..d10)
print (" ")
sleep(0.5)
print ("Super attack!")
print (" ")
sleep(0.3)
local finalFirstAttack = d20p10 * firstATK * 5
print ("Mult. 5x!")
print ("Final attack: "..finalFirstAttack)
return finalFirstAttack
elseif d10<=4 then
print ("Dice (10): "..d10)
print (" ")
sleep(0.3)
local finalFirstAttack = d20p10 * firstATK
print ("Final attack: "..finalFirstAttack)
print ()
return finalFirstAttack
end
end
end

If you're going to test this yourself, just put some random information at the player one's application, and just skip the second and third one's pressing Enter repeatedly until it asks you what monster you're figting, type "Buckley", it will ask what you want to do, put "Attack", you'll get the message that blinks.

Thanks!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users