How do you call events through c# code?

Started by signekatt, March 25, 2017, 06:00:33 PM

Previous topic - Next topic

signekatt

Is there a specific class that handles events and can you call them by for example name or some other identifier?

If anyone more experienced with modding rim world knows i would really appreciate the help.

Thank you.

Fluffy (l2032)

What are you trying to do? It would help us give a better answer if we had a better idea of what you're trying to accomplish.

signekatt

I am trying to implement some way of calling events from buildings, so maybe having a weather controlling machine or something similar.

RawCode

please show your code and mark place where "event call" should be placed and name of "event".

astralflow suggestion:
use IncidentQueue

quadroman1

One way would to create a new instance of an event and call the TryExecute function.  For example:


IncidentWorker_ColdSnap cs = new IncidentWorker_ColdSnap();

void SomeFunction(IncidentParms parms)
{
   cs.TryExecute(parms);
}


Not sure if this is the best way to do it but it works.

Jaxxa

I have a mod that calls the Trader Arrived events from a building in A15.

https://github.com/jaxxa/ED-SubspaceTransponder

Greep

https://ludeon.com/forums/index.php?topic=3408.0

I think I remember the stickied thread here having a weather based building in the third example, so it might help.
1.0 Mods: Raid size limiter:
https://ludeon.com/forums/index.php?topic=42721.0

MineTortoise:
https://ludeon.com/forums/index.php?topic=42792.0
HELLO!

(WIPish)Strategy Mode: The experienced player's "vanilla"
https://ludeon.com/forums/index.php?topic=43044.0

busboybud

#7
Does the code exampled still work in 1.0? I'm trying to fire (any) event on a tick interval basis but I always get a null ref.

[HugsLib][ERR] EMUTI caused an exception during OnTick: System.NullReferenceException: Object reference not set to an instance of an object
  at RimWorld.IncidentWorker_MakeGameCondition.TryExecuteWorker (RimWorld.IncidentParms parms) [0x00000] in <filename unknown>:0
  at RimWorld.IncidentWorker.TryExecute (RimWorld.IncidentParms parms) [0x00000] in <filename unknown>:0
  at EMUTI.EMUTI.fireEvent (System.String eventName) [0x00000] in <filename unknown>:0


The code is simple enough.. I'm guessing there is an assumption being made on one or more of the properties within IncidentParms never being null. This error happens for all the events I've tried to some capacity.

public void FireEvent(string eventName = "ColdSnap")
{
Log.Message("Begin FireEvent()");

IncidentParms parms = new IncidentParms
{
forced = true
};

IncidentDef def = DefDatabase<IncidentDef>.GetNamed(eventName);
def.Worker.TryExecute(parms);

Log.Message("End FireEvent()");
}


EDIT: After decompiling the source, I realized the target (Map in the case of ColdSnap) is required, and some other properties (i.e. points) are likely to be required depending on the type of Incident. My temporary fix is below, but there must be a better way to pull the current Map, World, or whatever target you need:



public Map MyMap;
public override void MapLoaded(Map map)
{
base.MapLoaded(map);
MyMap = map;
Log.Message("----- Map Loaded -----");
}

public void FireEvent(string eventName = "ColdSnap")
...
IncidentParms parms = new IncidentParms
{
target = MyMap,
forced = true
};
...