Simple .dll mod help?

Started by CB elite, December 29, 2014, 12:27:32 PM

Previous topic - Next topic

CB elite

So, I've read haplo's guide to creating a .dll mod, and it was super informative. However, being very new to the modding scene still, I have some very beginner questions.

I am working on creating a variation of the Verb_MeleeAttack for a weapon I am adding to the mod I am working on. I found the code for the Verb_MeleeAttack after using ILSpy on the Assembly-CSharp.dll.

All I want to do is take that code, and use it to create a verb that will have the same function as the Verb_MeleeAttack but will have different sounds. How do I go about accomplishing this?

I have Microsoft Visual Studio, if that's of any help :)

I just don't know how to use it very well, yet. Don't even know how to save a file as a .dll :(


Lord Fappington

to save a file as a .dll you'll have to specify a coding project as to be compiled as a library; be sure to compile with a .NET 3.5 target.

As for your specific question, I'd have to look at it abit to figure it out (I too am new to c# coding as well as rimworld modding).

Best,
Fap

unifex

#2
Quote from: CB elite on December 29, 2014, 12:27:32 PM
So, I've read haplo's guide to creating a .dll mod, and it was super informative. However, being very new to the modding scene still, I have some very beginner questions.

I am working on creating a variation of the Verb_MeleeAttack for a weapon I am adding to the mod I am working on. I found the code for the Verb_MeleeAttack after using ILSpy on the Assembly-CSharp.dll.

All I want to do is take that code, and use it to create a verb that will have the same function as the Verb_MeleeAttack but will have different sounds. How do I go about accomplishing this?

I have Microsoft Visual Studio, if that's of any help :)

I just don't know how to use it very well, yet. Don't even know how to save a file as a .dll :(

I've just uninstalled VS Express as it was about to expire and had trouble creating another Microsoft ID, but from memory:

1) Create a new C# "Class Library" project.
2) It will already have a class defined in your solution (use the explorer on the right pane to navigate classes), you can rename that to e.g. Verb_ChainswordAttack. Or you can File/Add.. a new Class.
3) Ultimately you should wind up with a file on the file system called Verb_ChainswordAttack.cs within your solution.
3) It will have a default namespace. This is to avoid class naming collisions (two different libraries creating a Dog class) and can be what you like. Change that to something like "MyNamespace".
4) If you go to the Project menu and choose Rebuild, that will compile your source code to a .DLL under bin/Debug (unless you have compile errors). You can change the build configuration to a 'Release' build, afaik that would strip out debugging symbols so won't make much difference (might optimize other stuff, not too sure).
5) In the ThingDef, you would refer to it as <verbClass>MyNamespace.Verb_ChainswordAttack</verbClass>
6) Your mod structure needs that assembly to be under Assemblies off the top level folder (same level as Defs).

Edit: obj/Debug -> bin/Debug

CB elite

#3
Thank you both for the help! I've made some progress, but I'm running into errors that I don't really know how to comprehend yet. Mind if I ask for a little more help?

Here is the full error message I get on startup:



Does this mean I have compile errors?

I have Assembly-CSharp.dll and UnityEngine.dll as references, but I'm being told that certain things within the Assembly-CSharp.dll are not existing in the current context. Do I have to copy all of the code over to my .dll file for it to work???

Edit:

Here's an example of what I'm seeing. It ways I'm missing something, but my references say I have it...


Rikiki

The error message says BadImageFormatException.
You must use png images.

Carnov

I'm also trying to learn .dll moding, and from what I can see, you might be missing the Rimworld using.
I think MeleeAttackMode is from the Rimworld using, simply add at the beginning :
using RimWorld;
It should solve some of your errors I think

unifex

That's right, its missing importing the RimWorld namespace at the top (using RimWorld;)

The image error I think could be my fault, try using the bin/Debug/*.dll instead of obj/Debug/*.dll. The latter is what you want, I'd brainfaded a bit.

unifex

The other thing I was going to mention is a tool called ILSpy.

http://ilspy.net/

This will decompile the bytecode in a .NET dll (my terminology is probably incorrect there), so you can have a look at the classes and to some extent the code. So you can File -> Open the main Assembly-CSharp to help find classes/techniques if you get stuck (its got a Search option and a limited Analyze option to help find references and things).

CB elite

Quote from: Carnov on December 30, 2014, 04:57:41 AM
I'm also trying to learn .dll moding, and from what I can see, you might be missing the Rimworld using.
I think MeleeAttackMode is from the Rimworld using, simply add at the beginning :
using RimWorld;
It should solve some of your errors I think

It actually solved all of my errors, so thank you a bunch!

However, now I face a new issue. The weapon that I have applied the verb to is seemingly useless, as the pawn does not use it in combat. The code for my verb is exactly the same as the melee attack verb, with the exception of the public class name (as you'll see in the picture I posted earlier), and the soundDef names for: hitting buildings and pawns, and missing altogether.

It reminds me of the frustration that came with installing a bionic arm on a colonist in Alpha 7; they wouldn't use a melee weapon, they would just punch their enemies... Ideas?

P.s. thank you all SO much for helping me out :D

unifex

I wonder if you get a right-click context menu item when using the weapon and you target something?

I haven't looked into it much yet, but I think the Verb_MeleeAttack is referenced elsewhere, for example, in Pawn.NewCurrentBestMeleeAttackVerb() it looks like it looks at your body's HealthDiffs (synonymous with a body part I guess) to find its best melee attack.

You might get more mileage out of subclassing the Verb_MeleeAttack class instead of subclassing just Verb i.e.

    public class Verb_PokeAttack : Verb_MeleeAttack { ... }

This way your verb will still "look like" a melee attack in those bits, and the rest of your code probably won't change. It might also be referenced in the think trees.

CB elite

You were exactly right :D

It works now! I honestly can't thank you enough for your help :) This adds a whole new level of immersion to my mod.

Lord Fappington