1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Unknown API Type Mismatch?

Discussion in 'Modding API' started by Kyneroth, Aug 18, 2018.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Kyneroth

    Kyneroth Trainee Engineer

    Messages:
    6
    Hey,

    I'm a very new beginner to Modding but I was trying to write a mod for an admin block (prevents damage). I want to have this block switch on and off by the player so they can control when the grid can be damaged etc. For some reason, once I have made a list of the available blocks on the grid, I then can't get the API to detect whether they are "Enabled". In the example code below, there is a marked out line which does this function. With it marked out, the grid is protected regardless of the block being on/off, it just has to be on the grid. With the line un-marked, the mod just doesn't work. Have I got some type of mis-match or am I using the wrong command, please help?

    Kyneroth

    Code:
    public void gridProtection(object target,ref MyDamageInformation damageInfo) {
    	VRage.Game.ModAPI.IMySlimBlock damagedBlock = target as VRage.Game.ModAPI.IMySlimBlock;
    	if(damagedBlock == null) { return; }
    	VRage.Game.ModAPI.IMyCubeGrid damagedGrid = damagedBlock.CubeGrid as VRage.Game.ModAPI.IMyCubeGrid;
    	if(damagedGrid == null) { return; }
    	List<VRage.Game.ModAPI.IMySlimBlock> blocks = new List<VRage.Game.ModAPI.IMySlimBlock>();
    	damagedGrid.GetBlocks(blocks, (x) => x.FatBlock is Sandbox.ModAPI.IMyTerminalBlock && x.FatBlock.BlockDefinition.SubtypeId.Contains("AdminiumArmor"));
    	foreach (var x in blocks) {
    //				if((x as IMyTerminalBlock).Enabled) {blockOn = true;}
    	}
    	if(blocks.Count > 0 && blockOn == true) {
    		damageInfo.Amount = 0;
    		damageInfo.IsDeformation = false;
    	}
    }
     
  2. Digi

    Digi Senior Engineer

    Messages:
    2,393
    IMyTerminalBlock doesn't have an Enabled property, IMyFunctionalBlock does.
    The F11 menu is where the compile errors are shown (or in the game log).

    You should use an IDE like Visual Studio (setup guide: https://forum.keenswh.com/threads/setup-visual-studio-for-se-programming.7385455/#post-1286989996 ).
    It will greatly help with autocomplete, compiling validation and you can even have a whitelist checker (https://github.com/WhitePhoera/SEWhitelistDiagnostic).

    That code will impact performance quite significantly though, you need a faster way of knowing if the grid has your block.
    One way would be: add a gamelogic component to that block and from it add the block itself to a list in a session component (via static instance accessor, remember to null it on UnloadData tho), then in the damage event just iterate that list and compare each block's grid with the grid in your event. I imagine the list would be small (<15 elements) so this should be fast enough.
     
  3. Kyneroth

    Kyneroth Trainee Engineer

    Messages:
    6
    Cheers Digi. Thank you for your fast response. That was really helpful thank you. I've tried using visual studio but was struggling to import the reference libraries so the link is super appreciated.

    Alright so first part, say if I wasn't to implement the suggested lag improvement you mentioned, is there a way of parsing the block to operate, for example, just declaring that block as IMyFunctionalBlock on that line or will that just produce another error?
    Second part, let's say that my coding ability is on par with a mine detector using his foot as trial and error...
    Would I be able to be really cheeky and ask for an example? When you say gamelogic component, I'm guessing you don't mean building component but rather a key feature? If I was to see a brief example I might be able to fumble my way forward from there.
    Again, thank you for your help
     
  4. Digi

    Digi Senior Engineer

    Messages:
    2,393
    I'm not sure what you mean by the first part there.

    Here's an example:

    You can split those classes into their own files if you want.

    EDIT:
    I guess you could also add
    Code:
    if(info.Amount == 0)
    	return;
    
    at the start of BeforeDamage() to ignore damages that are already nullified by other mods.
     
  5. Kyneroth

    Kyneroth Trainee Engineer

    Messages:
    6
    So the code itself operates at the moment to prevent damage but my intention for the block is to be turned on and off by the player so they can work on the grid and then switch it back to an admin grid to prevent damage.
    So I was asking, if IMyTerminalBlock doesn't contain an enabled property, would there be a way of declaring or parsing the block into an IMyFunctional block so that I can detect if the block is online and therefore prevent damage? I was using a list just in case there was another admin block attached, through merge or connector.
     
  6. Digi

    Digi Senior Engineer

    Messages:
    2,393
    And why can't you just add the .Enabled check in the loop there? The list I made is already IMyFunctionalBlock.
     
  7. Kyneroth

    Kyneroth Trainee Engineer

    Messages:
    6
    Sorry, I was just answering the top part of your message.
    Awesome, I will add that into the loop there as an if statement. Thanks for your help dude.
     
    • Like Like x 1
Thread Status:
This last post in this thread was made more than 31 days old.