Skip to content
Permalink
Browse files
Change "Script.AddScript" signature to generic "Script.InstantiateScr…
…ipt"
  • Loading branch information
crosire committed Oct 12, 2019
1 parent 5df5ef3 commit b5a3df89fac4840391e81222bb7be3285527bef7
Showing with 43 additions and 45 deletions.
  1. +1 −1 Examples.csproj
  2. +38 −40 examples/{AddScript.cs → ScriptInstance.cs}
  3. +4 −4 source/scripting_v3/GTA/Script.cs
@@ -36,10 +36,10 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="examples\AddScript.cs" />
<Compile Include="examples\EuphoriaDemo.cs" />
<Compile Include="examples\ExitVehicle.cs" />
<Compile Include="examples\IndicatorControl.cs" />
<Compile Include="examples\ScriptInstance.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="source\scripting_v3\ScriptHookVDotNet_APIv3.csproj">
@@ -4,12 +4,12 @@

namespace ScriptInstance
{
// Main Script is AutoStarted and using key press creates AI Scripts.
// T Key to Spawn AIone
// Y Key to Spawn AItwo
// G Key to change AIone Animation
// H Key to SetWait(6) for AItwo
// J Key to Pause AIone
// Main script is auto-started and creates AI scripts using key presses.
// T key to spawn AIone
// Y key to spawn AItwo
// G key to change AIone animation
// H key to SetWait(6) for AItwo
// J key to pause AIone

public class Main : Script
{
@@ -18,19 +18,13 @@ public class Main : Script

public Main()
{
Tick += OnTick;

KeyDown += OnKeyDown;

Interval = 1000;
}

private void OnTick(object sender, EventArgs e) { }

private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e == null) return

if (e.KeyCode == Keys.T) // Create AI Script and store as AIone
{
SpawnAIone();
@@ -47,7 +41,10 @@ private void OnKeyDown(object sender, KeyEventArgs e)
{
AIone.animation = "HandsUp";
}
else AIone.animation = "Jump";
else
{
AIone.animation = "Jump";
}

GTA.UI.Notification.Show("SpawnAI: Animation(" + AIone.animation + ");");
}
@@ -58,16 +55,22 @@ private void OnKeyDown(object sender, KeyEventArgs e)
}
else if (e.KeyCode == Keys.J) // Toggles Pause() for AIone
{
if (AIone.IsPaused) AIone.Resume();
else AIone.Pause();
if (AIone.IsPaused)
{
AIone.Resume();
}
else
{
AIone.Pause();
}
}
}

private void SpawnAIone()
{
if (AIone == null)
{
AIone = (AI)AddScript(typeof(AI));
AIone = InstantiateScript<AI>();

if (AIone != null)
{
@@ -78,7 +81,7 @@ private void SpawnAIone()
{
AIone.Abort();

AIone = null; // Clear Held instance to create a new script;
AIone = null; // Clear instance to create a new script next time

GTA.UI.Notification.Show("SpawnAI: Ped(1).Abort();");
}
@@ -88,7 +91,7 @@ private void SpawnAItwo()
{
if (AItwo == null || !AItwo.IsRunning)
{
AItwo = (AI)AddScript(typeof(AI));
AItwo = InstantiateScript<AI>();

if (AItwo != null)
{
@@ -99,7 +102,7 @@ private void SpawnAItwo()
{
AItwo.Abort();

//AItwo = null; Instead of clearing this instance i added a check to see if the old instance had been Aborted.
// Instead of setting AItwo to null, can also checking status with 'IsRunning'

GTA.UI.Notification.Show("SpawnAI: Ped(2).Abort();");
}
@@ -111,38 +114,39 @@ public class AI : Script
{
public AI()
{
Aborted += Shutdown;

Tick += OnTick;
Aborted += OnShutdown;

Interval = 3000;
}

private Ped ped = null;
private int wait = -1;
public string animation = "HandsUp";
private int _Wait = -1;

public void SetWait(int ms)
{
if (ms > _Wait) _Wait = ms;
if (ms > wait)
{
wait = ms;
}
}

private void OnTick(object sender, EventArgs e)
{
if (_Wait > -1)
if (wait > -1)
{
Wait(_Wait);
_Wait = -1;
Wait(wait);
wait = -1;
}

if (ped == null || !ped.Exists())
if (ped == null)
{
Model model = new Model(PedHash.Beach01AMY);

ped = World.CreatePed(model, Game.Player.Character.Position + (GTA.Math.Vector3.RelativeFront * 3));
ped = World.CreatePed(PedHash.Beach01AMY, Game.Player.Character.Position + (GTA.Math.Vector3.RelativeFront * 3));
}

if (ped != null && ped.IsAlive) // OnTick Repeats animation if Alive.
// Repeat animation if alive
if (ped != null && ped.IsAlive)
{
if (animation == "HandsUp")
{
@@ -151,20 +155,14 @@ private void OnTick(object sender, EventArgs e)
else if (animation == "Jump")
{
ped.Task.Jump();

//Abort();
}
}
}

private void Shutdown(object sender, EventArgs e)
private void OnShutdown(object sender, EventArgs e)
{
if (ped != null) // On Abort() clears the ingame Ped
{
ped.MarkAsNoLongerNeeded();

ped.Delete();
}
// Clear pedestrian on script abort
ped?.Delete();
}
}
}
@@ -283,19 +283,19 @@ public static void Yield()
}

/// <summary>
/// Adds a new <see cref="Script"/> to the CurrentDomain threads.
/// Spawns a new <see cref="Script"/> instance of the specified type.
/// </summary>
public static Script AddScript(Type scriptType)
public static T InstantiateScript<T>() where T : Script
{
var script = SHVDN.ScriptDomain.CurrentDomain.InstantiateScript(scriptType);
var script = SHVDN.ScriptDomain.CurrentDomain.InstantiateScript(typeof(T));
if (script == null)
{
return null;
}

script.Start();

return (Script)script.ScriptInstance;
return (T)script.ScriptInstance;
}
}
}

0 comments on commit b5a3df8

Please sign in to comment.