10 Replies - 1349 Views - Last Post: 06 March 2011 - 06:46 PM Rate Topic: -----

#1 rykin666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-January 10

Command to travel to a "paragraph"?

Posted 02 February 2011 - 10:04 PM

hopefully the title makes sense, if it does, im simply wondering what the command is, and what i need as a marker to jump to there.

for example, I have a menu with about 4 different options to "go to" another menu. i want to be able to jump to each menu regardless of where it is in the code.
...
best way to for me to explain this is to show an example and hope someone recognizes something like it.

MainPage
puts '1. Inventory'
puts '2. Stats'
puts '3. Map'
variable1 = gets.chomp
if variable1 = '1'
    goto InvPage
else
    if variable1 = '2'
        goto StatPage
    else
        if variable1 = '3'
            goto MapPage
        else
            puts "Please make a different selection.'
            #### ALSO I NEED TO KNOW WHAT GOES (presumably) HERE IN CASE OF THIS PROBLEM. PROBLEM BEING ENTERING AN INVALID CHOICE.
        end
    end
end



The key things to note here being "goto" being a command i made up telling the program to go to the chosen location and continuing the script. Inv/Stat/MapPage being the marker to "goto"

also, as mentioned above in the code, i have a similar situation causing me grief right now
a1 = ' '
puts 'Now then, how much would you like to start with?'
puts '1. $50,000'
puts '2. $80,000'
puts '3. $120,000'
puts '4. $200,000'
a1 = gets.chomp
if a1 == '1'
	cash = 50000
else
	if a1 == '2'
		cash = 80000
	else
		if a1 == '3'
			cash = 120000
		else
			if a1 == '4'
				cash = 200000
			else
				puts 'Sorry, could you please re enter your decision?'
			end
		end
	end
end



right now, if i type anything other than 1,2,3, or 4 it says "Sorry, could you please re enter your decision?" and continues the script normally, causing you to start the game broke. Which is a big issue.

i believe if i can get this "goto" thing to work, i could simply put a "page" or "paragraph" number/name above each paragraph, and put "goto page##" underneath "puts 'Sorry, could you please re enter your decision?'"



lastly, im very sorry if I'm spamming this forum, not my intentions, if it would be prefered i have no problem opening a single thread and continually post my questions in there. all i really have is you guys for support, or other forums, i have no one around me who knows any sort of coding, and i find it very hard to sit down and read a guide and remember anything i just read, i need to do it hands on and take my own notes in order to retain any knowledge.

Is This A Good Question/Topic? 0
  • +

Replies To: Command to travel to a "paragraph"?

#2 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 12:06 AM

It sounds like what you want is a method. Methods encapsulate chunks of code and separate them from the rest of the script, then methods can be called from other parts of the code, and the code in a method won't be run until it's called. They can take a number of arguments, and will return a single value (which may be nul). Here's an example:

def add(number1, number2)
    number1 + number2 #This will "fall off" and get returned to whatever called the method
end

def map_page!()
    do stuff for the map page
end



Then, you can acces your method by calling it's name and passing what values you want as arguments, separated by commas.

sum = add 1, 2 #sum now equals 3

#Or of you don't need to use the value that get's returned, just call it.
map_page



That's not nearly a complete explanation, but hopefully it can get you on the right track. Lot's of early languages included a goto command kind of like you described, but it has been mostly abandoned because separating different parts of code into methods is way easier to keep track of.

As far as spamming the forum goes, we like to keep it one thread per issue, and since you're following the rules and not just begging for "teh codez" I think you should be fine. ;)
Was This Post Helpful? 0
  • +
  • -

#3 rykin666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-January 10

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 04:32 PM

I'm sorry, still quite new at this. I understand the theory. "This chunk i will name chunk 1 so i can bring it back later in the program"
But im completely lost looking at the code, and the examples.
The usage of the word "Method" is also throwing me off. In my tutorial he compares Methods to verbs.
What I'm looking for, is what is the Method i need lol.
the "goto" command for ruby.
Was This Post Helpful? 0
  • +
  • -

#4 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 04:42 PM

So... no goto command for ruby. What you're doing when you create a method is that you're creating your own commands. The "go to this section and execute this code" is implied when you call the name of the method.

Here's a really rough example from your situation:

def get_starting_cash()
  a1 = ' '
  puts 'Now then, how much would you like to start with?'
  puts '1. $50,000'
  puts '2. $80,000'
  puts '3. $120,000'
  puts '4. $200,000'
  a1 = gets.chomp
  if a1 == '1'
    return 50000
  else
    if a1 == '2'
      return 80000
    else
      if a1 == '3'
        return 120000
      else
        if a1 == '4'
          return 200000
        else
          puts 'Sorry, could you please re enter your decision?'
          return get_starting_cash
        end
      end
    end
  end
end

cash = get_starting_cash
do other stuff...



See, there is a chunk of code that asks the user how much cash he wants and returns the number. If the user doesn't give an acceptable answer the code asks again and returns whatever the new answer is.

This post has been edited by 5thWall: 03 February 2011 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 rykin666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-January 10

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 05:07 PM

oookay, i get it now. you had worded it right, it just didnt sound right to me.
what ive got right now is:
def howmuch
a1 = ' '
puts 'Now then, how much would you like to start with?'
puts '1. $50,000'
puts '2. $80,000'
puts '3. $120,000'
puts '4. $200,000'
a1 = gets.chomp
if a1 == '1'
	cash = 50000
else
	if a1 == '2'
		cash = 80000
	else
		if a1 == '3'
			cash = 120000
		else
			if a1 == '4'
				cash = 200000
			else
				puts 'Sorry, could you please re enter your decision?'
				howmuch
			end
		end
	end
end
end
howmuch
puts 'Alright. You now have $'+cash.to_s+' in your bank!'




and what im getting is:

Now then, how much would you like to start with?
1. $50,000
2. $80,000
3. $120,000
4. $200,000
1
Alright. You now have $0 in your bank!
logout

[Process completed]

The part that tells you how much you have worked fine until i threw in the def end.
Is because i cant change variables from inside the method?
Was This Post Helpful? 0
  • +
  • -

#6 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 05:33 PM

It's not that you can't change variables, it's that you have to be careful which variables you're changing. What you're running up against here is called scoping, basically a variable is defined in certain areas of your program, what's called the variables "scope". Right now the variables that you're using are called local variables, and when they're created inside a method they only exist inside of that method (and a new one is created each time the method is called). Local variables all start with a lower case letter. Another type of variable is called an instance variable, and it starts with the '@' symbol. Instance variables scopes are limited to instances of objects, and you'll learn more about them when you get to objects. For right now you can know that when you use a variable that starts with a '@' it will be available inside and outside of a method.

this = 1
@that = 1

def do_stuff()
  this = 2
  @that = 2
end

do_stuff
puts this #prints '1'
puts @that #prints '2'



For the way your program is structured right now, It's considered a good practice to use local variables as much as you can. Like methods, scoping is used to help control the complexity of your program because it's easier to keep track of chunks of local variables than large numbers of variables that can get changed anywhere in your program. Since you're still a beginner, I wouldn't sweat it too much, but keep it in mind because it'll become more important as your programs become more complex.

This post has been edited by 5thWall: 03 February 2011 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 rykin666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-January 10

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 07:16 PM

okay, so adding @ in front basically makes it a universal variable. cool

last question for ya, for now anyways, in the code you gave me in your second response, could you try to explain to me how the script still ran the "questionnaire" when you had Return's in it, while mine without i need to tell it to execute that method?
and the cash = get_starting_cash line
does the result of that answer turn "def get_starting_cash" into say, variable1 = (the integer)50000.
Was This Post Helpful? 0
  • +
  • -

#8 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 08:05 PM

Oh, I forgot to explain the return, right.

So, all methods in ruby return a value, either explicitly by using the return keyword, or the value of the very last line sort of falls off the end. If you use the return command, the method stops right there, and nothing else is executed. When a value is returned, you can think of it as replacing the method call with the returned value. If you don't use or save the returned value then it just goes away.
Was This Post Helpful? 0
  • +
  • -

#9 rykin666  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-January 10

Re: Command to travel to a "paragraph"?

Posted 04 February 2011 - 11:48 PM

interesting, i really like the way that works
ill have to mess around with it to see how it function, but it sounds very useful.

thanks again for your help. i think ive more or less bashed out all the questions i have on this topic... for now lol.

~L
Was This Post Helpful? 0
  • +
  • -

#10 DillonSalsman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 144
  • Joined: 30-October 07

Re: Command to travel to a "paragraph"?

Posted 06 March 2011 - 06:23 PM

I agree with what was stated about putting the chunks of code into methods.
Furthermore, keep asking the player for input until you get a result you want.
For example:
def init_player
  get_player_name until @player_name != ""
  get_player_sex until @player_sex != ""
  get_player_class until @player_class != nil
  get_starting_money until @player_coins != 0
end


until is one of my favorite keywords in Ruby.

Or use a while TRUE loop
def do_something
while TRUE # can't remember if a do goes here
MainPage
puts '1. Inventory'
puts '2. Stats'
puts '3. Map'
variable1 = gets.chomp
if variable1 = '1'
    goto InvPage
else
    if variable1 = '2'
        goto StatPage
    else
        if variable1 = '3'
            goto MapPage
        else
            puts "Please make a different selection.'
            #### ALSO I NEED TO KNOW WHAT GOES (presumably) HERE IN CASE OF THIS PROBLEM. PROBLEM BEING ENTERING AN INVALID CHOICE.
        end
    end
end
end # this end is for the while loop, didn't feel like inserting spaces..



PS: I have not tested any of this code, so don't expect to be able to copy and paste ;)
Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Command to travel to a "paragraph"?

Posted 06 March 2011 - 06:46 PM

Don't use goto statements, that's bad practice and you'll get attacked by a velociraptor.

Posted Image

Do something like:

begin
  selected_option = gets.chomp
  if selected_option == "1"
    puts "Welcome to the Welcome Screen!"
  elsif selected_option == "2"
    puts "This is the options menu."
  elsif selected_option == "3"
    puts "Logging out. Goodbye!"
  else
    puts "Please select a valid option."
  end  
end while not selected_option == "3"



If you don't want to use strings as conditionals, but plain integers, the .chomp.to_i method will capture the input as a number.

gets.chomp.to_i

So even better, use a switch statement:

begin
  selected_option = gets.chomp.to_i
  case selected_option
    when 1
      puts "Welcome to the Welcome Screen!"
    when 2
      puts "This is the options menu."
    when 3
      puts "Logging out. Goodbye!"
    else
      puts "Please select a valid option."
  end  
end while not selected_option == 3


This post has been edited by Sergio Tapia: 06 March 2011 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1