Multiple triggers to start an effect at multiple origins.

Have questions about CoD4 mapping that aren't covered in the tutorials section? Post here!

Moderator: Core Staff

roxsman
CJ Wannabe
CJ Wannabe
Posts: 12
Joined: October 14th, 2011, 1:44 pm
Location: Arnhem, Netherlands

Multiple triggers to start an effect at multiple origins.

Post by roxsman » October 14th, 2011, 2:37 pm

Hello everybody,

Working on a level and I want to achieve an effect which isn't working out the way I want to.

Desired effect: Throughout the level are multiple buttons (trigger_use_touch), when one of them is triggered, an effect should be played at multiple origins. To give you an idea: the buttons are fire alarm triggers and I have multiple red lights around the map. I have configured an effect that resembles the flashing light of a fire alarm. I've also defined a sound in a soundalias file that should be played during the effect. Both the FX (flashing light) as well as the sound file are approx 20 secs long and after the 20 seconds the effect should stop (no looped effect). I've put a delay on the trigger_use_touch itself so you can trigger it again after 60 secs.
All script_origins have targetname fire_orig and all triggers have targetname fire_trig so I guess they should both go in an array but my scripting knowledge just isn't sufficient to get it to work.

Have been scavenging forums for some code to get me in the right direction and I've found some but not enough to make it happen. I have a pretty strong feeling that it SHOULD be possible.. now I just need to find out how :)

If I should different targetnames I would be glad to hear but I kinda went with that because of some other forum posts I read.

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Multiple triggers to start an effect at multiple origins

Post by Drofder2004 » October 14th, 2011, 4:26 pm

Code: Select all

main()
{
   level.alarm = false;
   thread alarmTrigThink();
   thread alarms();
}
 
alarmTrigThink()
{
   fireAlarms = getEntArray("fire_trig","targetname");
 
   for(i=0;i<fireAlarms;i++)
      fireAlarms[i] alarmTrig();
}
 
alarmTrig()
{
   while(1)
   {
      self waittill("trigger");   // Wait for alarm to be touched
      level notify("toggleAlarm"); // Send event to turn on/off alarm
      thread AutoOff(); // Function to turn off alarm after set time
      wait 30; // This alarm is disable for 30 seconds
   }
}
 
alarms()
{
   fireAlarmOrigs = getEntArray("fire_orig","targetname");
 
   while(1)
   {
      level waittill("toggleAlarm");  // Wait for event ON
      level.alarm = true;
 
      for(i=0;i<fireAlarmOrigs.size;i++)
         fireAlarmOrigs[i] turnOn();  // Turn on the alarm
 
      level waittill("toggleAlarm");  // Wait for event OFF
      level.alarm = false;
 
      for(i=0;i<fireAlarmOrigs.size;i++)
         fireAlarmOrigs[i] turnOff();  // Turn off the alarm
   }
}
 
autoOff()
{
   level endon("toggleAlarm"); // AutoOff stops when alarm is manually triggered
   if(level.alarm)
   {
      wait 60; // auto turn off alarm at 60 seconds
      level notify("toggleAlarm");
   }
}
      
turnOn()
{
   // Place your 'playFx' and 'soundFx' stuff here for alarms being on.
}
 
 
turnOff()
{
   // Place your 'playFx' and 'soundFx' stuff here for alarms being off.
}
Havent tested code so may be some errors, but that is the basics of it...
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

roxsman
CJ Wannabe
CJ Wannabe
Posts: 12
Joined: October 14th, 2011, 1:44 pm
Location: Arnhem, Netherlands

Re: Multiple triggers to start an effect at multiple origins

Post by roxsman » October 14th, 2011, 5:06 pm

Holy crap.. didn't expect THIS much help this fast :) Thanks a lot!! I'm on the road right now but as soon as I can position myself behind my computer I'm gonna give this a shot!

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: Multiple triggers to start an effect at multiple origins

Post by IzNoGoD » October 14th, 2011, 6:13 pm

Or, even shorter and without those damn notifies:

Code: Select all

 
main()
{
        level.alarmfx=loadfx("fx goes here");
        level.alarmsound="soundalias goes here";
        level.armed=true;
        thread alarmTrigsetup();
}
 
alarmTrigsetup()
{
        fireAlarms = getEntArray("fire_trig","targetname");
        for(i=0;i<fireAlarms;i++)
                fireAlarms[i] alarmTrig();
}
 
alarmTrig()
{
        while(true)
        {
                self waittill("trigger");
                if(level.armed)
                {
                        level.armed=false;
                        turnonalarms();
                        wait 60;
                        level.armed=true;
                }
        }
}
 
turnonalarms()
{
        locations=getentarray("fire_orig","targetname");
        for(i=0;i<locations.size;i++)
        {
                playfx(level.alarmfx,locations[i].origin);
                locations[i] playsound(level.alarmsound);
        }
}
LMGTFY!

Its not a glitch... Its the future!

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Multiple triggers to start an effect at multiple origins

Post by Drofder2004 » October 15th, 2011, 2:12 am

"damn notifies"?
Notify's are the essence of event based programming, why you would discredit their use is beyond me...

My code is also slightly wrong, I must admit, I interpreted the idea as the 'switch' for on and off, so my code would theoretically not work as intended. Use the code provided by IzNoGod (and if you need a manual override switch, feel free to enjoy the easy use notify system :lol: )
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: Multiple triggers to start an effect at multiple origins

Post by IzNoGoD » October 15th, 2011, 12:36 pm

Drofder2004 wrote:"damn notifies"?
Notify's are the essence of event based programming, why you would discredit their use is beyond me...
[offtopic]
I know, I love notifies. It's just that for beginners, it's damn hard to understand how they work. They are also quite a heavy load for the game, so that's why I called them "damn notifies"[/offtopic]
LMGTFY!

Its not a glitch... Its the future!

roxsman
CJ Wannabe
CJ Wannabe
Posts: 12
Joined: October 14th, 2011, 1:44 pm
Location: Arnhem, Netherlands

Re: Multiple triggers to start an effect at multiple origins

Post by roxsman » October 16th, 2011, 10:20 am

Something else that you guys are probably able to shed some light on.. I made a new gsc for this script and called it from the mp_mapname.gsc. Now what has struck me before is that it matters in which order you call the scripts (like putting it before the _load or after for instance). I've also used the thread function sometimes in the mp_mapname.gsc and then it also seems to matter. In some forums I read to put all your custom stuff before the _load and on some other I read that it all should be placed after the _load.

Please enlighten me :)

And to give you an update: I haven't been able to work on it a lot yet.. I went with IzNoGod's code but it hasn't worked so far, tho I must admit I haven't been able to troubleshoot yet (that's also why I asked the question above)..

Anyways, i've put a iprintlnbold in the triggerpart (after level.armed=false) to rule out any fx mistakes on my part but whenever I trigger the buttons nothing happens.

Probably will be able to work on it later today..

IzNoGoD
CJ Worshipper
CJ Worshipper
Posts: 343
Joined: January 6th, 2009, 8:39 pm
Location: Netherlands/Holland

Re: Multiple triggers to start an effect at multiple origins

Post by IzNoGoD » October 16th, 2011, 10:50 am

LMGTFY!

Its not a glitch... Its the future!

megazor
CJ Worshipper
CJ Worshipper
Posts: 414
Joined: July 22nd, 2009, 3:02 am
Location: Russia, Vladivostok

Re: Multiple triggers to start an effect at multiple origins

Post by megazor » October 16th, 2011, 12:54 pm

iznogod died

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Multiple triggers to start an effect at multiple origins

Post by Drofder2004 » October 16th, 2011, 2:02 pm

That doesn't answer the question of 'order of events', that only answers the question of how threading affects the order of events. Simply put, the "_load" is not a threaded function.
megazor wrote:iznogod died
Right. :roll:

Code: Select all

main()
{
        /* FX Function */
        /* Art Function */
        /* _Load::main */
 
        /* _compass::setUpMinimap */
 
        /* *THREAD* YOUR FUNCTION HERE */
 
        /* setExpFog */
        /* VisionSetNaked */
        /* ambientPlay */
 
        /* game[] variables */
 
        /* map specific dvars */
        /* compass range dvar */
}
The _load function will load many additional functions, such as minefields, destructilbles, FX and importantly during map development, this code will pause the entire code to generate the reflection probes.

Stick to the order above, you do not need to put all of them in, but if you do use any, stay in that order. Also, to make you life easier, instead of placing ALL of your 'main' functions in there, place a single function that loads all of your own functions.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

roxsman
CJ Wannabe
CJ Wannabe
Posts: 12
Joined: October 14th, 2011, 1:44 pm
Location: Arnhem, Netherlands

Re: Multiple triggers to start an effect at multiple origins

Post by roxsman » October 17th, 2011, 2:10 pm

Well, I'll be damned! Yesterday evening I made an immense reply with al snippets of code I tried, all errors, etc. and the site timed out after clicking submit, Back didn't work so I guess I gotta revert to CTRL-C'ing my posts before posting.
First off I noticed one small mistake in IzNoGod's code: for(i=0;i<fireAlarms;i++) should have fireAlarm.size instead. After I fixed that I stopped getting errors and then the rest of the script seemed to work as well except for the most important thing: the effect and sound did not play.
After trying a whole bunch of things without success I thought it would probably be better to start with ONE trigger and ONE origin and get that to work before starting out with the big guns..
So I made a small testlevel with just one origin, one trigger and customized the script and then it worked!! I put in more origins and switched to getentarray and when using the trigger the effect & sound played at all origins!! Victory is near!! So I figured it wouldn't be a problem to switch to multiple triggers as well but that part still isn't working.
I made two triggers in a level (left & right) and two origins (left & right). When I press either trigger, both origins should play the effect. But it only works on the right trigger. Both triggers have the same targetname (alarm_trig) and target (alarm_orig). When I press the left trigger nothing happens.


This is the code I'm using now:

Code: Select all

main()
{
	level.alarmfx = loadfx("misc/fire_alarm_light");
	level.alarm_sound = "jb_fire_alarm";
	level.armed=true;
	thread alarmTrigsetup();
}
	
alarmTrigsetup()
{
        fireAlarms = getEntArray("alarm_trig","targetname");
        for(i=0;i<fireAlarms.size;i++)
                fireAlarms[i] alarmTrig();
}

alarmTrig()
{
        while(true)
        {
                self waittill("trigger");
	if(level.armed)
		{
		level.armed=false;
		turnonalarms();
		wait 45;
		level.armed=true;
		}
        }
}


turnonalarms()
{
	o = getEntArray("alarm_orig","targetname");
	for(i=0;i<o.size;i++)
		{
		playfx(level.alarmfx, o[i].origin);
		o[i] playsound (level.alarm_sound);
		}
}
Any ideas about why only one trigger is working?

User avatar
Rezil
Core Staff
Core Staff
Posts: 2030
Joined: July 24th, 2006, 11:21 am
Location: Cramped in a small cubicle/making another jump map

Re: Multiple triggers to start an effect at multiple origins

Post by Rezil » October 17th, 2011, 3:25 pm

Just making sure, but you do realize you have to wait 45 seconds(in your case) before you can use either trigger again right? I would also suggest printing debugging lines in all of your functions so you know exactly where the problem is.
Drofder2004: Drofder's rules for reviewing a map
[...]
#5 If your name is Rezil, minimum 5/5.
---
<LT>YosemiteSam[NL]:
I heard somewhere that the best way to start is juggling 2 balls with one hand, so you will get a feel for it.

roxsman
CJ Wannabe
CJ Wannabe
Posts: 12
Joined: October 14th, 2011, 1:44 pm
Location: Arnhem, Netherlands

Re: Multiple triggers to start an effect at multiple origins

Post by roxsman » October 17th, 2011, 4:08 pm

Yeah those 45 secs were put in deliberately to prevent someone from flooding the alarm. So after 45 secs I can trigger the right trigger again but the left one doesn't do anything. It's also not an "order-thing" cuz if I try the left one just after the level starts nothing happens either, while the right one works perfectly.. I guess only the one with the lowest entity number is working but that's only an assumption, haven't checked that out yet..
Signatures? We don't need no steenking signatures!!

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Multiple triggers to start an effect at multiple origins

Post by Drofder2004 » October 18th, 2011, 12:17 am

Code: Select all

alarmTrigsetup()
{
        fireAlarms = getEntArray("alarm_trig","targetname");
        for(i=0;i<fireAlarms.size;i++)
                fireAlarms[i] thread alarmTrig();
}
Thread was missing, code was pausing. Problem should now be resolved.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

megazor
CJ Worshipper
CJ Worshipper
Posts: 414
Joined: July 22nd, 2009, 3:02 am
Location: Russia, Vladivostok

Re: Multiple triggers to start an effect at multiple origins

Post by megazor » October 18th, 2011, 5:51 am

Megazor, Doctor of Scripts, agrees.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests