Is there a way to make new variables (.dll) accessable via xml?

Started by Haplo, April 14, 2014, 04:05:00 PM

Previous topic - Next topic

Haplo

Hello,

does anyone know of a way to define new variables in c# which can be defined via xml?
And if you know how to, can you please tell me?
I'm looking for a way to make ApplyDamageMin, ApplyDamageMax, ApplyDamageTicks of a Building accessable via xml, but so far I haven't found anything that works..

iame6162013

Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Robert J. Hanlon: "Never attribute to malice that which is adequately explained by stupidity."

Architect

Quote from: iame6162013 on April 14, 2014, 04:32:38 PM
doesn't the "FertilizerPump" mod have the stuff u need?

Nah, he's looking to make his own tags in the XML files, and read them in his code. Truth be told I've been looking to ask the same thing soon, just haven't got round to it :P
Check out BetterPower+ and all its derivatives by clicking the picture below.

It adds many new methods of power generation and uses for it, as well as other things such as incidents.


iame6162013

Quote from: Architect on April 14, 2014, 04:35:47 PM
Quote from: iame6162013 on April 14, 2014, 04:32:38 PM
doesn't the "FertilizerPump" mod have the stuff u need?

Nah, he's looking to make his own tags in the XML files, and read them in his code. Truth be told I've been looking to ask the same thing soon, just haven't got round to it :P

yea i'm intrested in .dll mods to just i don't know how to :)
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Robert J. Hanlon: "Never attribute to malice that which is adequately explained by stupidity."

WorldOfIllusion

I've tried to work this out as well, so far without luck.

Looking through decompiled code it seems that it may be hardcoded what properties get loaded from XML at the moment (there's classes which dictate the fields for the different types of objects), but I have no idea how it defines which class to use as the list of properties, so no way to mod it yet.
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)

Vendan

Actually, this is currently possible, and really rather easy.  First, extend ThingDef with your extra desired properties, i.e.

public class MyThingDef : ThingDef
{
  public string ExtraStuff;
}


Then just add a class="" to your xml, i.e.

<ThingDef Class="MyThingDef, MyAssembly">


Note, you need to have a fully referenced type in there, i.e. with the namespace AND the assembly name, in this example, the assembly is named MyAssembly.

Then just add the relevant tags, i.e.

<ExtraStuff>Ketchup</ExtraStuff>


and that thingdef will be of type MyThingDef with an .ExtraStuff == "Ketchup".  RimWorld does a bunch of type conversion stuff, so you can easily have dictionaries, lists, enums, just look at what ThingDef already has!

Haplo

Thank you Vendan.
Based on your definition I could read the variables. There was a part missing, as I got mostly NullRefExceptions but
I figured out what I did wrong: I tried to force the def to be the new class via:
public new ModNamespace.MyThingDef def;
But the result was always null.. Strange, isn't it? ;)

For everyone else, here is the complete guide how to could access your variables.
All thanks goes to Vendan, I only did a bit of clarification :)

First, make an extend ThingDef with your extra desired properties, i.e.

public class MyThingDef : ThingDef
{
  public string myVariable;
}


Then just expand the ThingDef-definition with class="" in your xml, i.e.

<ThingDef Class="ModNamespace.MyThingDef, MyAssemblyFilenameWithoutExtension">

Note, you need to have a fully referenced type in there, i.e. with the namespace AND the assembly name, in this example, the assembly is named MyAssemblyFilenameWithoutExtension.

Then just add the relevant tags, i.e.

<myVariable>Ketchup</myVariable>


and that thingdef will be of type MyThingDef with an .myVariable == "Ketchup". 
RimWorld does a bunch of type conversion stuff, so you can easily have dictionaries, lists, enums, just look at what ThingDef already has!

Access to the variables is only possible with a typecast:

((ModNamespace.MyThingDef)def).myVariable

Vendan

yeah, I'm adding code inside MyThingDef, so I had access to it as type MyThingDef already.  You should do some stuff like

if(thingdef is MyThingDef)
{
  MyThingDef mydef = (MyThingDef)mydef;
  //work with it here
}


to keep from having exceptions.  Also, the AssemblyName isn't always the name of the dll without the extension, it's whatever is in the AssemblyName field in your project properties.

Haplo

Oh, thanks for clarification. I didn't even notice the entry there. Just changed it with everything else when needed. Changing without thinking ;)

Tynan

Tynan Sylvester - @TynanSylvester - Tynan's Blog

StorymasterQ

Quote from: Tynan on April 15, 2014, 07:32:37 PM
Wow, thank you for figuring this out!

Oh, great, the actual developer is learning from the modding community :D You really ought to employ these people, Tynan!
I like how this game can result in quotes that would be quite unnerving when said in public, out of context. - Myself

The dubious quotes list is now public. See it here

Vendan

Lol, hey is there a good place for us to post requests for mod writers?  There's a lot of things that I could do way easier if there was just a little support added to the engine.  Stuff like I've had to extend Pawn just to get a little extra data stored in the save game...

Tynan

Quote from: Vendan on April 15, 2014, 08:52:19 PM
Lol, hey is there a good place for us to post requests for mod writers?  There's a lot of things that I could do way easier if there was just a little support added to the engine.  Stuff like I've had to extend Pawn just to get a little extra data stored in the save game...

Please do ask questions, I want to add stuff that's needed. Or maybe there's an easier way to do what you want to do.
Tynan Sylvester - @TynanSylvester - Tynan's Blog

WorldOfIllusion

#13
has anyone tried this with enumeration? When I tried rimworld wouldn't launch and was throwing 'unable to cast' errors in the log.

*EDIT* that said, I now have it working.
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)