Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Daft text adventure project

by jeroenes (Priest)
on May 29, 2001 at 16:53 UTC ( #83908=note: print w/replies, xml ) Need Help??

in reply to Daft text adventure project

I like this kind of ambitions very much :-).

For the 'back' feature: You need a whole new interface for that, whether you implement it in object-oriented perl or just in function-perl.

You'll have to rewrite every instance of changing a variable to use code that remembers that chance. So instead of saying $foo++ you would say something like increment( $foo, $undo_stack). Because of inheritance, this is somewhat easier to mix in some OOP code than into non-OOP code, but your that really depends on how you have written/will write the code.

Anyhow, you have to put into an extra layer between your normal code and the change of variables.

I would use a undo-stack of a certain size (depending on the amount of resources you want to use). You can only undo the action that has been pushed onto the stack the last. When the stack exceeds its predefined size, you 'freeze' the first action of that stack into the vars.

Many GUI-OO libs have a predefined undo-class. However, you need to write your own methods/ childs to define undo-actions for your own code...

Hope this helps,

"We are not alone"(FZ)

Replies are listed 'Best First'.
Re: Re: Daft text adventure project
by Tiefling (Monk) on May 29, 2001 at 17:14 UTC
    Actually, since (as is typical in this family of software) I'm writing a different kind of data at each step, it's not as difficult as you might think.

    Here's a little pseudo-code to illustrate:

    initialise_stuff; set_abilities; set_race; set_class; set_skills;

    That's the way (roughly) that it looks now. In my bad old BASIC days, I'd simply have had a GOTO triggered on the return value of each procedure to send the program back a chunk if the user asked to go back. Since you're given the chance to review any decision you make at the time you make it, this should allow procedures called to offer up the previously made choice for revision (and if it doesn't, I'll re-write the procedure so that it does) - I just need a clean way to drop back a stage on the appropriate return value.

      My mind is not in general compatibility mode, apparently.

      Does this mean that you only allow the user to correct his input? Otherwise, the state of your program has changed since that step, because variables/objects have been altered. So you need to revoke these alterations.

      Checking for return values is really simple in perl:

      #This example is wearn out to the bone due to it's general use open FILE, 'data.txt' or die "Could not open data.txt: $!"; #in general, if function() returns either false or some true info: MAIN: { initialise_stuff; ask_user(); function( @_ ) or redo MAIN; .... }
        Ok. The code you provide is roughly how my creation program looks at the moment. (I don't want initialise redone anyway - some of that is going to be replaced by a function call, and the rest will doubtless be exported to a module, but for the interim, it just gets called once and does the dirty work.) However, what the desired state of affairs is, is that there are several functions, function1, function2, etc., which each set a different bunch of data (so that function1 sets %hash1, @array1 and $string1, for example), and the completed character is the union of all those sets, recast by a final function into a format the adventure will want to read. Each function is set up so that the player can choose a result (an arrangement of ability scores or a character race, in the currently operative functions), see what it looks like, and then accept it and proceed or reject it and go back to the top of the procedure. However, if a player, having chosen his ability scores, sees the races and wants to go back and re-arrange or re-roll his/her scores, we need to be able to call the ability-rolling procedure again, preferably preserving the old stats so that s/he can rearrange them, rather than utterly clobbering them as a re-start of the main program would do. Similarly, if, having seen the classes, the player wants a different race, we need to offer up the race procedure again.

        I get the feeling I'm not articulating this very well. Ah, well.

        Thanks again - Tiefling.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://83908]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2020-12-05 05:27 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (63 votes). Check out past polls.