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.

[Help 2] How to make function sleep for a period amount of time?

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Post Reply
User avatar
Pixir
Fighter
Fighter
Posts: 14
Joined: Fri Aug 18, 2017 3:04 pm
SFD Account: Pixir
SFD Alias: Pixie Dust
Location: Romania
Gender:
Age: 17

[Help 2] How to make function sleep for a period amount of time?

Post by Pixir » Thu Feb 08, 2018 1:11 am

Code: Select all

public void Spawn(TriggerArgs args){
	Vector2 worldPosition = Vector2.Zero;
	Random rnd = new Random(); 
	int y = rnd.Next(-200,200);
	int x = rnd.Next(-200,200);
	
	IObject Crate = (IObject)Game.CreateObject("HangingCrateHolder",new Vector2(y,x), 0f);
	Crate.SetBodyType(BodyType.Static);
	IObjectOnDestroyedTrigger Trigger = (IObjectOnDestroyedTrigger)Game.CreateObject("OnDestroyedTrigger");
	Trigger.SetTriggerDestroyObjects(Crate);
	Score();

}
I'm trying to make a script that spawns random HangingCrateHolder(s) and detects when they were destroyed but it doesn't seem to work

Code: Select all

CS1502 The best overloaded method match for 'SFDGameScriptInterface.IObjectOnDestroyedTrigger.SetTriggerDestroyObjects(System.Collections.Generic.IEnumerable<SFDGameScriptInterface.IObject>)' has some invalid arguments @(26, 2) Trigger.SetTriggerDestroyObjects(Crate);

CS1503 Argument 1: cannot convert from 'SFDGameScriptInterface.IObject' to 'System.Collections.Generic.IEnumerable<SFDGameScriptInterface.IObject>' @(26, 35) Trigger.SetTriggerDestroyObjects(Crate);

Any help is appreciated
Last edited by Pixir on Thu Feb 08, 2018 5:58 pm, edited 2 times in total.
0 x

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

Post by JakSparro98 » Thu Feb 08, 2018 11:31 am

Pixir wrote:
Thu Feb 08, 2018 1:11 am

Code: Select all

public void Spawn(TriggerArgs args){
	Vector2 worldPosition = Vector2.Zero;
	Random rnd = new Random(); 
	int y = rnd.Next(-200,200);
	int x = rnd.Next(-200,200);
	
	IObject Crate = (IObject)Game.CreateObject("HangingCrateHolder",new Vector2(y,x), 0f);
	Crate.SetBodyType(BodyType.Static);
	IObjectOnDestroyedTrigger Trigger = (IObjectOnDestroyedTrigger)Game.CreateObject("OnDestroyedTrigger");
	Trigger.SetTriggerDestroyObjects(Crate);
	Score();

}
I'm trying to make a script that spawns random HangingCrateHolder(s) and detects when they were destroyed but it doesn't seem to work

Code: Select all

CS1502 The best overloaded method match for 'SFDGameScriptInterface.IObjectOnDestroyedTrigger.SetTriggerDestroyObjects(System.Collections.Generic.IEnumerable<SFDGameScriptInterface.IObject>)' has some invalid arguments @(26, 2) Trigger.SetTriggerDestroyObjects(Crate);

CS1503 Argument 1: cannot convert from 'SFDGameScriptInterface.IObject' to 'System.Collections.Generic.IEnumerable<SFDGameScriptInterface.IObject>' @(26, 35) Trigger.SetTriggerDestroyObjects(Crate);

Any help is appreciated
Since the method accepts Generic Collections and arrays you only need to include the object inside one them:

Code: Select all

//Using arrays
IObject[] Objects=new IObject[1];
Objects[0]=Crate;
Trigger.SetTriggerDestroyObjects(Objects);
or

Code: Select all

//Using lists
List<IObject> Objects = new List<IObject>();
Objects.Add(Crate);
Trigger.SetTriggerDestroyObjects(Objects);
0 x

User avatar
Pixir
Fighter
Fighter
Posts: 14
Joined: Fri Aug 18, 2017 3:04 pm
SFD Account: Pixir
SFD Alias: Pixie Dust
Location: Romania
Gender:
Age: 17

Post by Pixir » Thu Feb 08, 2018 12:44 pm

Thank you it worked :D
0 x

User avatar
Pixir
Fighter
Fighter
Posts: 14
Joined: Fri Aug 18, 2017 3:04 pm
SFD Account: Pixir
SFD Alias: Pixie Dust
Location: Romania
Gender:
Age: 17

Post by Pixir » Thu Feb 08, 2018 5:57 pm

Code: Select all

public void Spawn(TriggerArgs args){
	Vector2 worldPosition = Vector2.Zero;
	Random rnd = new Random(); 
	int y = rnd.Next(-200,200);
	int x = rnd.Next(-200,200);
	
	IObject Crate = (IObject)Game.CreateObject("HangingCrateHolder",new Vector2(y,x), 0f);
	Crate.SetBodyType(BodyType.Static);
	Crate.CustomID ="Aim";
	IObjectOnDestroyedTrigger Trigger = (IObjectOnDestroyedTrigger)Game.CreateObject("OnDestroyedTrigger");
	IObject[] Objects=new IObject[1];
	Objects[0]=Crate;
	Trigger.SetTriggerDestroyObjects(Objects);
	Trigger.CustomID="Aim5";
	Trigger.SetScriptMethod("ScoreUp");
	Score();
	IObjectTimerTrigger Timer = (IObjectTimerTrigger)Game.CreateObject("TimerTrigger");
	Timer.SetIntervalTime(3000);
	Timer.SetRepeatCount(1);
	Timer.SetScriptMethod(Delete(Crate,Trigger));
	Timer.Trigger();
	
}

public void Delete(IObject Crate,IObjectOnDestroyedTrigger Trigger){
Trigger.Destroy();
Crate.Remove();
}
How to make the function to wait for 2000ms and then delete the trigger and the HangingCrateHolder?
Any help is appreciated
0 x

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

Post by Gurt » Thu Feb 08, 2018 7:42 pm

Do a "fire and forget" update event.

Code: Select all

Events.UpdateCallback.Start((float e) => { yourTrigger.Remove(); }, 2000, 1);
First parameter is an Action<float>, second the minimum game time to elapse in milliseconds (2000 for 2 seconds) and the third is the repeat count. Set it to 1 to only trigger the action once.
1 x
Gurt

User avatar
Pixir
Fighter
Fighter
Posts: 14
Joined: Fri Aug 18, 2017 3:04 pm
SFD Account: Pixir
SFD Alias: Pixie Dust
Location: Romania
Gender:
Age: 17

Post by Pixir » Fri Feb 09, 2018 10:57 am

Thank you it worked :D
0 x

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests