Page 1 of 3

Array help

Posted: August 14th, 2010, 5:34 am
by Major Tom
So I recently started working with arrays and I have a question about them.
How do I get trigger 1 to open door 1 with out having to put the numbers in?
Any help would be appreciated.

Re: Array help

Posted: August 14th, 2010, 6:58 am
by megazor
say u have 10 triggers and 10 doors named respectively - so trig1 is to open door1, trig2 is to open door2 and so on.

the code below lets the ten triggers open the corresponding doors (in the example, the doors move upstairs)

Code: Select all

main()
{
	for (i = 1; i < 11; i++)
		thread openMe(i);
}

openMe(i)
{
	door = getEnt("door"+i, "targetname");
	trig = getEnt("trig"+i, "targetname");
	
	while (1)
	{
		trig waittill("trigger");
		door moveZ(100, 1);
	}
}

Re: Array help

Posted: August 14th, 2010, 1:08 pm
by Drofder2004
Thats not an array, thats just scripting.

You can do the following.

Make your doors.
Give them a targetname of simply "door".

Now make your triggers.
(Do not give them a targetname)

Next select your first door, then select your first trigger and press W to link them.
Do this for all combinations, making sure to select the DOOR first.

Now

Code: Select all

main()
{
   doors = getentarray("door", "targetname");
   for(i=0li<doors.size;i++)
      doors[i] thread doorThink();
}

doorThink()
{
   trig = self.target;
   
   while(1)
   {
      trig waittill("trigger");
      // Start opening door etc
   }
}
The only problem with this code is making the door rotate in the correct direction.
There are several ways you can do this...

1. Make sure all doors and hinges are in the same place, so that rotating 90 degrees will always work.
2. Make different arrays for doors that rotate different directions
3. Use the script_noteworthy attribute to assign the doors a value to which you can recall in the scripts, and then coding an "if" or "switch" statement...
4. Using a script origin linked to the door and scripting the door to move to the origin and back...

Re: Array help

Posted: August 14th, 2010, 1:14 pm
by megazor
in fact, is there a difference between these ways (in-game)?

Re: Array help

Posted: August 14th, 2010, 1:20 pm
by megazor
brushmodels have their own set of angles, so rotating should work. u can change their angles so that they fit to the necessary direction.

example:

key: angles
value: 0 90 0

Re: Array help

Posted: August 14th, 2010, 3:52 pm
by Drofder2004
megazor wrote:in fact, is there a difference between these ways (in-game)?
Yes, the use of an array gives you more options if you wish to expand on the idea.
megazor wrote:brushmodels have their own set of angles, so rotating should work. u can change their angles so that they fit to the necessary direction.

example:

key: angles
value: 0 90 0
Although this is true, you do not always wish to rotate the door clockwise, so the doors that you wish to open anti-clockwise, you would need to change the code.

Re: Array help

Posted: August 14th, 2010, 9:01 pm
by Major Tom
I'll have to try that, thanks.

Re: Array help

Posted: August 15th, 2010, 1:26 am
by megazor
gives you more options
for example:

Re: Array help

Posted: August 15th, 2010, 9:39 am
by Drofder2004
megazor wrote:
gives you more options
for example:
array.size = No need for me to count my entities.
Prefab = No need for me to name every individual script object in the map, I also do not need to worry about how many I use.
Reduced human error = What happens when you accidently skip a number while making the 34th door on your map?


Go create a map with 50 doors and 50 triggers and I will do the same, see who finishes first?
- You make 100 different individual entities
- I make 1 entity and link the trigger, prefab, copy and paste 50 times.

Re: Array help

Posted: August 15th, 2010, 1:38 pm
by megazor
well the things u enumerated sound nice but they arent 'options', they just save time of a mapper. what i meant was some advantage in-game.

Re: Array help

Posted: August 15th, 2010, 2:47 pm
by Drofder2004
An array is just an easy accesible list, in this case of entities.

Consider it a group I can easily move around.
It makes scripting life easier, as I only have to write ONE line of code to get the array, and not create a loop to define the array. Once the array has been created I can easily and uniformly use the array using a SINGLE varaible, I can assign a variable to a global variable and not require multiple threads when obtaining a single unit from the array.

I can always reference the count of arrays array[array.size-1] will always reference the last entry.
When assigning array contents I can dynamically change the contents without changing the varialbe being used...


Are you just trolling now, or are you seriously not taking the fact that arrays are better than looping threads?

Re: Array help

Posted: August 15th, 2010, 3:24 pm
by megazor
u wrote almost the same. u just should have said there was no difference in-game. thats what i was wondering of. lol.

and btw, i may be blind but ur code loops threads as well as mine does.

Re: Array help

Posted: August 15th, 2010, 5:13 pm
by waywaaaard
Don't forget that you are supposed to write some nice and clear code that is reusable.

Re: Array help

Posted: August 15th, 2010, 9:49 pm
by Major Tom
Well I probably screwed it up, but capital W turned on show entities as wireframe and w didn’t say that it did anything. Still tried the script out and it didn’t work.

Re: Array help

Posted: August 16th, 2010, 1:28 pm
by Drofder2004
Post your script in full.