New to this. Adding User Functions

I'm trying to add a one-off random encounter triggered when the player leaves a specific type of room. I have currently implemented this by using an exit script which is triggered by the RandowChance function when the player exits a room and an if clause and attribute which prevents recurrence. This works fine.

My problem is that I have to copy the same scripts into the exit of every room where this encounter might take place ( 8 duplicates of 20 lines of code). Obviously it would be more efficient to put the encounter script into a function and call that function when leaving each room as this would need only one copy of the encounter script in the who;e code.

Can this be done? If so how? I have read the documentation but I can only find Internal functions. Please help


Hello.

If you are using 8 duplicates, you can:

Create a function, naming it RoomExitStuff

Paste your code from the exit script directly into the script for the function.

In the exit script for each of those rooms, replace the script with:

RoomExitStuff

That will call that script each time.


A Dumb Example Game:

<!--Saved by Quest 5.7.6606.27193-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Function for Exits">
    <gameid>7307b762-ac9e-407a-b341-60d0323cc228</gameid>
    <version>1.0</version>
    <firstpublished>2018</firstpublished>
    <subtitle>A Dumb Example Game</subtitle>
    <author>KV</author>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <onexit type="script">
    </onexit>
    <exit alias="north" to="room1">
      <inherit name="northdirection" />
    </exit>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
  </object>
  <object name="room1">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room">
      <inherit name="southdirection" />
    </exit>
    <exit alias="north" to="room2">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="room2">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room1">
      <inherit name="southdirection" />
    </exit>
    <exit alias="north" to="room3">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="room3">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room2">
      <inherit name="southdirection" />
    </exit>
    <exit alias="north" to="room4">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="room4">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room3">
      <inherit name="southdirection" />
    </exit>
    <exit alias="north" to="room5">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="room5">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room4">
      <inherit name="southdirection" />
    </exit>
    <exit alias="north" to="room6">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="room6">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room5">
      <inherit name="southdirection" />
    </exit>
    <exit alias="north" to="room7">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="room7">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room6">
      <inherit name="southdirection" />
    </exit>
    <exit alias="north" to="room8">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="room8">
    <inherit name="editor_room" />
    <onexit type="script">
      RoomExitStuff
    </onexit>
    <exit alias="south" to="room7">
      <inherit name="southdirection" />
    </exit>
  </object>
  <function name="RoomExitStuff">
    msg ("The real script will be here!")
  </function>
</asl>

Another idea is to create a new room parent type that extends the existing room editor and give it an exit script calling your function, then apply this room type to any rooms you want and they will all work exactly the same way.

That way the only unique stuff you'll have to add to the child rooms are the exits which can't be inherited.

I am doing this in my own game and is very efficient. If you want an example let me know and I can put something in this thread.


first {
  SpawnZombie (this)
}

You can put a function into another function. You copy paste your old code into a new function. I have a whole mess of code in my games. It is unneeded if you just copy paste everything in the exit scripts, as you've already said.


This is the kind of thing I am using, I can customize the beforefirstenter. beforeenter, enter, onexit, and turn scripts with inheritable code:

<type name="SpecialRoomType">
<inherit name="editor_room" />
<beforeenter type="script">
   BeforeEnterFunction()
</beforeenter>
<enter type="script">
	EnterFunction()
</enter>
<onexit type="script">
	ExitFunction()
</onexit>
<beforefirstenter type="script">
    BeforeFirstEnterFunction()
</beforefirstenter>
</type>

<object name="SpecialRoom1">
<inherit name="SpecialRoomType" />
</object>

Many thanks for all your replies especially K.V. for the code sample which worked a treat.


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

Support

Forums