简体   繁体   中英

Make something static within a case (C#)

I'm trying to do a, erm... mess.

I need to make a multiplier multiply by something besides a plain number. I did an Enums at first, but it said it couldn't convert that, so I tried... this:

case 225: {//Flying Press -- INCOMPLETE, needs Flying-type
        setup.Multiplier *= Server.Moves.Move.Element.Flying;
     }
     break;

However, I get the error that an object reference is needed for Server.Moves.Move.Element.get . Apparently you do this by making it static. However, I am unable to go right to there and make it static. I am a programmer for a game, but I only have access to an extremely small portion of the game. It's also a very rustic editor-- Imagine Visual Basic with all its features but error log and the save/open buttons removed.

What I'm trying to accomplish here is make case 225, an attack, count in the Flying element when it inflicts damage. I could do a work-around that sets different elements based on the enemy, but I'd like to avoid doing that. It's very... Just, all around unpleasant and not truly what I want to accomplish.

So uh, is there some way I can set this to be static? Or, well, give it an object reference? I am incredibly new to C#, so even the basics are difficult for me >>

The solution isn't to make the field static because (in all likelihood) the rest of the server code depends on it being a non-static member. Non-static members require an object reference - the object has to be instantiated and stored somewhere beforehand, you can't just attempt to access it using the class name.

You need to find a reference to a Server.Moves.Move object in order to be able to access its members. There could be any number of such objects out there at runtime. Without knowing the API of this server you're using, nobody can really guess where to look for one (and the right one).

You can't access non-static members from a static method, and you cant make non static members static. I only have this small snippet of your code, but why cant you just make setup.Multiplier static. Or make a static method multiplier and pass in the value that needs to be added up.

public static void multiplier(int value){
    setup.Multiplier *= value ; 
}

Found a work-around. After seeing everyone say I can't make it static, I investigated other methods to change the damage.

case 225: {//flying press
        int fly = DamageCalculator.CalculateTypeMatchup(Enums.PokemonType.Flying, setup.Defender.Type1) + DamageCalculator.CalculateTypeMatchup(Enums.PokemonType.Flying, setup.Defender.Type2);
            setup.Multiplier *= fly;
            setup.Multiplier /= 4;
}
break;

I had the case calculate the damage that Element.Flying (albeit written in its enums forme) would make against the defender, and made it so the multiplier was affected by that damage. Since it was giving me wildly over-the-top damage, I figured out what should be the normal damage and divided fly by that. Rounded to the nearest number, made the multiplier divide by it.

I guess this case is a bit unique, but uhh... @anyone else with this issue-- See if you can't find some kind of work-around to affect multiplier like I did.

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