Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Code Snippets

Ghillie edited this pageDec 23, 2022 ·25 revisions

This is a collection of some useful code-snippets made by users. Please feel free to add more!

Quick Jump


Force GroundZ/True Water Depth by Dilapidated

This function will force GroundZ/WaterZ and grab the WaterDepth of areas even if the collisions of the map aren't loaded and the area is far away from the player

/// <summary>///     Forces Ground Z position even when the location doesn't have collisions loaded/// </summary>publicstaticVector3ForceGroundZ(thisVector3v){floatzcoord=0.0f;varoutArgb=newOutputArgument();float[]firstCheck=newfloat[]{0,100,200,300,400,500,600,700,800,900,1000};float[]secondCheck=newfloat[]{1000,900,800,700,600,500,400,300,200,100,0,-100,-200,-300,-400,-500};float[]thirdCheck=newfloat[]{-500,-400,-300,-200,-100,0,100,200,300,400,500,600,700,800,900,1000};if(Function.Call<bool>(Hash.GET_GROUND_Z_FOR_3D_COORD,v.X,v.Y,1000f,outArgb))zcoord=outArgb.GetResult<float>();if(zcoord==0){for(inti=0;i<firstCheck.Length;i++){Function.Call(Hash.REQUEST_COLLISION_AT_COORD,v.X,v.Y,firstCheck[i]);GTA.Script.Wait(0);}if(Function.Call<bool>(Hash.GET_GROUND_Z_FOR_3D_COORD,v.X,v.Y,1000f,outArgb))zcoord=outArgb.GetResult<float>();}if(zcoord==0){Log.Write(true,"ZCoord secondCheck");for(inti=0;i<secondCheck.Length;i++){Function.Call(Hash.REQUEST_COLLISION_AT_COORD,v.X,v.Y,secondCheck[i]);GTA.Script.Wait(0);}if(Function.Call<bool>(Hash.GET_GROUND_Z_FOR_3D_COORD,v.X,v.Y,1000f,outArgb))zcoord=outArgb.GetResult<float>();}if(zcoord==0){Log.Write(true,"ZCoord thirdCheck");for(inti=0;i<thirdCheck.Length;i++){Function.Call(Hash.REQUEST_COLLISION_AT_COORD,v.X,v.Y,thirdCheck[i]);GTA.Script.Wait(0);}if(Function.Call<bool>(Hash.GET_GROUND_Z_FOR_3D_COORD,v.X,v.Y,1000f,outArgb))zcoord=outArgb.GetResult<float>();}returnnewVector3(v.X,v.Y,zcoord);}/// <summary>///     Forces Water Z position even when the location doesn't have collisions loaded/// </summary>publicstaticVector3ForceWaterZ(thisVector3v){floatzcoord=-500.0f;varoutArgb=newOutputArgument();float[]firstCheck=newfloat[]{0,100,200,300,400,500,600,700,800,900,1000};float[]secondCheck=newfloat[]{1000,900,800,700,600,500,400,300,200,100,0,-100,-200,-300,-400,-500};float[]thirdCheck=newfloat[]{-500,-400,-300,-200,-100,0,100,200,300,400,500,600,700,800,900,1000};if(Function.Call<bool>(Hash.TEST_PROBE_AGAINST_ALL_WATER,v.X,v.Y,1000f,v.X,v.Y,-500f,1,outArgb))zcoord=outArgb.GetResult<Vector3>().Z;if(zcoord==-500){for(inti=0;i<firstCheck.Length;i++){Function.Call(Hash.REQUEST_COLLISION_AT_COORD,v.X,v.Y,firstCheck[i]);GTA.Script.Wait(0);}if(Function.Call<bool>(Hash.TEST_PROBE_AGAINST_ALL_WATER,v.X,v.Y,1000f,v.X,v.Y,-500f,1,outArgb))zcoord=outArgb.GetResult<Vector3>().Z;}if(zcoord==-500){Log.Write(true,"ZCoord secondCheck");for(inti=0;i<secondCheck.Length;i++){Function.Call(Hash.REQUEST_COLLISION_AT_COORD,v.X,v.Y,secondCheck[i]);GTA.Script.Wait(0);}if(Function.Call<bool>(Hash.TEST_PROBE_AGAINST_ALL_WATER,v.X,v.Y,1000f,v.X,v.Y,-500f,1,outArgb))zcoord=outArgb.GetResult<Vector3>().Z;}if(zcoord==-500){Log.Write(true,"ZCoord thirdCheck");for(inti=0;i<thirdCheck.Length;i++){Function.Call(Hash.REQUEST_COLLISION_AT_COORD,v.X,v.Y,thirdCheck[i]);GTA.Script.Wait(0);}if(Function.Call<bool>(Hash.TEST_PROBE_AGAINST_ALL_WATER,v.X,v.Y,1000f,v.X,v.Y,-500f,1,outArgb))zcoord=outArgb.GetResult<Vector3>().Z;}returnnewVector3(v.X,v.Y,zcoord);}/// <summary>///     Forces Water Depth even when the location doesn't have collisions loaded/// </summary>publicstaticfloatForceWaterDepth(thisVector3v){floatresult=0.0f;varground=v.ForceGroundZ().Z;varwater=v.ForceWaterZ().Z;if(water>ground)result=water-ground;if(ground==0)result=500f;returnresult;}}

Tick-System by Nacorpio

For those of you who find it very frustrating to work with ticks, I have come up with a solution.I don't know how many of you who would find this useful, but I do at least.

You just implement this abstract class, and you'll be ready to go.

An example on how you could use IsActive, is when wrapping around peds.This can also be found in my mod, which you can see on my Github page.You could make it tick only when the ped is alive, and so on.IsActive = Ped != null && Ped.IsAlive;

Basically, it organizes the ticks, and makes the whole thing a lot easier.You can also give any instance a lifespan.

If you still don't understand why you should use an updater, check my mod out:

https://github.com/Nacorpio/GTA-Z/blob/master/GTAZ/Controllable/ControllableEntity.cs
https://github.com/Nacorpio/GTA-Z/blob/master/GTAZ/Controllable/ControllablePed.cs
https://github.com/Nacorpio/GTA-Z/blob/master/GTAZ/Controllable/ControllableVehicle.cs
https://github.com/Nacorpio/GTA-Z/blob/master/GTAZ/Peds/ZombiePed.cs

publicabstractclassUpdater{privatebool_active=false;privateint_activeTick;privateint_inactiveTick;privatereadonlyint_startTick;privateint_tick;privateDictionary<int,Action>_actionQueue=newDictionary<int,Action>();protectedUpdater(intstartTick=0){_tick=startTick;_startTick=startTick;}publicvoidTick(){OnUpdate(_tick);if(_actionQueue.ContainsKey(_tick)){_actionQueue[_tick].Invoke();}if(_tick==0||_tick==_startTick){OnFirstUpdate();}if(_active){_inactiveTick=0;OnActiveUpdate(_activeTick,_tick);if(_activeTick==0){OnFirstActiveUpdate(_tick);}_activeTick++;}else{_activeTick=0;OnInactiveUpdate(_inactiveTick,_tick);if(_inactiveTick==0){OnFirstInactiveUpdate(_tick);}_inactiveTick++;}_tick++;}/// <summary>/// Fired everytime a tick has been made./// </summary>/// <param name="tick">The total amount of ticks.</param>protectedabstractvoidOnUpdate(inttick);/// <summary>/// Fired on the absolute first update./// </summary>protectedabstractvoidOnFirstUpdate();/// <summary>/// Fired everytime a tick has been made while active./// </summary>/// <param name="activeTick">The current tick on this activity state.</param>/// <param name="tick">The total amount of ticks.</param>protectedabstractvoidOnActiveUpdate(intactiveTick,inttick);/// <summary>/// Fired everytime a tick has been made while inactive./// </summary>/// <param name="inactiveTick">The current tick on this activity state.</param>/// <param name="tick">The total amount of ticks.</param>protectedabstractvoidOnInactiveUpdate(intinactiveTick,inttick);/// <summary>/// Fired the first time an active update has been made./// </summary>/// <param name="tick">The total amount of ticks.</param>protectedabstractvoidOnFirstActiveUpdate(inttick);/// <summary>/// Fired the first time an inactive update has been made./// </summary>/// <param name="tick">The total amount of ticks.</param>protectedabstractvoidOnFirstInactiveUpdate(inttick);publicDictionary<int,Action>ActionQueue{get{return_actionQueue;}set{_actionQueue=value;}}/// <summary>/// Returns the tick of which the ticking started./// This isn't always 0, as the StartTick can be set./// </summary>publicintStartTick{get{return_startTick;}}/// <summary>/// Returns how many times this Updating instance has ticked./// </summary>publicintTick{get{return_tick;}}/// <summary>/// If active, returns how many times it has been ticked during its session./// </summary>publicintActiveTicks{get{return_activeTick;}}/// <summary>/// If inactive, returns how many times it has been ticked during its session./// </summary>publicintInactiveTicks{get{return_inactiveTick;}}/// <summary>/// Returns whether this Updating instance is active./// </summary>publicboolIsActive{get{return_active;}protectedset{_active=value;}}}

Logger Helper

As you cannot currently log items to the main ScriptHookVDotNet.log file, you can use this helper class to log events to a file of your choosing.

usingSystem;usingSystem.IO;/// <summary>/// Static logger class that allows direct logging of anything to a text file/// </summary>publicstaticclassLogger{publicstaticvoidLog(objectmessage){File.AppendAllText("MyModLogFile.log",DateTime.Now+" : "+message+Environment.NewLine);}}

Usage:

Logger.Log("This will get written to the log!");

Result:

31/05/2015 15:06:23 : This will get written to the log!

Simple Command/Hotkey Script

This is a basic script which allows you to easily add more commands/hotkeys by adding them to a dictionary. This script is especially useful if you just want to test new features. When you press Comma/Dot on your NumPad, a text-prompt will open and you can input your command.

publicclassMainScript:Script{privatereadonlyDictionary<Keys,Action>_hotkeys;privatereadonlyDictionary<string,Action<string[]>>_hotstrings;privatereadonlyUIText_statusText;privatebool_enabled;publicboolEnabled{get{return_enabled;}set{_statusText.Caption="Example Script: "+(value?"~g~ON":"~r~OFF");_enabled=value;}}publicMainScript(){Tick+=OnTick;KeyDown+=OnKeyDown;Interval=0;_statusText=newUIText("Example Script: ~r~OFF",newPoint(10,10),0.4f,Color.WhiteSmoke,0,false);Enabled=true;_hotstrings=newDictionary<string,Action<string[]>>();_hotstrings.Add("zerowanteds",(args)=>{Game.Player.WantedLevel=0;UI.Notify("Wanteds cleared!");});_hotkeys=newDictionary<Keys,Action>();_hotkeys.Add(Keys.Decimal,()=>{stringresult=Game.GetUserInput(20);if(result==null)return;String[]command=result.Split(' ');;if(_hotstrings.ContainsKey(command[0]))_hotstrings[command[0]](command.Skip(1).ToArray());elseUI.Notify("Unknown Command");});_hotkeys.Add(Keys.NumPad1,SpawnVeh);}privatevoidOnTick(objectsender,EventArgse){_statusText.Draw();if(!Enabled)return;}privatevoidOnKeyDown(objectsender,KeyEventArgse){if(e.KeyCode==Keys.F5)Enabled=!Enabled;if(!Enabled)return;foreach(varhotkeyin_hotkeys.Keys.Where(hotkey=>e.KeyCode==hotkey))_hotkeys[hotkey]();}publicvoidSpawnVeh(){//Spawn a Vehicle}}

Key Filter

A key eventHandler will hold events triggered during a Script.Wait();Once the Wait is complete your key eventHandler will work through the queued key presses.

To Handle the queue you can add a Filter Script like below...

publicclassKeyFilter:Script{publicKeyFilter(){KeyDown+=OnKey;}privatevoidOnKey(objectsender,KeyEventArgse){KeyFilter.Yield();if(e!=null&&!e.Handled)e.Handled=true;}}

The KeyFilter uses Yield() to wait until after your normal key eventHandler has run.In your Main key eventHandler you can check if the key is !e.Handled.

Native Handler Finder

This code finds the handler addresses of native functions and you can see how they exactly work. This is usefulonly for advanced developers who know how to read x64 assembly.You will need updated native hashes to get addresses of natives.FiveM providesa crossmap as well asthe game version array.

This code is a set of definitions to find native addresses. Ported fromOpenVHook.

address=FindPattern("\x76\x32\x48\x8B\x53\x40","xxxxxx");// you can use the implementation of FindPattern in SHVDNnativeRegistrationTableAddr=newIntPtr((long*)(*(int*)(address+9)+address+13));public unsafestruct NativeRegistration{public ulong nextRegBase;    publiculong nextRegKey;    public fixedulong handlers[7];    publicuint numEntries1;    publicuint numEntries2;    publicuint _unknown;    public fixedulong hashes[7];/*        // decryption        key = this ^ nextRegKey  // only lower 32 bits        nextReg = nextRegBase ^ key<<32 ^ key        // encryption        key = this ^ nextRegKey  // only lower 32 bits        nextRegBase = nextReg ^ key<<32 ^ key        only lower 32 bits of this^nextRegKey are used, higher 32 bits are ignored.        thus, higher 32 bit of nexRegBase must contain the info of (masked) higher address of next registration.        the first two members of struct are named as Base/Key respectively in that sense.    */publicstaticunsafeNativeRegistration*GetNextRegistration(NativeRegistration*reg){uintkey=(uint)(((ulong)reg)^reg->nextRegKey);return(NativeRegistration*)(reg->nextRegBase^((ulong)(key)<<32)^key);}publicstaticuintGetNumEntries(NativeRegistration*reg){return((uint)(((ulong)(&(reg->numEntries1)))&0xFFFFFFFF))^reg->numEntries1^reg->numEntries2;}publicstaticulongGetHash(NativeRegistration*reg,uintindex){uintkey=((uint)&reg->hashes[2*index])^(uint)reg->hashes[2*index+1];returnreg->hashes[2*index]^((ulong)(key)<<32)^key;}}publicstaticunsafeIntPtrGetNativeHandler(ulongnativeHash){if(nativeRegistrationTableAddr==IntPtr.Zero){returnIntPtr.Zero;}NativeRegistration*table=*(NativeRegistration**)(nativeRegistrationTableAddr+(int)(0x8*(nativeHash&0xFF))).ToPointer();for(;table!=null;table=NativeRegistration.GetNextRegistration(table)){for(uinti=0;i<NativeRegistration.GetNumEntries(table);i++){if(nativeHash==NativeRegistration.GetHash(table,i)){returnnewIntPtr((long)table->handlers[i]);}}}returnIntPtr.Zero;}

Usage;

GetNativeHandler(0x7D2B9E6A64637269);// get the handler address of PLAYER_PED_ID (works only in b2372)GetNativeHandler(0x8339643499D1222E);// get the handler address of 0x8339643499D1222E, a.k.a. _SET_ENTITY_ANGULAR_VELOCITY (the hash will be changed in the versions later than b2372)
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp