Reputation: 147
I just got into game modding, and I've hit a wall. I'm actually a C++ programmer, but the modding im currently doing requires me to use C#, which shouldn't be too big of an issue, but I am fairly certain I'm missing out on some key C# concepts.
Im trying to bind the "Delete" button to a function that increments a variable using "GetAsyncKeyState". I've tried every variation that is out there of this function, type casting it etc. but nothing is working. Below is the entire function and there are some examples of what variations I've tried below that again.
Function:
private void incModJump()
{
if (Convert.ToBoolean(Movement.GetAsyncKeyState(127) & 32768))
{
Thread.Sleep(150);
this.modJump += 1f;
this.modWallJump += 1f;
}
}
Variants of the "if" statement I've tried out.
(These are all tested with and without "Convert.ToBoolean", neither works.)
if (GetAsyncKeyState(127) > 0)
if (GetAsyncKeyState(127) & 0x8000)
if (GetAsyncKeyState(127) & 0x8000 == 0x8000)
if (GetAsyncKeyState(127) & 32768)
if (GetAsyncKeyState(127) & -32768)
None of the above seems to work, and I am at a loss as to how I will implement this feature, I've looked at example C# code using this function as well, so I doubt it's a syntax error (which the compiler would've warned me about, too.)
Like mentioned, I'm a C++ programmer, so this might be because I'm not so familiar with C#, therefore I will include the entire class I'm trying to modify, just in case the problem lies elsewhere. The only code I've touched here is including some more "using" headers and adding the variables that are within the if statement, and the function. (Please ignore the commented out part about the tokens, they're caused by the decompiler.)
using System;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;
// Token: 0x0200007B RID: 123
public class Movement : MonoBehaviour
{
// Token: 0x06000282 RID: 642
private void Start()
{
this.fighting = base.GetComponent<Fighting>();
this.standing = base.GetComponent<Standing>();
this.info = base.GetComponent<CharacterInformation>();
this.controller = base.GetComponent<Controller>();
this.grabHandler = base.GetComponent<GrabHandler>();
this.au = base.GetComponentInChildren<AudioSource>();
BodyPart[] componentsInChildren = base.GetComponentsInChildren<BodyPart>();
this.rigidbodies = new Rigidbody[componentsInChildren.Length];
for (int i = 0; i < this.rigidbodies.Length; i++)
{
this.rigidbodies[i] = componentsInChildren[i].GetComponent<Rigidbody>();
}
this.screenshake = ScreenshakeHandler.Instance;
this.rightHand = base.GetComponentInChildren<RightHand>().GetComponent<Rigidbody>();
this.leftHand = base.GetComponentInChildren<LeftHand>().GetComponent<Rigidbody>();
}
// Token: 0x06000283 RID: 643
private void FixedUpdate()
{
this.flyVelocity *= 0.95f;
if (this.controller.canFly)
{
this.MoveFly(this.flyVelocity);
this.MoveFly(Vector3.up * 0.37f);
this.leftHand.AddForce(Vector3.down * 2000f * Time.fixedDeltaTime + Vector3.forward * 2000f * Time.fixedDeltaTime, ForceMode.Acceleration);
this.rightHand.AddForce(Vector3.down * 2000f * Time.fixedDeltaTime + Vector3.forward * -2000f * Time.fixedDeltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000284 RID: 644
private void MoveFly(Vector3 direction)
{
if (this.info.sinceFallen < 0f)
{
return;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(direction * this.forceMultiplier * this.fighting.movementMultiplier * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(direction * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000285 RID: 645
public void Fly(Vector3 direction)
{
this.flyVelocity += direction * Time.deltaTime * 10f;
}
// Token: 0x06000286 RID: 646
public void MoveRight()
{
if (this.info.sinceFallen < 0f)
{
return;
}
float num = 1f;
if (!this.controller.isAI)
{
num = Mathf.Abs((!this.controller.HasControl) ? ((this.standing.LeftStickYValue >= -0.5f) ? 0.6f : 0f) : this.controller.PlayerActions.Movement.X);
}
if (this.grabHandler.isHoldingSomething)
{
num *= 0.1f;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(-Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(-Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000287 RID: 647
public void Move(float direction)
{
if (this.info.sinceFallen < 0f)
{
return;
}
float num = 1f;
if (!this.controller.isAI)
{
num = Mathf.Abs(this.controller.PlayerActions.Movement.X);
}
if (this.grabHandler.isHoldingSomething)
{
num *= 0.1f;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(direction * Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(direction * Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000288 RID: 648
public void MoveLeft()
{
if (this.info.sinceFallen < 0f)
{
return;
}
float num = 1f;
if (!this.controller.isAI)
{
num = Mathf.Abs((!this.controller.HasControl) ? ((this.standing.LeftStickYValue >= -0.5f) ? 0.6f : 0f) : this.controller.PlayerActions.Movement.X);
}
if (this.grabHandler.isHoldingSomething)
{
num *= 0.1f;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000289 RID: 649
public bool Jump(bool force = false, bool forceWallJump = false)
{
bool result = this.DoJump(force, forceWallJump);
this.au.PlayOneShot(this.jumpClips[UnityEngine.Random.Range(0, this.jumpClips.Length)]);
return result;
}
// Token: 0x0600028A RID: 650
private bool DoJump(bool force = false, bool forceWallJump = false)
{
bool result = false;
this.standing.gravity = this.jumpTime * 0.5f;
float d = 0.3f;
foreach (Rigidbody rigidbody in this.rigidbodies)
{
rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z);
if (!force)
{
if (this.info.wallNormal != Vector3.zero)
{
rigidbody.AddForce(this.info.wallNormal * this.jumpForceMultiplier * this.modWallJump, ForceMode.VelocityChange);
rigidbody.AddForce(Vector3.up * this.jumpForceMultiplier * this.modWallJump, ForceMode.VelocityChange);
result = true;
}
else
{
rigidbody.AddForce(Vector3.up * this.jumpForceMultiplier * this.modJump, ForceMode.VelocityChange);
result = false;
}
}
else if (forceWallJump)
{
rigidbody.AddForce(this.info.wallNormal * d * this.jumpForceMultiplier * 0.75f, ForceMode.VelocityChange);
rigidbody.AddForce(Vector3.up * d * this.jumpForceMultiplier * 0.85f, ForceMode.VelocityChange);
}
else
{
rigidbody.AddForce(Vector3.up * d * this.jumpForceMultiplier, ForceMode.VelocityChange);
}
}
this.screenshake.AddShake(Vector3.up * 0.01f);
return result;
}
// Token: 0x060014F1 RID: 5361
private void incModJump()
{
if (Convert.ToBoolean(Movement.GetAsyncKeyState(127) & 32768))
{
Thread.Sleep(150);
this.modJump += 1f;
this.modWallJump += 1f;
}
}
// Token: 0x06001526 RID: 5414
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(int nVirtKey);
// Token: 0x040002CB RID: 715
public RigidbodyMovement[] rigsToMove;
// Token: 0x040002CC RID: 716
public float forceMultiplier;
// Token: 0x040002CD RID: 717
public float jumpForceMultiplier;
// Token: 0x040002CE RID: 718
public float jumpTime = 0.5f;
// Token: 0x040002CF RID: 719
private Standing standing;
// Token: 0x040002D0 RID: 720
private CharacterInformation info;
// Token: 0x040002D1 RID: 721
private Controller controller;
// Token: 0x040002D2 RID: 722
private GrabHandler grabHandler;
// Token: 0x040002D3 RID: 723
private Fighting fighting;
// Token: 0x040002D4 RID: 724
private Rigidbody[] rigidbodies;
// Token: 0x040002D5 RID: 725
private ScreenshakeHandler screenshake;
// Token: 0x040002D6 RID: 726
private AudioSource au;
// Token: 0x040002D7 RID: 727
public AudioClip[] jumpClips;
// Token: 0x040002D8 RID: 728
public Vector3 flyVelocity = Vector3.zero;
// Token: 0x040002D9 RID: 729
private Rigidbody leftHand;
// Token: 0x040002DA RID: 730
private Rigidbody rightHand;
// Token: 0x04001332 RID: 4914
private float modJump = 3f;
// Token: 0x04001333 RID: 4915
private float modWallJump = 3.75f;
}
If this is simply impossible, are there any other ways I could implement this feature?
Upvotes: 3
Views: 3259
Reputation: 3923
Here is a simple example of how you can use GetASyncKeyState() in C# to check if a key is pressed.
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Int32 vKey);
int VK_DELETE = 0x2E;
while (true)
{
short keyStatus = GetAsyncKeyState(VK_DELETE);
if ((keyStatus & 1) == 1)
{
//if you land here, the key was pressed
}
}
We are checking if the least significant bit is set, read more in the remarks on MSDN. This ensure we only execute once per key press rather than every time the keystate is polled. As others said in the comments, GetASyncKeyState is good for quick proof of concepts but not great for professional use. One of the reasons is because it's not safe across all applications, other applications might set the result of GetASyncKeyState() and also affect your program.
Upvotes: 2