简体   繁体   中英

How would I loop this

I've modded in custom 'origins' (character classes) to a game and they do show up in-game. Then I tried to add functionality to them. When you click on a origin, you spawn in as a certain character. For example, if i choose a origin "Hank J Wimbleton" in the character creator menu, then i spawn as hank in-game. It works when it isn't in a loop, but copy and pasting the code would take too long, and make the file size bigger because theres a lot of characters in the game. I've tried to loop it but the code returns either the first character of the list or the last one. (crackpot is first and swamp zed is last).

List 'characters' contains all the characters in the game. array named 'array' contains a character named 'arena'

Expected results: the code should change "arena" to the character you choose when choosing origin.

my explaining skills are quite bad but i got a screenshot that i hope will clear up some questions:

在此处输入图片说明

Choosing The Sheriff should spawn you in as The Sheriff The character names on the origins on the left side of the screenshots repeat because theres different versions of one character, with different armor and size. The have been named like that by the devs.

What code i tried:

   array[0].myOrigin = this.selectedOrigin.myOrigin;
            for (int j = 1; j < list.Count; j++)
            {
                if (array[0].myOrigin.FullName == list[j].FullName)
                {
                    array[0] = Char_Data.CreateCharacter(characters[j]);
                }
            } 

FullName is a string.

myOrigin is the origin that you click on.

The code works when its not in a loop:

if (array[0].myOrigin.FullName == list[5].FullName)
                    {
                        array[0] = Char_Data.CreateCharacter(characters[5]);
                    }

and spawns you in as the character you chose.

The problem with the loop is that it overwrites the value in array[0] for every match which is why you end up with the last one when using the loop. If you want it to stop after the first match use the keyword break; inside the if statement.

array[0].myOrigin = this.selectedOrigin.myOrigin;

for (int j = 1; j < list.Count; j++)
{
    if (array[0].myOrigin.FullName == list[j].FullName)
    {
        array[0] = Char_Data.CreateCharacter(characters[j]);
        break;
    }
} 

This will assign the first matching item to array[0] and then exit the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM