Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

How do you make a TimerTrigger call a function inside of a Class

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Post Reply
User avatar
pillers
Meatbag
Posts: 7
Joined: Sun Dec 02, 2018 10:11 am
Title: Deluxe Superfighter
Age: 25

How do you make a TimerTrigger call a function inside of a Class

Post by pillers » Thu Jan 24, 2019 6:12 am

I'm creating a TimerTrigger inside of one of my Class functions. I want this Timer to call another function within the same Class, however I get this script error.
Image
Here is the piece of code that im running

Code: Select all

public class McCree {
	public IPlayer ply;
	public HandgunWeaponItem Peacekeeper;
	public IObjectTimerTrigger FTHTrigger;
	public bool FTHEnabled = true;
	public bool CombatRollEnabled = true;
	
	public McCree(IPlayer ply) {
		this.ply = ply;
		Peacekeeper = this.ply.CurrentSecondaryWeapon;
	}

	public void Update() {
		if (this.ply.IsRolling) {
			CombatRoll();
		}
		if (this.ply.IsCrouching && FTHEnabled==true && this.ply.CurrentWeaponDrawn == WeaponItemType.Handgun) {
			IObjectTimerTrigger FTHTrigger = (IObjectTimerTrigger)Game.CreateObject("TimerTrigger");
			FTHTrigger.SetIntervalTime(100);
			FTHTrigger.SetRepeatCount(Peacekeeper.CurrentAmmo);
			FTHTrigger.SetScriptMethod("FanTheHammer");
			FTHTrigger.Trigger();
			FTHEnabled = false;
		}
	}
	
	public void FanTheHammer(TriggerArgs args) {
		Vector2 dir = new Vector2(  (this.ply.GetWorldPosition().X+50) * (this.ply.FacingDirection) , (this.ply.GetWorldPosition().Y+RANDOM.Next(-15,15)));
		Vector2 pos = new Vector2(  (this.ply.GetWorldPosition().X + (7*this.ply.FacingDirection)) , this.ply.GetWorldPosition().Y+10);
		Game.SpawnProjectile(ProjectileItem.REVOLVER, pos, dir);
	}
	public void CombatRoll() {
		this.ply.GiveWeaponItem(WeaponItem.REVOLVER);
	}
}
The function FanTheHammer should just unload the remaining rounds left inside of the revolvers cylinder in quick succession I want to use a timer because it would otherwise be way too fast and unload all the bullets in the same frame.Is there an easier way of doing this? because I might be going about this in a very convoluted way. I would appreciate any help whatsoever thank you♥
0 x

Code: Select all

while true

User avatar
JakSparro98
Superfighter
Superfighter
Posts: 530
Joined: Fri Jul 15, 2016 7:56 pm
Started SFD: PreAlpha 1.0.5
Location: Rome, Italy
Gender:
Age: 25

Post by JakSparro98 » Fri Jan 25, 2019 10:32 pm

pillers wrote:
Thu Jan 24, 2019 6:12 am
I'm creating a TimerTrigger inside of one of my Class functions. I want this Timer to call another function within the same Class, however I get this script error.
Image
[..]
You get this error with the function because you declared it inside "another" class, why am I saying another? you might ask.

Just think of the script section as a virtual big class that is were all the code is placed

Code: Select all

class GameScript
    {


        /* ALL THE CODE PUT IN THE SCRIPT EDITOR WINDOW */


    }
When you want to use auto-starting functions (OnStartup, AfterStartup, ect...) or triggerable functions they have to be declared on the "base" class and not being encapsulated inside other.
0 x

User avatar
pillers
Meatbag
Posts: 7
Joined: Sun Dec 02, 2018 10:11 am
Title: Deluxe Superfighter
Age: 25

Post by pillers » Sat Jan 26, 2019 3:18 am

yup... that seems to be the case. So whats the work around to this because obviously I can't have my FanTheHammer function outside of my Class since i wont have access to my Class variables. Is there actually no way to call my class functions with a TimerTrigger? I've tried setting the script method to "McCree.FanTheHammer" but that doesn't seem to work either. any suggestions for a fix?
0 x

Code: Select all

while true

User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1884
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 34

Post by Gurt » Sat Jan 26, 2019 9:58 am

The simplest way is to use the Events.UpdateCallback function. Example:

Code: Select all

private class MyInnerClass
{
	public int Counter = 0;
}

public void OnStartup()
{
	MyInnerClass myClass = new MyInnerClass();
	Events.UpdateCallback.Start((float t) => {
		myClass.Counter += 1;
		Game.ShowPopupMessage(myClass.Counter.ToString());
	}, 100, 5); // Calls each 100 ms 5 times before stopping
}
Or using it inside a class directly:

Code: Select all

private class MyInnerClass
{
	public int Counter = 0;

	public void StartCounter()
	{
		Events.UpdateCallback.Start((float t) => {
   		Counter += 1;
		   Game.ShowPopupMessage(Counter.ToString());
	   }, 100, 5); // Calls each 100 ms 5 times before stopping
	}
}

public void OnStartup()
{
	MyInnerClass myClass = new MyInnerClass();
        myClass.StartCounter();
}
0 x
Gurt

Post Reply