Forum Jump :

Author Message


Posts: 7
Rank:


Level: Member

Country: nl
Location: Rotterdam
Occupation:
Age:
In-game name:

 
#1 Posted at 2014-12-11 01:19        
     
Hello everyone,

My name is Tiberius and i'm pretty new on these forums. I've been coming here for a long time, but never posted anything before.

What I see on multiplayer games is when two or more people breach rooms, there is usually too much time between them when they enter a room. I'm trying to create a script that attaches players to each other and basically turns them into a train. This way they enter the room at the same time. I'm doing this via an 'addaction' with some arguments about looking left or right. So far so good :)

This is what I got so far:

if (!isisDedicated) exitwith {};

_first = _this select 0;
_second = _this select 1;
_direction = _this select 3;

_second attachTo [_first, [0, -.7, 0] ];
_second setDir _direction;

I now need a way to detach "_second" (the caller of the script and second guy in the train) in the quickest way possible so the room can be cleared quickly after the units entered it.
This script only aims at entering the room. As soon the units are in the room I want them to be able to move freely.

To do this I'm looking into detecting 'keys presses/downpress'. So as soon as either 'W', 'A', 'S' or 'D' is pressed by _second he should be in detached. I've been looking other threads but I'm getting nowhere. I've been looking at this topic and similar others, but I have not been able to apply this to what I need... :(

Is there anyone who can point me in the right direction on this, or have a better solution for what I'm trying to do.
I'm not really good @ scripting and I only have basic knowledge. Of course, to makes things simple, it is for multiplayer use...

Thanks in advance for your time and effort.

Cheers
Tiberius

This post was edited by Tiberius (2014-12-12 01:28, 1042 days ago)


Author Message


Posts: 131
Rank:


Level: Member

Country: us
Location:
Occupation:
Age: 22
In-game name: Joshua

 
#2 Posted at 2014-12-11 02:47        
     
You are definitely on the right path with the event handlers.

I am working on a script that relies heavily on key presses, I set up the event handler at the beginning of the script with global variables for the DIK code and shift. Below that is my code, specifically a while loop to keep the script going until its done. To get a key code in the while loop (this can be outside a while loop too for your script) I just get the global variable that the event handler assigns the key code to.

The only draw back is this has to use a global variable. I am not sure if this is multiplayer compatible as I haven't been able to test it in a multiplayer enviornment. With local variables, it would for sure be multiplayer compatible, but I don't know how a script handles global variables across multiple instances of the same script in a multiplayer setting.

Here is the relevant code from the script I am working on:
pressedKey = -1;
shift = false;
(findDisplay 46) displayAddEventHandler ["KeyUp", {pressedKey = _this select 1; shift = _this select 2;}];
if(pressedKey == 41)then{hint "TEST";};
//Key 41 is the tilde (`) key

Do note that I am using "KeyUp" for my event handler, as I had issues with "KeyDown" for the input in my script, for yours however, I am almost certain that "KeyDown" will work better.

Hopefully this helps, let me know if I didn't cover something you needed or you need more explanation.


Advertisement


Author Message


Posts: 7
Rank:


Level: Member

Country: nl
Location: Rotterdam
Occupation:
Age:
In-game name:

 
#3 Posted at 2014-12-11 12:46        
     
Hello jshear95,

Many many thanks for your response, it works like a charm in the editor (will try multiplayer later today).
This is how the script turned out:

if (!isisDedicated) exitwith {};


_first = _this select 0;
_second = _this select 1;
_direction = _this select 3;
_breach = true;
pressedKey = -1;


_second attachTo [_first, [0, -.7, 0] ];
_second setDir _direction;

while {_breach} do
	{
	if (_first == _second) then 

		{
		detach _second;
		_breach = false;
		};

	if (!alive _first && !alive _second) then

		{
		detach _second;
		hint "dead";
		_breach = false;
		};

	(findDisplay 46) displayAddEventHandler ["KeyDown", {pressedKey = _this select 1; }];
	if(pressedKey == 17 || pressedKey == 30 || pressedKey == 32 && _breach) then
		{
		detach _second;
		_breach = false;
		};

	sleep 0.1;	

	};

exit;

Although it works, I do not entirely understand what is happening here. for instance, what is the reason for -1 value for the pressedKey variable?

Next thing to do is testing it and see if it actually helps clearing rooms :P
And further improving this script of course!


Author Message


Posts: 10
Rank:


Level: Member

Country: au
Location:
Occupation:
Age:
In-game name:

 
#4 Posted at 2014-12-11 12:46        
     
The problem with monitoring the w,a,s,d keys is that two players might move before they have entered the building causing the code to execute before it is supposed to.

You could try something like the boundingBox command and execute the code when both players are inside the building.


Author Message


Posts: 7
Rank:


Level: Member

Country: nl
Location: Rotterdam
Occupation:
Age:
In-game name:

 
#5 Posted at 2014-12-11 13:12        
     
# Blackjack 89 : The problem with monitoring the w,a,s,d keys is that two players might move before they have entered the building causing the code to execute before it is supposed to.

That is indeed what is happening... Will the boundingbox check also work when you are already in a building and if you want check several rooms?

The idea is a generic script that can be controlled and applied easily. It is not for a specific building or one single occasion.

Is their a way to limit the eventhandler to one unit/the caller?

This post was edited by Tiberius (2014-12-11 13:48, 1043 days ago)


Author Message


Posts: 131
Rank:


Level: Member

Country: us
Location:
Occupation:
Age: 22
In-game name: Joshua

 
#6 Posted at 2014-12-11 15:37        
     
I don't know how the event handler will work with multiple players. My guess is it will only monitor one person (the script caller) at a time needing a script instance for each person.

The reason for keypressed being -1 is so that it is not equal to any DIK code. DIK codes start at 0 and go up from there (probably to 104 for each key on the keyboard). You could set pressed key equal to any value you want. The -1 ensures that it doesn't accidentally register a value for a key.
keypressed is only initialized as -1, when a key is pressed it will change to the DIK code for the given key. i.e. keypressed = 17 after 'w' is pressed.


Author Message


Posts: 7
Rank:


Level: Member

Country: nl
Location: Rotterdam
Occupation:
Age:
In-game name:

 
#7 Posted at 2014-12-11 23:40        
     
Thanks for the explanation jshear95. It makes it more clear now :)

The multiplayer test was a succes btw :) The eventhandler only reacted to the caller, as jshear95 predicted! The script did exactly what I wanted it to do! Thanks to both jshear95 and Blackjack 89 for your input.

Now i'm going to improve this script :)

Cheers
Tiberius

This post was edited by Tiberius (2014-12-12 01:29, 1042 days ago)