28 posts
Posted 07 June 2012 - 05:26 PM
Note: I'm not sure how pastebin works, as my CC didn't come with it in the programs section, if you can help with that, it'd be appreciated.
This is just a segment of my code, but it's where the error is. The while function isn't even done, but if I can't get it to work in this form, I don't want to complicate it more.
while health > 0 do
action()
enemyturn()
Basically I'm making a game where if your health is above 0, you keep playing. When I run it, this error comes up:
charos:46: attempt to call string.
2447 posts
Posted 07 June 2012 - 05:30 PM
We need to see more than that code - that code is where the error occurs but not what is wrong with the code. It looks like you are setting the value of one of the functions to a string somewhere.
28 posts
Posted 07 June 2012 - 05:32 PM
Alright, so if pastebin didn't come with my installation, how would I go about using it so I can show you the whole code?
286 posts
Location
Bonn Germany
Posted 07 June 2012 - 05:39 PM
If help is a number and there is closing end for the wile-loop and those
action()
enemyturn()
are valid functions I see no problem with these lines.
PS: pastebin is a free online past tool http://pastebin.com/ no CC program.Please post a bit more of your code. And tell us in which line the error occurs.
286 posts
Location
Bonn Germany
Posted 07 June 2012 - 05:41 PM
Alright, so if pastebin didn't come with my installation, how would I go about using it so I can show you the whole code?
If your code is not on a server navigate to your .minecraft folder and then look in /saves/YOUR_WORLD_NAME/computers/THE_ID_OF_THE COMPUTER/ for the file with your code.
28 posts
Posted 07 June 2012 - 05:43 PM
Alright, so how would I put my code on Pastebin, as far as I know you can't copy the code?
28 posts
Posted 07 June 2012 - 05:44 PM
health = 20
magic = 15
ehealth = 30
print("The point of the game is to kill the evil king Charos. You can do that by attacking, healing, or restoring magic.")
print("The commands are 'attack' 'heal' and 'magic'. Additionally, you can use 'suicide' to automatically end the game.")
print("You die if you run out of health or magic. You heal at the expense of magic.")
function action()
io.write("What would you like to do?")
action = io.read()
if action == "attack" then
attack = math.random(1,5)
ehealth = ehealth - attack
print("You attacked for " .. attack .. " damage! Now Charos has " .. ehealth .. " health left!")
elseif action == "magic" then
restore = math.random(1,7)
magic = restore + magic
print("You restored " .. restore .. " magic for a total of " .. magic .. " magic!")
elseif action == "heal" then
hrestore = math.random(1,5)
magic = magic - hrestore
health = health + hrestore
print("You used " .. hrestore .. " magic to restore " .. hrestore .. " health for a total health of " .. health .. " health! and " .. magic .. " magic!")
elseif action == "suicide" then
health = 0
end
end
enemyturn = function()
eturn = math.random(1,2)
if eturn == 1 then
eattack = math.random(1,5)
health = health - eattack
print("Charos attacks for " .. eattack .. " damage. Now you have " .. health .. " health left!")
elseif eturn == 2 then
eheal = math.random(1,5)
ehealth = ehealth + eheal
print("Charos healed for " .. eheal .. " health, now he has " .. ehealth .. " health!")
end
end
while health > 0 do
action()
enemyturn()
end
286 posts
Location
Bonn Germany
Posted 07 June 2012 - 05:44 PM
Ok your error is in this line
enemyturn = function()
If you want to create a function make it like you did it before
function enemyturn()
[...]
end
Oh no! I was wrong. But using two ways of function definition in weird.
HINT: Please use the Code tags if you post code in the CC forum. (CODE) and (/CODE) (with square brackets instead of normal ones)
28 posts
Posted 07 June 2012 - 05:45 PM
Also, I plan to change it so when Charos dies, the game ends, but the code isn't done.
2447 posts
Posted 07 June 2012 - 05:49 PM
You're setting action to a string, then trying to call that. Rectify that.
28 posts
Posted 07 June 2012 - 06:03 PM
Where exactly am I doing that? What line is wrong?
1604 posts
Posted 07 June 2012 - 06:12 PM
Here:
function action() -- define "action" as a function
io.write("What would you like to do?")
action = io.read() -- set action to a string
You overwrite the variable containing the function (yes, functions are also variables) with a string.