newbie scripter: Door switch
Moderator: Event DM
- JollyOrc
- Elder Sage
- Posts: 3984
- Joined: Fri Jan 17, 2003 11:41 am
- Timezone: Europe, CE(S)T
- Location: Germany
- Contact:
newbie scripter: Door switch
Hi
I want to have a lever that operates two doors.
if switched it locks the one door, and unlocks the other, and giving a message which one is locked now.
If switched again, it unlocks the first, and locks the other, again giving a message.
I've puzzled out how to do if, but how to query the states of objects is beyond me, I've just started looking at the scripting thing.
so I want to do:
if (door1.locked)
door2.lock
door1.unlock
tell "Door 1 open now"
else
door1.lock
door2.unlock
tell "door 2 open now"
fi
how to do that in NWNScripting ?
Have I seen that right that there isn't an "else" in NWNScripting ?
I want to have a lever that operates two doors.
if switched it locks the one door, and unlocks the other, and giving a message which one is locked now.
If switched again, it unlocks the first, and locks the other, again giving a message.
I've puzzled out how to do if, but how to query the states of objects is beyond me, I've just started looking at the scripting thing.
so I want to do:
if (door1.locked)
door2.lock
door1.unlock
tell "Door 1 open now"
else
door1.lock
door2.unlock
tell "door 2 open now"
fi
how to do that in NWNScripting ?
Have I seen that right that there isn't an "else" in NWNScripting ?
- Lafferty
- Scholar
- Posts: 1129
- Joined: Tue Feb 11, 2003 5:08 pm
- Location: look at my hands... they are HUGE. And they cant touch themselves...
- Contact:
there is an else ... e.g.
The following examples have the same effect:
it is also available as CHM file : http://nwvault.ign.com/Files/other/data ... 1708.shtml
there also is a switch statement which is not used often... i dont know why as it can make scripts quite readable:int bAlpha = FALSE;
int bBravo = FALSE;
if (bAlpha)
{
// statement 1 ...
}
else if (bBravo)
{
// statement 2 ...
}
else
{
// statement 3 ...
// in this example, this is the code that is executed becase
// both bAlpha and bBravo are FALSE
}
The following examples have the same effect:
You might want to have a look here: http://www.reapers.org/nwn/reference/int x = 0;
// if block - silly!
if (x == 0)
{
// statements ...
}
else if (x == 1)
{
// statements ...
}
else if (x == 2)
{
// statements ...
}
else
{
// statements ...
}
// switch block
// the above if/else block is functionally equivalent to the below
// switch statement
switch (x)
{
case 0:
// statements ...
// without a break testing would continue
// in this example if we didn't use a break, the default
// condition would also evaluate as true if x == 0
break;
case 1:
// statements ...
break;
case 2:
// statements ...
break;
default:
// statements ...
break;
}
it is also available as CHM file : http://nwvault.ign.com/Files/other/data ... 1708.shtml
Tool for crafters Do you want some human to your salt? nomanisanisland.monolar.de
- Lafferty
- Scholar
- Posts: 1129
- Joined: Tue Feb 11, 2003 5:08 pm
- Location: look at my hands... they are HUGE. And they cant touch themselves...
- Contact:
FUCK:!! I just lost my session! i had it ready! damn it... sorry.... i know this is for the rants and raves!
Tool for crafters Do you want some human to your salt? nomanisanisland.monolar.de
- Lafferty
- Scholar
- Posts: 1129
- Joined: Tue Feb 11, 2003 5:08 pm
- Location: look at my hands... they are HUGE. And they cant touch themselves...
- Contact:
ok, but this one is tested:
Maybe there is a simpler way to do it.
You need the two doors (with tags: "MyDoor_1" and "MyDoor_2") and a lever. Put this script in the OnUsed part of the placeable lever.
Set the initial state of one door to locked and can be relocked and plot would also make sense.
The other door should also be plot and check the can be relocked property
I think that way the door could still be lockpicked, haven't tested it
Maybe there is a simpler way to do it.
You need the two doors (with tags: "MyDoor_1" and "MyDoor_2") and a lever. Put this script in the OnUsed part of the placeable lever.
Set the initial state of one door to locked and can be relocked and plot would also make sense.
The other door should also be plot and check the can be relocked property
I think that way the door could still be lockpicked, haven't tested it
All the delays are necessary to give the doors the time to get closed if they are open. I'm not sure if they are really necessary. Could be that nwscript allows a door to be locked even if it is still open...void main()
{
object oDoor1 = GetObjectByTag("MyDoor_1");
object oDoor2 = GetObjectByTag("MyDoor_2");
object oPC = GetLastUsedBy();
ActionCloseDoor(oDoor1);
ActionCloseDoor(oDoor2);
if (GetLocked(oDoor1))
{
DelayCommand(1.5, SetLocked(oDoor1, FALSE));
DelayCommand(1.5, SetLocked(oDoor2, TRUE));
DelayCommand(2.5, ActionOpenDoor(oDoor1));
DelayCommand(1.5, FloatingTextStringOnCreature("Door1 unlocked, Door2 locked", oPC));
}
else
{
DelayCommand(1.5, SetLocked(oDoor2, FALSE));
DelayCommand(1.5, SetLocked(oDoor1, TRUE));
DelayCommand(2.5, ActionOpenDoor(oDoor2));
DelayCommand(1.5, FloatingTextStringOnCreature("Door2 unlocked, Door1 locked", oPC));
}
Tool for crafters Do you want some human to your salt? nomanisanisland.monolar.de
- Lafferty
- Scholar
- Posts: 1129
- Joined: Tue Feb 11, 2003 5:08 pm
- Location: look at my hands... they are HUGE. And they cant touch themselves...
- Contact:
ok, just tested it. set the doors properties to "key needed to lock or unlock" and leave the key field empty. That way the doors cannot be lockpicked
Tool for crafters Do you want some human to your salt? nomanisanisland.monolar.de
- Lafferty
- Scholar
- Posts: 1129
- Joined: Tue Feb 11, 2003 5:08 pm
- Location: look at my hands... they are HUGE. And they cant touch themselves...
- Contact:
ok, you also dont need all the DelayCommands. The script looks then like this.
void main()
{
object oDoor1 = GetObjectByTag("MyDoor_1");
object oDoor2 = GetObjectByTag("MyDoor_2");
object oPC = GetLastUsedBy();
ActionCloseDoor(oDoor1);
ActionCloseDoor(oDoor2);
if (GetLocked(oDoor1))
{
SetLocked(oDoor1, FALSE);
SetLocked(oDoor2, TRUE);
ActionOpenDoor(oDoor1);
FloatingTextStringOnCreature("Door1 unlocked, Door2 locked", oPC);
}
else
{
SetLocked(oDoor2, FALSE);
SetLocked(oDoor1, TRUE);
ActionOpenDoor(oDoor2);
FloatingTextStringOnCreature("Door2 unlocked, Door1 locked", oPC);
}
Tool for crafters Do you want some human to your salt? nomanisanisland.monolar.de
- JollyOrc
- Elder Sage
- Posts: 3984
- Joined: Fri Jan 17, 2003 11:41 am
- Timezone: Europe, CE(S)T
- Location: Germany
- Contact:
ok, once you understand the basics, it's easy:
defines the object oDoor1 and assigns the door with the tag "MyDoor_1" to this object.
defines another oject. This time, it gets assigned the PC that has last used the object that calls this script. Confusing ? Let's assume that this line is part of a script that is bound to a lever. Now Joe Hero pulls that lever. The script is placed in the OnUsed event, and is thus now executed.
This line of code now determines who exactly was the idiot that pulled that lever. So the variable oPC now points neatly to Joe Hero.
should be selfexplanatory. I explain it nonetheless: this command causes the object oDoor1 (that's the door with the tag "MyDoor_1", remember ?) to do the action "CloseDoor". The door closes, neat and simple.
locks the object, in this case again our trusty "MyDoor_1".
this looks a bit complicated, but is easy. The term GetLocked(oDoor1) checks if that door is actually locked. if it is, it executes the part between the first set of { } - brackets.
If not, it executes the second set.
Both sets contain essentiallly the same command, only with slightly different values.
The command FloatingTextStringOnCreature causes the string in the first variable in the ()-brackets to be displayed above the creature which is pointed at with the second variable, in this case that's probably Joe Hero.
Play around with these things, they are easier than they look.
Code: Select all
object oDoor1 = GetObjectByTag("MyDoor_1");
Code: Select all
object oPC = GetLastUsedBy();
This line of code now determines who exactly was the idiot that pulled that lever. So the variable oPC now points neatly to Joe Hero.
Code: Select all
ActionCloseDoor(oDoor1);
Code: Select all
SetLocked(oDoor1, TRUE);
Code: Select all
if (GetLocked(oDoor1))
{
FloatingTextStringOnCreature("Door 1 locked", oPC);
}
else
{
FloatingTextStringOnCreature("Door1 unlocked", oPC);
}
If not, it executes the second set.
Both sets contain essentiallly the same command, only with slightly different values.
The command FloatingTextStringOnCreature causes the string in the first variable in the ()-brackets to be displayed above the creature which is pointed at with the second variable, in this case that's probably Joe Hero.
Play around with these things, they are easier than they look.