Ludeon Forums

RimWorld => Mods => Help => Topic started by: Thom Blair III on August 19, 2018, 10:06:38 PM

Title: How to generate a keypress automatically?
Post by: Thom Blair III on August 19, 2018, 10:06:38 PM
I'm new here and starting to learn the basics of modding. I am wondering how to generate a keypress event. How can I do this? (I don't know how to use Harmony yet if that's the answer.)

When I asked this question on Stack Overflow, they wanted to know "what I was trying to generate  a keypress into" and I didn't know the answer. What programming language do mods use in XML files? (I'm only learning how to edit them right now, not DLLs)
Title: Re: How to generate a keypress automatically?
Post by: Jaxxa on August 19, 2018, 10:23:02 PM
1. When do you want this to happen?
2. What do you want to happen?

Most likely it will be easier to call some piece of code directly instead of trying to add a key event.

QuoteWhat programming language do mods use in XML files?
Extensible Markup Language
https://en.wikipedia.org/wiki/XML

XML is its own Language that is used for Data Storage.

Title: Re: How to generate a keypress automatically?
Post by: FueledByOCHD on August 19, 2018, 10:23:35 PM
While I am new as well to modding. But without knowledge of c# you will not be able to generate a key press event, If I am wrong please feel free to let me know. but from what I understand of modding RimWorld is the Def's are meant mainly for data that is meant to be use in game.
Title: Re: How to generate a keypress automatically?
Post by: Thom Blair III on August 19, 2018, 10:35:17 PM
Quote from: Jaxxa on August 19, 2018, 10:23:02 PM
1. When do you want this to happen?
2. What do you want to happen?
I have a mod (Allow Tool) that will unforbid everything on the map if you press the Home key. So, I was thinking about having the mod generate a Home keypress every Rimworld hour or so. It's an easy way to collect freshly killed animals for free meals and also keep rotting animals from accumulating.

Quote
Extensible Markup Language
XML is its own Language that is used for Data Storage.
Thank you! People on Stack Overflow yelled at me and said XML is not a language, it's a file format.
Title: Re: How to generate a keypress automatically?
Post by: Jaxxa on August 19, 2018, 11:23:26 PM
Quote from: FueledByOCHD on August 19, 2018, 10:23:35 PM
While I am new as well to modding. But without knowledge of c# you will not be able to generate a key press event, If I am wrong please feel free to let me know. but from what I understand of modding RimWorld is the Def's are meant mainly for data that is meant to be use in game.

I agree, this sounds like something that will need C#.

Quote from: Thom Blair III on August 19, 2018, 10:35:17 PM
I have a mod (Allow Tool) that will unforbid everything on the map if you press the Home key. So, I was thinking about having the mod generate a Home keypress every Rimworld hour or so. It's an easy way to collect freshly killed animals for free meals and also keep rotting animals from accumulating.

As far as I know you will need to use C# .dll files todo this in Rimworld. Also it is most likely better to make a mod that calls the "unforbid everything on the map" code directly instead of simulating a keypres.

Quote from: Thom Blair III on August 19, 2018, 10:35:17 PM
Thank you! People on Stack Overflow yelled at me and said XML is not a language, it's a file format.

Its not a programming language, it is a Markup language. A file format is not too bad a way of thinking of it. But I don't think it will be much use for this mod.


You could also look at non mod solutions, such as a program like AutoHotKey.
Title: Re: How to generate a keypress automatically?
Post by: Thom Blair III on August 20, 2018, 12:10:08 AM
Quoteit is most likely better to make a mod that calls the "unforbid everything on the map" code directly instead of simulating a keypres.
Ah! Awesome idea, I didn't think of that! That should be much easier.

QuoteYou could also look at non mod solutions, such as a program like AutoHotKey.
Yes, I was thinking about this, but modding (when I can do it) is as much fun as the game itself!  :D  It feels like I get to participate in the game with the pawns, doing research, constructing necessary things, helping out the colony! Like meta-gaming. :)

Thanks for all the input!
Title: Re: How to generate a keypress automatically?
Post by: Thom Blair III on August 20, 2018, 12:41:02 AM
So, I found the section of Allow Tool mod responsible for unforbidding all things:
<AllowTool.ThingDesignatorDef>
        <defName>AllowAllDesignator</defName>
<designatorClass>AllowTool.Designator_AllowAll</designatorClass>
<category>Orders</category>
<insertAfter>AllowTool.Designator_Forbid</insertAfter>
        <label>Unforbid everything</label>
<description>Unforbids all forbidden items on the map.
Click to activate.</description>
<iconTex>allowAll</iconTex>
<dragHighlightTex>allowHighlight</dragHighlightTex>
<soundSucceeded>TickHigh</soundSucceeded>
<hotkeyDef>DesignatorAllowAll</hotkeyDef>
<messageSuccess>Allow_all_success</messageSuccess>
<messageFailure>Allow_all_failure</messageFailure>
</AllowTool.ThingDesignatorDef>


So, judging by this, I would need a routine that calls/executes/runs (I'm not sure what the term is) DesignatorAllowAll every X ticks then starts the tick counter again. Does this seem like something that can be done in XML? If not, I am familiar enough with programming languages that I think I might be able to cobble together a C+ routine that does this, unless referencing/calling DesignatorAllowAll in the code is somehow complex. What do you think?
Title: Re: How to generate a keypress automatically?
Post by: FueledByOCHD on August 20, 2018, 12:48:11 AM
Why would you need to make a c+ routine? you could do same thing probably easier in c#, unless that is what you meant
Title: Re: How to generate a keypress automatically?
Post by: Thom Blair III on August 20, 2018, 04:09:45 AM
Whoops! Thank you, I actually didn't catch that C# is a language now. I've been out of the programming loop for so long the last one I worked with was C++. I'll check out C# and see what it's like. Thanks!
Title: Re: How to generate a keypress automatically?
Post by: Canute on August 20, 2018, 04:30:37 AM
Or in this case, you use a script program like AutoIt, and this script simulate a keypress each hour.
You can use the record/capute thing, and just edit the resulting script about the delay/timer.
But ofcouse you can learn c# for further use of your modding career ! :-)
Title: Re: How to generate a keypress automatically?
Post by: FueledByOCHD on August 20, 2018, 10:19:26 AM
If you use HugsLib then you can easily use the features of that by attaching to the Tick Method
Title: Re: How to generate a keypress automatically?
Post by: RawCode on August 20, 2018, 11:43:27 AM
who need to generate "key A pressed" when you can directly execute action, that will happen when player pressed said key?

also unity do not have such volatile and hard to manage events, instead you can check what keys pressed now in safe and non blocking manner, you can read about how to implement keys properly in unity manual.

if you want keys and events anyway, read about interrupts and threading, and probably you selected wrong game to mod, you really don't need c++ low level stuff for unity game, code injection is only exception.
Title: Re: How to generate a keypress automatically?
Post by: Thom Blair III on August 20, 2018, 07:31:09 PM
Thanks! I'll check out HugsLib when I get a chance. Until then, I'm just using a little AppleScript that just presses Home every 143 seconds/3 Rimworld hours. It works perfectly so far, but I'd like to get more into modding, so thanks for all the input!
Title: Re: How to generate a keypress automatically?
Post by: FueledByOCHD on August 21, 2018, 01:17:03 AM
Your welcome and hope you enjoy the modding :)