1

I'm making a thing to help me keep track of my players for d&d and i hit a roadblock. relevant piece of code:

playercount = nil
playername = nil

playernamegroup = Array.new
playeracgroup = Array.new
playermaxhpgroup = Array.new
playercurrenthpgroup = Array.new


def party(n)
  return if n == 0
  party(n-1)

  player = {}
  puts "-What's player #{n}'s name?"
  playername = gets.chomp 
  playernamegroup <<  playername    
  puts "-What's their AC?"
  playerac = gets.chomp.to_i
  puts "-Got it. What's their max HP?"
  playermaxhp = gets.chomp.to_i
  $players[n] = player
  puts "-Okay."
end

there's a loop further down that runs the party block the number of times given, here:

loop do
  puts "-How many players today?"  
  playercount = gets.chomp.to_i
    if 0 >= playercount
      puts "-You can't have no players in a party. That's not D&D, that's you having no friends."
      redo
   elsif 8 < playercount
     puts "-Hey now, that's a huge party. I can only handle eight players at once."
     redo
    elsif 8 >= playercount
      break     
   else 
     puts "-A number between 1 and 8, please."
     redo
   end
end

$players = [] 

party(playercount)

when i run it i get: Error: undefined method `playernamegroup' for main:Object

i've tried shuffling things around and still get the same error, and i've looked through other asks and haven't figured it out. help?

2
  • What is the code supposed to do? Commented Jun 15, 2017 at 6:26
  • Add player stats to arrays Commented Jun 15, 2017 at 17:54

1 Answer 1

2

You need to declare the arrays inside your function

def party(n)
  playernamegroup = Array.new
  playeracgroup = Array.new
  playermaxhpgroup = Array.new
  playercurrenthpgroup = Array.new

  return if n == 0
  party(n-1)

  player = {}
  puts "-What's player #{n}'s name?"
  playername = gets.chomp 
  playernamegroup <<  playername    
  puts "-What's their AC?"
  playerac = gets.chomp.to_i
  puts "-Got it. What's their max HP?"
  playermaxhp = gets.chomp.to_i
  $players[n] = player
  puts "-Okay."
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.