Thread: text rpg problem

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Create an instance of the structure and update the values.
    Sent from my iPadŽ

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    heres what i came up with, still not working

    the structure
    Code:
    //structure to return values
    struct att fn();
    {
           int hpr;
           int mpr;
           int expr;
    };
    then the attack function

    Code:
    //attack function
    int attackm(int hp,int mp,int atck,int def,int exp,int ehp,int emp,int eatck,int edef,int eexp){
        int amenu;
        int cehp=ehp;
        int chp=hp;
        int cmp=mp;
        int cemp=emp;
        //attack menu
    while(cehp>0&&chp>0){
        system("CLS");
        printf("1.Attack\n");
        printf("2.Magic\n");
        printf("3.Items\n");
        printf("4.Run (roll to run)\n");
        printf("\n You              Enemy"); 
        printf("\n%d/%d HP        %d/%d HP",chp,hp,cehp,ehp);
        printf("\n%d/%d MP          %d/%d MP\n",cmp,mp,cemp,emp);
        scanf("%d",&amenu);
    
    
        
    
        
        switch(amenu){
                      case 1:
                           system("CLS");
                           cehp=cehp-atck;
                           if(cehp<=0){
                                         ehp=0;
                                         }
                           printf("You do %d damage, enemy has %d HP left\n",atck,cehp);
                           chp=chp-eatck;
                           printf("You take %d damage, you have %d HP left\n",eatck,chp);
                           cont();
                           break;
                           }
                           
                           if(ehp<=0){
                                         printf("You win!\n");
                                         printf("You gain %d EXP\n!",eexp);
                                         exp=exp+eexp;
                                         cont();
                                         }
    
    }
     struct att returns;
     returns.hpr=chp;
     returns.mpr=cmp;
     returns.expr=exp;
         return (returns);
    }

  3. #18
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    I still havn't figured it out

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    //structure to return values
    Code:
    struct att fn();
    {
           int hpr;
           int mpr;
           int expr;
    };
    This is trying to be too much. You need to first define the structure, and then make a function that returns one. Like so:
    Code:
    struct foo
    {
        int bar;
        int baz;
    };
    We have now define what our structure will look like. Now just like making any other function return one type, we make a function that returns this one type. The type of this structure is 'struct foo'. This gives us:
    Code:
    struct foo myfunction( void )
    {
        struct foo x; /* declare a 'struct foo' instance called x. */
        /*...do stuff...*/
        return x;
    }
    Now a call to this function might look something like:
    Code:
    struct foo abc;
    
    abc = myfunction( );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by xxwerdxx
    thanks, that clears up a lot about structures, but i dont know how i would use that to return the values of the attackmenu

    If you want to set the values of the integers that you are passing into your attack routine, you have to pass the address of the integer.

    As your program is written now, you are passing by value:

    Code:
    attack(int foo, int bar)
    The value of foo and bar will be placed on the stack and available to your function, but they just that -- values. Changing them in your attack routine will have no effect on the value back in your calling routine.

    Doing something like this:
    Code:
    attack(int foo, int bar) {
       int *pfoo = &foo;
       int *pbar = &bar;
    ...
    }
    won't work either, because you still don't have the address of the ints in your calling routine.

    You have to pass by reference (at least that's what they used to call it when I started programming):

    Code:
    attack(int *foo, int *bar) {
       *foo = whatever value you're setting for foo 
       *bar = whatever value you're setting for bar 
    ...
    }
    Instead of calling attack like this:
    Code:
    int foo = 0;
    int bar = 100;
    
    attack(foo, bar);
    You'd call it like this:
    Code:
    int foo = 0;
    int bar = 100;
    
    attack(&foo, &bar);
    You can apply the same technique to structures. Think of the structure as a container that will nicely hold all of your values. That way, you pass your attack routine a pointer to the structure, manipulate the values inside the structure, and return.

    And finally, as someone said early, you probably want to take a more object oriented approach -- that way you can encapsulate methods (i.e. "routines") along with the data in an object. The objects will represent various entities in your game.

    Hope this has helped somewhat....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with text cursor in C
    By sureshkumarct in forum C Programming
    Replies: 3
    Last Post: 12-22-2006, 12:17 AM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. Text Rpg Of Courseee
    By Paninaro in forum Game Programming
    Replies: 6
    Last Post: 06-24-2002, 01:54 PM
  5. my text based RPG!
    By Leeman_s in forum Game Programming
    Replies: 11
    Last Post: 05-13-2002, 08:24 AM