Easier Way? - Item in Room Switched On?

dgparryuk
Is there an easier way of doing this than i've just done?


    <object name="Room1">
<inherit name="editor_room" />
<listchildren />
<description type="string"></description>
<enter type="script">
if (Trap1.parent = Room1) {
if (IsSwitchedOn(Trap1)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap2.parent = Room1) {
if (IsSwitchedOn(Trap2)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap2.parent = Room1) {
if (IsSwitchedOn(Trap2)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap3.parent = Room1) {
if (IsSwitchedOn(Trap3)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap4.parent = Room1) {
if (IsSwitchedOn(Trap4)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap5.parent = Room1) {
if (IsSwitchedOn(Trap5)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap6.parent = Room1) {
if (IsSwitchedOn(Trap6)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap7.parent = Room1) {
if (IsSwitchedOn(Trap7)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap8.parent = Room1) {
if (IsSwitchedOn(Trap8)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
if (Trap9.parent = Room1) {
if (IsSwitchedOn(Trap9)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
</enter>


I have 502 rooms - i don't really want to repeat that that many times....

dgparryuk
and a second bit of BAD code from me....

            if (player.opponent = "Op1") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op1.parent)
}
if (player.opponent = "Op2") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op2.parent)
}
if (player.opponent = "Op3") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op3.parent)
}
if (player.opponent = "Op4") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op4.parent)
}
if (player.opponent = "Op5") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op5.parent)
}
if (player.opponent = "Op6") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op6.parent)
}
if (player.opponent = "Op7") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op7.parent)
}
if (player.opponent = "Op8") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op8.parent)
}
if (player.opponent = "Op9") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op9.parent)
}


again an easier way of doing it?
a neater way? and the output is "Object : Room213" is there anyway of changing that to just Room213 or even the Alias
ie Room7 is "Access Road" (one of about 20 rooms called Access Road) or am i doing it wrong?

The Pixie
dgparryuk wrote:Is there an easier way of doing this than i've just done?
I have 502 rooms - i don't really want to repeat that that many times....

Do you want this same code to run when the player enters every room? Go to the Script tab of the game object, and put the script in the "Script when entering a room" bit.

Code (untested!):
for (i, 1, 9) {
o = GetObject("Trap" + i)
if (o.parent = player.parent) {
if (IsSwitchedOn(o)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
}

The Pixie
dgparryuk wrote:and a second bit of BAD code from me....

            if (player.opponent = "Op1") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op1.parent)
}
if (player.opponent = "Op2") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op2.parent)
}
if (player.opponent = "Op3") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op3.parent)
}
if (player.opponent = "Op4") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op4.parent)
}
if (player.opponent = "Op5") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op5.parent)
}
if (player.opponent = "Op6") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op6.parent)
}
if (player.opponent = "Op7") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op7.parent)
}
if (player.opponent = "Op8") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op8.parent)
}
if (player.opponent = "Op9") {
msg ("Your Target (" + player.opponent + ") is in Room" + Op9.parent)
}


again an easier way of doing it?
a neater way? and the output is "Object : Room213" is there anyway of changing that to just Room213 or even the Alias
ie Room7 is "Access Road" (one of about 20 rooms called Access Road) or am i doing it wrong?

This should do all that (GetDisplayAlias will grab the alias if it has one, and the name otherwise):
msg ("Your Target (" + GetDisplayAlias(player.opponent) + ") is in Room" + GetDisplayAlias(player.opponent.parent))

dgparryuk
The Pixie wrote:

"dgparryuk"

Is there an easier way of doing this than i've just done?
I have 502 rooms - i don't really want to repeat that that many times....


Do you want this same code to run when the player enters every room? Go to the Script tab of the game object, and put the script in the "Script when entering a room" bit.

Code (untested!):
for (i, 1, 9) {
o = GetObject("Trap" + i)
if (o.parent = player.parent) {
if (IsSwitchedOn(o)) {
msg ("Boom (Random Place, Lose All Items)")
}
}
}



That's exactly what i wanted - basically if there is a trap in the room and it's switched on it runs the message - which is a place holder till i figure out the code for that ;-)

And it worked perfectly

dgparryuk
The Pixie wrote:
This should do all that (GetDisplayAlias will grab the alias if it has one, and the name otherwise):
msg ("Your Target (" + GetDisplayAlias(player.opponent) + ") is in Room" + GetDisplayAlias(player.opponent.parent))



I get an error of
Error running script: Error compiling expression '"Your Target (" + GetDisplayAlias(player.opponent) + ") is in Room" + GetDisplayAlias(player.opponent.parent)': Object reference not set to an instance of an object.


I think i tried something similar to "player.opponent.parent" and it didn't work...

The Pixie
Ah, I guess at some point player.opponent is not set. Try this:
if (not player.opponent = null) {
msg ("Your Target (" + GetDisplayAlias(player.opponent) + ") is in Room" + GetDisplayAlias(player.opponent.parent))
}

dgparryuk
same error - If you want to laugh at how bad some of my code is - i've attached it

essentially it's a cat and mouse game in a maze with 10 Characters, and each person can only target one other person at a time
the keys open doors
passcard allow access to the computers
the traps & weapons/defence are obvious

I'm trying to get the easy bits done first....
the NPC is going to be REALLY hard, as i want them to be able to pick up weapons and such like...

Pertex
You must change your start script a bit:

msg ("Op9 is worth : " + Op9.prize)
show menu ("Choose Your Opponent?", Split ("Op1;Op2;Op3;Op4;Op5;Op6;Op7;Op8;Op9", ";"), false) {
player.opponent = GetObject(result)
msg ("Your Opponent is : " + player.opponent.name)
}
}
else {
tmp_opponent = GetRandomInt(1,9)
player.opponent = GetObject("Op" + tmp_opponent)
msg ("Your Opponent is : " + player.opponent.name)
}
}


If you set player.opponent ="Op1" the opponent variable is a string variable and you can't access the parent object of a string variable. So you first must get the object with the name "Op1" with GetObject("Op1")

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums