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.

PlayerSpawnTrigger Trigger Limit on for loops?

Here you can find answered ScriptAPI topics.
Forum rules
By using the forum you agree to the following rules.
Post Reply
User avatar
Mr Argon
Fighter
Fighter
Posts: 56
Joined: Sat Mar 09, 2019 2:22 am
SFD Account: Argón (steam)
SFD Alias: Mr. Argon
Started SFD: Pre-Alpha 1.8.2c
Location: Argentina
Gender:
Age: 20

PlayerSpawnTrigger Trigger Limit on for loops?

Post by Mr Argon » Sun Feb 16, 2020 12:55 am

Well, I don't know if I must post this here or in the Bugs Topic. Anyways, I'm experiencing something really confusing. I'm using a for loop to make a PlayerSpawnTrigger spawn certain number of enemies depending on circunstances, but I can't figure out why it always spawns two, like it's the limit. Here's some relative code.
public void Spawn(TriggerArgs args)
{
     IObjectPlayerSpawnTrigger playerspawn = Game.GetSingleObjectByCustomID("enemy")as IObjectPlayerSpawnTrigger;
     for (int i = 0; i < 6; i++)
     {
          playerspawn.Trigger();
     }
}
     
this code always spawns the enemy two times, I thought there was a limit, but then I tried this...
public void Spawn(TriggerArgs args)
{
     IObjectPlayerSpawnTrigger playerspawn = Game.GetSingleObjectByCustomID("enemy")as IObjectPlayerSpawnTrigger;
     while (true)
     {
          playerspawn.Trigger();
     }
}
     
the previous code crashed the game. Obviously because there was too many players to spawn. I tried doing the equivalent to the for loop using a while loop, and I got exactly the same result: two players being spawned.
0 x
I'м д Liттlэ оdd... sтill саи livе шiтн тнат.

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 » Sun Feb 16, 2020 7:44 pm

Triggers can only be triggered once per update. When that's done any additional times a trigger is activated the same update it will be put on hold to the next update once. That's why you see it only spawning a player twice. Once this update and once next update. This is by design to avoid cyclic triggers getting stuck in an infinite loop, crashing the game.

What you want to do from code is to call IObjectPlayerSpawnTrigger.CreatePlayer(); function instead if you only care about creating the player (this doesn't count as activating the trigger). If you really need the IObjectPlayerSpawnTrigger.Trigger() function to be run you should try to split it up and only call it once every update.
0 x
Gurt

User avatar
Mr Argon
Fighter
Fighter
Posts: 56
Joined: Sat Mar 09, 2019 2:22 am
SFD Account: Argón (steam)
SFD Alias: Mr. Argon
Started SFD: Pre-Alpha 1.8.2c
Location: Argentina
Gender:
Age: 20

Post by Mr Argon » Sun Feb 16, 2020 7:57 pm

Thanks for the information! I don't really need the Trigger() function. I managed to solve it by using a Timer Trigger set to 100ms and a repeat count set to the number of players it should spawn. I'll try with the createplayer as well, maybe when I need to simply create a player instead of an entire queue for a survival style map
0 x
I'м д Liттlэ оdd... sтill саи livе шiтн тнат.

Post Reply