【问题标题】:How to pass a local variable to another function?如何将局部变量传递给另一个函数?
【发布时间】:2014-01-12 07:14:59
【问题描述】:

所以我在做一个文字冒险游戏。它有数百条故事线,一个功能齐全的战斗系统,我现在正在尝试创建一个影响系统。基本上它应该是这样工作的:某些反应/动作会增加或减少你对不同角色的影响。我想在choice_6() 和story_7() 中使用影响变量。我怎么做?请不要给我任何链接。我已经经历了许多其他答案,但它们对我来说没有意义,所以如果您要复制和粘贴,至少要以与其他答案不同的方式解释它。谢谢。

int choice_6()
    {
        int influence = 0;
        int choice = 0;
        printf("What do you do?\n");
        printf("\t1. You: No. I know absolutely nothing about you. You could be a monster\n");
        printf("\ttoo for all I know! Normal people don't turn into frogs!\n");
        printf("\t2. You: Your right we should keep moving. You can tell me when your\n");
        printf("\tready.\n");
        printf("\t3. You: Okay where do you think should we go then?\n");
        do 
        {
            scanf_s("%i", &choice);
            switch (choice)
            {
                case 1:
                    {
                        printf("\tBrie flashes a pained look on her face and you immediately regret saying that.\n");
                        printf("\tBrie: You really think I'm a monster?\n");
                        system("pause");
                        influence -= 10;
                        printf("You lose influence and are now at %i with Brie.\n", influence);
                        system("pause");
                        printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
                        printf("However, depending on the situation, low influence is not always a bad thing...\n");
                        system("pause");
                        printf("\tYou: Your right we should keep moving.\n");
                        break;
                    }
                case 2:
                    {
                        printf("\tBrie: Thank you. I'd rather not discuss this my life story in a dark\n");
                        printf("\tdungeon.\n");
                        system("pause");
                        influence += 10;
                        printf("You gain influence and are now at %i influence with Brie.\n", influence);
                        system("pause");
                        printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
                        printf("However, depending on the situation, low influence is not always a bad thing...\n");
                        system("pause");
                        printf("\tYou: I'd have to agree with you there. Let's see what is up these stairs.\n");
                        choice = 2;
                        break;
                    }
                case 3:
                    {
                        printf("Brie smiles at this.\n");
                        printf("\tBrie: Well the only way out seems to be these stairs so let's go up.\n");
                        system("pause");
                        influence += 10;
                        printf("You gain influence and are now at %i influence with Brie.\n", influence);
                        system("pause");
                        printf("Influence affects characters reactions towards you, their effectiveness in combat, and even their allegiances in rare cases.\n");
                        printf("However, depending on the situation, low influence is not always a bad thing...\n");
                        system("pause");
                        printf("\tYou: Sounds good to me I am quite frankly done with dungeons about now.\n");
                        break;
                    }
                default:
                    {
                        printf("Type the number for the choice you want to do\n");
                        system("pause");
                        break;
                    }
            }
        }while(choice != 1 && choice != 2 && choice != 3);
    }

    int story_7()
    {
        printf("\ninfluence is %i\n", influence);
        printf("You lead the way walking upstairs\n");
        system("pause");
        printf("You turn the corner while crouched to find a room full of gremlins and goblins.\n");
        system("pause");
        printf("You grab Brie's hand and roll to the left behind some crates before they see you.\n");
        system("pause");
        printf("Even though you realize you will probably not make it out of this situation\n");
        printf("alive, you can't help but feel lucky with Brie being so tightly pressed against you on the floor.\n");
        system("pause");
        printf("After a long period of silence to confirm nobody has seen you both enter,\n");
        printf("Brie looks up at you and rolls her eyes.\n");
        system("pause");
        printf("*whispers*\tBrie: At least buy me dinner first jeez.\n");
        system("pause");
        printf("*whispers*\tYou: I'd love to but we should probably kill these uglies first.\n");
        system("pause");
        if (influence > 0)
        {
            printf("Brie laughs at this and slowly leans you off her to have both of you crouch.");
            system("pause");
        }
        else
        {
            printf("Brie: Ugh just get off of me!\n");
            printf("Brie pushes you off her violently and you manage to stay crouched.");
            system("pause");
        }
    }



    int main()
{
    int play = 0;
    int influence = 0;
    intro_1();
    story_1();
    choice_1();
    story_2();
    choice_2();
    story_3();
    choice_3();
    story_4();
    choice_4();
    story_5();
    choice_5();
    intro_2();
    combat_1();
    story_6();
    choice_6();
    story_7();
    choice_7();
    system("pause");
}

【问题讨论】:

  • 您认为您的scanf_s("%i", &choice) 调用如何修改choice_6() 的本地变量?
  • @jamesdlin 我不这么认为。整数“选择”是我用来让玩家选择他们想要做的情况的变量。
  • @user2073308:呃,如果你不认为你的scanf_s 调用修改了choice 的值,那么你认为它到底是如何工作的?
  • @jamesdlin 好吧,也许我不清楚。 “choice6()”是你在游戏中做出的第六个选择的函数名。在该函数中,scanf_s 修改“choice”的值,并使用一个开关根据“choice”的值确定要使用的大小写。

标签: c function variables


【解决方案1】:

如果您想从函数中修改它,可以将 指针 传递给 influence 变量。像这样:

int choice_6(int *influence)
{
    // ...
    *influence -= 10;
    // use *influence wherever you would have had used influence before
    // ...
}

int story_7(int influence)
{
    // ...
    if (influence > 0) ...
    // ...
}

int main()
{
    int influence = 0;
    // ...
    choice_6(&influence);
    story_7(influence);
    // ...
}

这是将 指针 传递给 choice_6(),因为您需要在该函数中修改 influence。这会将 传递给 story_7(),因为您不需要在那里修改 influence 值。

【讨论】:

  • 当我和我的编程老师谈论我正在制作的游戏时,他说的一件事是:“指针和 Go-To 是糟糕的编程。”那么您是否可以提出不使用这两个概念的其他解决方案?
  • @user2073308: 如果你的老师这么说关于指针,那么他/她真的不应该接近 C。虽然指针对于初学者来说有它的陷阱,但如果没有指针,你将不会在 C 中走得很远指针。在任何重要的 C 程序中,谨慎使用指针都是必不可少的。如果要避免使用指针,请不要使用 C;还有其他语言根本没有指针(例如 Python、Java),或者指针对于良好的编程通常不是必需的(例如 C++)。 C 不是这样的语言。
  • @LieRyan 我不知道为什么我说指针我的意思是全局变量我很抱歉并且你正确地指出指针对于 C 来说是必需的。我只是很沮丧我无法让它工作。无论如何,您的解决方案确实有效,非常感谢您!!!! :DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
【解决方案2】:

您应该创建存储游戏状态的游戏对象,在这种情况下,是影响力和其他事件标志。

然后您需要将此游戏对象设置为全局对象,或者通过指针/引用将其传递给可能需要检查或修改游戏状态的每个函数。

struct MyGame {
    int influence;
    int has_cheese;
};

void choice_6(struct MyGame * game) {
    ...
    game->influence += 10;
    ...
}
int main(...) {
    struct MyGame game;
    game->influence = 0;
    game->has_cheese = FALSE; // or 0
    ...
    choice_6(&game);
    ....
}

【讨论】:

  • FALSE 未定义,当我执行choice_6(&game);和 story_7(&game);
  • @user2073308:这只是一个示例代码,您应该修改它以适应您的目的,而不是盲目复制。如果没有#define-ed,则可以使用 0 代替 FALSE。其次,您可以在搜索引擎(例如 Google)上输入您收到的错误消息(即重新定义形参),错误的原因应该很明显。
【解决方案3】:

您需要通过引用传递influence 变量。这意味着您提供引用的函数可以更改变量本身的值(而不仅仅是变量的副本)。

通过在函数签名中在参数前面加上 * 并在前面加上 & 进行传递:

int choice_6(int *influence)
    {
....

    int main()
{
    int play = 0;
    int influence = 0;
    choice_6(&influence);
....

【讨论】:

  • 我尝试了这个解决方案,但是当我尝试在 story_7 中调用影响力时,我不断收到错误
  • 每个要修改变量influence的函数都需要将变量的引用(指针)传递给它,所以就像你需要将函数int choice_6()更改为int choice_6(int *influence)一样,你还必须将int story_7() 更改为int story_7(int *influence)
猜你喜欢
  • 2017-03-26
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多