Noox Posted June 23, 2014 Share Posted June 23, 2014 Is it possible to stop a tick and execute another one in a different file (still in the same project/dll)? For example, I have Class1.cs and Class2.cs, just to keep things organized in Class1.cs in the main tick I do X and in Class2.cs I do Y. At a certain point I want to stop X and start Y, is it possible? Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 Make sure they are both public then do Class2.<ClassName>.<TickEventFunctionName>.Start() or .Stop() Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 Thanks! Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 Thanks! No problem Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 Something more tick related. I'm doing a script. it's basically the GTA V rampage and everything is fine except for one thing. I have one method which spawns the enemies with a for loop, the problem is that the tick stops while that method is being executed until the for loop is over and everything becomes glitched, I tried what you posted above but it doesn't work. (Class1 is called Main and Class2 Functions in this case, the tick is named mainTick) So I used Main.<mainTick>.Start(); This is the code if you need it Main namespace ClassLibrary1{ public class Main : Script { Functions functions = new Functions(); bool inRampage = false; bool healthIsBeingAdded = false; bool spawningEnemies = false; int killCounter = 0; Weapon weapon = Weapon.Rifle_AK47; public Main() { Interval = 250; this.Tick += new EventHandler(mainTick); this.KeyDown += new GTA.KeyEventHandler(keyTick); } public void mainTick(object sender, EventArgs e) { Game.DisplayText(Convert.ToString(killCounter)); //DISAPPEARS WHEN END IS PRESSED if(inRampage) { Player.Character.Weapons.FromType(weapon).Ammo = 500; if (Player.Character.Weapons.Current != weapon) //DOESN'T WORK Player.Character.Weapons.Select(weapon); if (Player.Character.Health <= 50 && !healthIsBeingAdded) //DOESN'T WORK functions.healthRegen(ref healthIsBeingAdded); //passed by reference and not by value if (!spawningEnemies) functions.enemySpawn(ref spawningEnemies); Player.WantedLevel--; //DOESN'T work, there was an if here and I removed it to see if it was the cause but it's the same foreach(Ped peds in Functions.enemyGroup) //THIS FOREACH IS NOT BEING EXECUTED { if (peds.Health < 1) { Game.DisplayText("One is dead!"); Functions.enBlip.Delete(); killCounter++; } if (Functions.enemyGroup.MemberCount < 1) Functions.enemyArray[0].Delete(); } } } public void keyTick(object sender, GTA.KeyEventArgs e) { if(e.Key == Keys.End) { functions.rampageSettings(); inRampage = true; } } }} Functions namespace ClassLibrary1{ class Functions : Script { static public Group enemyGroup; public static Blip enBlip; public static Ped[] enemyArray; public void rampageSettings() { Player.Character.Health = 100; Player.Character.Armor = 100; World.CarDensity = 0; World.PedDensity = 0; //I need to delete cops and peds close to the player when this is executed } public void healthRegen(ref bool healthRegen) { Main main = new Main(); healthRegen = true; for (int health = Player.Character.Health; health <= 100; health+=2) { Player.Character.Health = health; Wait(500); } healthRegen = false; } public void enemySpawn(ref bool spawning) //WORKS { //Main.<mainTick>.Start(); spawning = true; Random rnd = new Random(); int killsNeededToFinish = rnd.Next(50, 100); enemyArray = new Ped[killsNeededToFinish]; for(int i = 0; i < killsNeededToFinish; i++) { Vector3 playerPos = World.GetNextPositionOnStreet(Player.Character.Position.Around(100.0F)); enemyArray[i] = World.CreatePed("M_M_ENFORCER", playerPos, RelationshipGroup.Criminal); if (i == 0) enemyGroup = new Group(enemyArray[i]); else changeAI(enemyArray[i]); Wait(rnd.Next(1500, 7000)); /*if (!Exists(enemyArray[oldLeader])) { enemyGroup = new Group(enemyArray[i]); oldLeader = i; }*/ } } public void changeAI(Ped e) { enBlip = e.AttachBlip(); e.SetPathfinding(true, true, true); e.ChangeRelationship(RelationshipGroup.Player, Relationship.Hate); e.AlwaysDiesOnLowHealth = true; e.RandomizeOutfit(); e.CowerInsteadOfFleeing = true; e.Task.FightAgainst(Player.Character); Weapon weapon = Weapon.SMG_MP5; e.Weapons.FromType(weapon).Ammo = 500; e.Weapons.Select(weapon); e.Task.GoTo(Player.Character); enemyGroup.AddMember(e); } }} Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 Something more tick related. I'm doing a script. it's basically the GTA V rampage and everything is fine except for one thing. I have one method which spawns the enemies with a for loop, the problem is that the tick stops while that method is being executed until the for loop is over and everything becomes glitched, I tried what you posted above but it doesn't work. (Class1 is called Main and Class2 Functions in this case, the tick is named mainTick) So I used Main.<mainTick>.Start(); This is the code if you need it Main namespace ClassLibrary1{ public class Main : Script { Functions functions = new Functions(); bool inRampage = false; bool healthIsBeingAdded = false; bool spawningEnemies = false; int killCounter = 0; Weapon weapon = Weapon.Rifle_AK47; public Main() { Interval = 250; this.Tick += new EventHandler(mainTick); this.KeyDown += new GTA.KeyEventHandler(keyTick); } public void mainTick(object sender, EventArgs e) { Game.DisplayText(Convert.ToString(killCounter)); //DISAPPEARS WHEN END IS PRESSED if(inRampage) { Player.Character.Weapons.FromType(weapon).Ammo = 500; if (Player.Character.Weapons.Current != weapon) //DOESN'T WORK Player.Character.Weapons.Select(weapon); if (Player.Character.Health <= 50 && !healthIsBeingAdded) //DOESN'T WORK functions.healthRegen(ref healthIsBeingAdded); //passed by reference and not by value if (!spawningEnemies) functions.enemySpawn(ref spawningEnemies); Player.WantedLevel--; //DOESN'T work, there was an if here and I removed it to see if it was the cause but it's the same foreach(Ped peds in Functions.enemyGroup) //THIS FOREACH IS NOT BEING EXECUTED { if (peds.Health < 1) { Game.DisplayText("One is dead!"); Functions.enBlip.Delete(); killCounter++; } if (Functions.enemyGroup.MemberCount < 1) Functions.enemyArray[0].Delete(); } } } public void keyTick(object sender, GTA.KeyEventArgs e) { if(e.Key == Keys.End) { functions.rampageSettings(); inRampage = true; } } }} Functions namespace ClassLibrary1{ class Functions : Script { static public Group enemyGroup; public static Blip enBlip; public static Ped[] enemyArray; public void rampageSettings() { Player.Character.Health = 100; Player.Character.Armor = 100; World.CarDensity = 0; World.PedDensity = 0; //I need to delete cops and peds close to the player when this is executed } public void healthRegen(ref bool healthRegen) { Main main = new Main(); healthRegen = true; for (int health = Player.Character.Health; health <= 100; health+=2) { Player.Character.Health = health; Wait(500); } healthRegen = false; } public void enemySpawn(ref bool spawning) //WORKS { //Main.<mainTick>.Start(); spawning = true; Random rnd = new Random(); int killsNeededToFinish = rnd.Next(50, 100); enemyArray = new Ped[killsNeededToFinish]; for(int i = 0; i < killsNeededToFinish; i++) { Vector3 playerPos = World.GetNextPositionOnStreet(Player.Character.Position.Around(100.0F)); enemyArray[i] = World.CreatePed("M_M_ENFORCER", playerPos, RelationshipGroup.Criminal); if (i == 0) enemyGroup = new Group(enemyArray[i]); else changeAI(enemyArray[i]); Wait(rnd.Next(1500, 7000)); /*if (!Exists(enemyArray[oldLeader])) { enemyGroup = new Group(enemyArray[i]); oldLeader = i; }*/ } } public void changeAI(Ped e) { enBlip = e.AttachBlip(); e.SetPathfinding(true, true, true); e.ChangeRelationship(RelationshipGroup.Player, Relationship.Hate); e.AlwaysDiesOnLowHealth = true; e.RandomizeOutfit(); e.CowerInsteadOfFleeing = true; e.Task.FightAgainst(Player.Character); Weapon weapon = Weapon.SMG_MP5; e.Weapons.FromType(weapon).Ammo = 500; e.Weapons.Select(weapon); e.Task.GoTo(Player.Character); enemyGroup.AddMember(e); } }} The ><'s where just an example to show that that is where you put the name of your tick event. Take those out so it looks like Main.mainTick.Start();. If you don't want to stop the timer when something is happening within that said timer, then just set a boolean to true when it is processing stuff in the timer, and false when it gets to the end of the tick event. Then, when you are stopping the timer, just do if (!boolean) Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 Uhm, I don't understand Also, main.mainTick.Start(); doesn't work because it says it's not a valid construct. Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 (edited) Uhm, I don't understand Also, main.mainTick.Start(); doesn't work because it says it's not a valid construct. The class has to be public and the variable has to be public and static. It should look like this: public class ClassNameHere : Script{ public static GTA.Timer mainTick;} Just look at this: http://social.msdn.microsoft.com/Forums/en-US/a3bcf89f-2ce0-42b8-a6b5-d47e3ea63bd2/how-to-global-a-variable-in-c?forum=csharpgeneral Edited June 23, 2014 by LetsPlayOrDy Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 I tried to inform myself about GTA.Timer and found an old topic in this forum, I added a second timer but the game crashes when it's done loading :| I don't know if this is what you meant public class Main : Script { Functions functions = new Functions(); bool inRampage = false; bool healthIsBeingAdded = false; bool spawningEnemies = false; int killCounter = 0; GTA.Timer secondTick; Weapon weapon = Weapon.Rifle_AK47; public Main() { Interval = 250; this.Tick += new EventHandler(mainTick); secondTick = new GTA.Timer(1000); secondTick.Tick += secondaryTick; secondTick.Start(); this.KeyDown += new GTA.KeyEventHandler(keyTick); } public void mainTick(object sender, EventArgs e) { Game.DisplayText(Convert.ToString(killCounter)); if(inRampage) { Player.Character.Weapons.FromType(weapon).Ammo = 500; if (Player.Character.Weapons.Current != weapon) Player.Character.Weapons.Select(weapon); if (Player.Character.Health <= 50 && !healthIsBeingAdded) functions.healthRegen(ref healthIsBeingAdded); Player.WantedLevel--; foreach(Ped peds in Functions.enemyGroup) { if (peds.Health < 1) { Game.DisplayText("One is dead!"); Functions.enBlip.Delete(); killCounter++; } if (Functions.enemyGroup.MemberCount < 1) Functions.enemyArray[0].Delete(); } } } public void secondaryTick(object sender, EventArgs e) { if (!spawningEnemies) functions.enemySpawn(ref spawningEnemies); } public void keyTick(object sender, GTA.KeyEventArgs e) { if(e.Key == Keys.End) { functions.rampageSettings(); inRampage = true; } } }} Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 (edited) I tried to inform myself about GTA.Timer and found an old topic in this forum, I added a second timer but the game crashes when it's done loading :| I don't know if this is what you meant public class Main : Script { Functions functions = new Functions(); bool inRampage = false; bool healthIsBeingAdded = false; bool spawningEnemies = false; int killCounter = 0; GTA.Timer secondTick; Weapon weapon = Weapon.Rifle_AK47; public Main() { Interval = 250; this.Tick += new EventHandler(mainTick); secondTick = new GTA.Timer(1000); secondTick.Tick += secondaryTick; secondTick.Start(); this.KeyDown += new GTA.KeyEventHandler(keyTick); } public void mainTick(object sender, EventArgs e) { Game.DisplayText(Convert.ToString(killCounter)); if(inRampage) { Player.Character.Weapons.FromType(weapon).Ammo = 500; if (Player.Character.Weapons.Current != weapon) Player.Character.Weapons.Select(weapon); if (Player.Character.Health <= 50 && !healthIsBeingAdded) functions.healthRegen(ref healthIsBeingAdded); Player.WantedLevel--; foreach(Ped peds in Functions.enemyGroup) { if (peds.Health < 1) { Game.DisplayText("One is dead!"); Functions.enBlip.Delete(); killCounter++; } if (Functions.enemyGroup.MemberCount < 1) Functions.enemyArray[0].Delete(); } } } public void secondaryTick(object sender, EventArgs e) { if (!spawningEnemies) functions.enemySpawn(ref spawningEnemies); } public void keyTick(object sender, GTA.KeyEventArgs e) { if(e.Key == Keys.End) { functions.rampageSettings(); inRampage = true; } } }} I don't think you can access Interval = 250;this.Tick += new EventHandler(mainTick); this timer from another file, but you can access GTA.Timer secondTick; this one from another file if you change it to public static GTA.Timer secondTick; Edited June 23, 2014 by LetsPlayOrDy Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 Sorry I made confusion. I found the problem of my script, why the lines after the enemySpawn method is called are not being executed, since when a method is called the CPU jumps to that code the tick is "paused" if we can say it that way and I want to add a second one so the enemies keep spawning and the main tick is not paused and it checks everything that needs to be checked but if I use that second timer/tick the game crashes as soon as it's done loading. I tried the windows forms timer and it's the same thing Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 Sorry I made confusion. I found the problem of my script, why the lines after the enemySpawn method is called are not being executed, since when a method is called the CPU jumps to that code the tick is "paused" if we can say it that way and I want to add a second one so the enemies keep spawning and the main tick is not paused and it checks everything that needs to be checked but if I use that second timer/tick the game crashes as soon as it's done loading. I tried the windows forms timer and it's the same thing I don't know what the problem is Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 Aww, I will keep playing around with the timer then. Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 (edited) There was an error in the second class that I managed to fix, that's why it was crashing Anyway, are timers performance eaters? EDIT: Woops, double post, sorry! Edited June 23, 2014 by Noox Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 There was an error in the second class that I managed to fix, that's why it was crashing Anyway, are timers performance eaters? EDIT: Woops, double post, sorry! Are timers performance eaters? Depends on what's in the tick event I guess. Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 (edited) Anyway the first tick stops even if there is the second one, are there any ways to execute part of a script in a second thread? Edited June 23, 2014 by Noox Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 (edited) Anyway the first tick stops even if there is the second one, are there any ways to execute part of a script in a second thread? I don't think that is possible. Just change the one timer to a GTA.Timer and then you can access it from anywhere. Edited June 23, 2014 by LetsPlayOrDy Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 Even if I use Main.mainTick.Start() inside the method when it's called the main tick stops being executed, I have to start another mod I guess :| Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 23, 2014 Share Posted June 23, 2014 Noox im trying to read what ur doing but this conversation is going all over... paste your code on pastebin using c# syntax highlighter (make sure u have Visual studio format it first, u can do this by going to the end of the file... deleting the last } and then typing it again... unless u disabled this of course) Then tell me in english, not code talk what u need to happen... and ill tell u how to fix. Link to comment Share on other sites More sharing options...
Noox Posted June 23, 2014 Author Share Posted June 23, 2014 Noox im trying to read what ur doing but this conversation is going all over... paste your code on pastebin using c# syntax highlighter (make sure u have Visual studio format it first, u can do this by going to the end of the file... deleting the last } and then typing it again... unless u disabled this of course) Then tell me in english, not code talk what u need to happen... and ill tell u how to fix. Sorry but I'm not good at explaining Anyway I managed to fix by putting everything in the main tick, the code itself is a mess and hard to read but it works. Thanks anyway. Link to comment Share on other sites More sharing options...
LordOfTheBongs Posted June 23, 2014 Share Posted June 23, 2014 if it works it works, that's all that matters... ur not writing a book for someone to read Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now