Reputation: 276
I'm working on a text-based RPG currently. It is very simple, but I'm still having some issues, as I'm fairly new to Python. I want the player to be able to call a certain function, then after they are done, to return to the function they were on before.
def inventory():
invent = {'a': 1, 'b':2}
print invent
def room_function():
input = raw_input("Type [i] for inventory!").lower()
if input == 'i':
inventory()
As shown above, I want the player to be able to call their inventory from any given function they are in. After they call the function, I want the player to be able to return to the function they were in. This example of code is very, very simplified. Here is a more elaborate example of my code:
inventory = {'Backpack': ['Water Bottle', 'Apple', 'Book'],
'Clothes': ["Business Casual Attire"],
'Weapon': ['None']
}
def equip():
print inventory
equip_items ={'Clothes': "Business Casual Attire",
'Weapon': 'None'}
while equip:
print ' '
equip_load = raw_input("Type the item name to equip.: ").lower()
if equip_load == 'hipster attire':
if 'Hipster Attire' in inventory['Clothes']:
print "Hipster Attire has been equiped!"
print "UPDATED EQUIPPED ITEMS"
print ' '
for x in equip_items:
if x in equip_items:
inventory['Clothes'] = 'Hipster Attire'
print equip_items
continue
else:
print "You do not have this item! "
print "Your inventory list is: "
equip()
def room():
#Player does stuff here; allows them to access inventory equip new items.
So my idea is to combine the equip items mechanism with the inventory system. To do this, I need the player to be able to call their inventory function, and then from there, exit that function and return to whatever function they were in. Am I setting this up correctly? Or am I really off the ball here, so to speak? Am I understanding these concepts correctly? Thanks for the help.
Upvotes: 2
Views: 9642
Reputation: 9010
As DaneSoul noted, this is the default behavior. Here's an example for completeness.
def function1():
print 'in function 1'
def function2():
print 'in function 2'
function1()
print 'back in function 2'
function2()
# Output:
# in function 2
# in function 1
# back in function 2
Upvotes: 2
Reputation: 113
I'd recommend you use a 'controller' function to act as a bus, calling functions based on received input. Then after the controller function runs, use a return
statement, without returning anything. This will then return the program back to the function that called the controller.
Upvotes: 1
Reputation: 6633
I suggest writing a function for all your global commands and calling it instead of the raw_input function. For example:
def get_command(player_data):
while True:
command = raw_input('What would you like to do?')
if command == 'equip':
equip(player_data)
elif command == 'inventory':
inventory(player_data)
...
else
return command
Then you can call it instead of raw_input in your room functions. If the player gives a command that is handled by the get_command function directly it will perform the action, then repeat until they give something that get_command doesn't know about. In that case the room will get the command and can handle it appropriately.
def room(player_data):
print "You're in a room"
command = get_command(player_data)
if command == 'leave':
print "Ok you're gone."
...
Upvotes: 0
Reputation: 4511
When you call function, after function have finished execution it return result in the same place where it was called. So, I don't see a problem at all - the idea you ask is default behavior already. What problems have you met with realization?
And for RPG mechanics, OOP would be really great.
If you are new to Python, I'd strongly recommend you try these Python courses at Coursera:
Upvotes: 2