GAME CONCEPT AND DESIGN
COSC 320.101_SU09
SUMMER 2009
Creating Objects
I don't know why this is the most magical part to me, but it is. Being able to create objects (items, placeables, monsters) through scripting is almost diety-like. It takes a little up front work, but it's definitly worth mastering.
Preparing the Object
The first thing you need to do is to know the resref of the object you're going to be spawning in. There's a few ways to do this:
- If it's a custom object that you created, find it in the Custom palette. Right-click and select Edit. Locate the resref and jot it down.
- If it's a standard item, you cannot do the above. The resref you see will be a fake one because the toolset thinks you're going to make a custom one. Instead, put the object on the map then right-click and Edit that object. The resref you see there will be the real one. Jot it down, then delete the object from the map.
- If you're recovering the resref of an object through scripting, you can get its resref with something like: string sItemToSpawn = GetResRef(oTarget); Of course this assumes that earlier in your script you've "put your finger on" oTarget which is the object to be copied.
The Object Type
As we've learned in the last couple of weeks, there's lots of objects in the game that are considered to be "objects." Before you spawn in yours, you need to know what type of object it is: is it a placeable? an item? a creature? These are all constant values and look like this:
OBJECT_TYPE_PLACEABLE
OBJECT_TYPE_ITEM
OBJECT_TYPE_CREATURE
There's 11 object types that you can spawn in dynamically. To see them all, go to the scripting window, click the Constants button and search on "object_type" and you'll see them all.
At a Waypoint
You will often want to spawn your object in at the location of a waypoint. To do this, you'll need to tell the engine which waypoint and then determine where the waypoint is located. Let us assume that you want to spawn a creature named Bubba and you have a waypoint for him that you've tagged as "Bubba_Home".
First we locate that waypoint:
object oBubbaSpawnPoint = GetWaypointByTag("Bubba_Home");
Now, we'll get the location of that waypoint object:
location lBubbaSpawnPoint = GetLocation(oBubbaSpawnPoint);
There...now we have the location of that waypoint.
Notes: Typically, the location variable is only used briefly or is used repeatedly, so instead of typing out something long each time, we tend to just say location lLocation. It looks a bit odd and confusing at first, but makes sense when you grok it.
Where the Player Is
Or you may want to spawn the item to the player's location. Assuming you've identified the player earlier in your script (as, say, oPlayer), you can then recover his position similarly:
location lLocation = GetLocation(oPlayer);
Deciding on a Tag
When you create an object you have the option of giving it a tag of your choice. This is handy if you plan to use a script later on to do something else to that object (such as kill it, make it attack, or give it special actions). So think of a tag if you want to. If you don't use a tag, then the engine will use the default tag from the Properties Sheet.
Spawning in the Object
To spawn in your new object, you use the CreateObject( ) command. However, the command takes a number of arguments in order to work. Conveniently, the arguments are the things we've been talking about in this document. The template for the command is:
CreateObject(<object type>,<resref>,<location>,<appear animation>,<tag>);
The appear animation means that you can set some creatures up to do more than just "fade into view." Spiders fall from above, undead crawl up from the ground (I think...I could be wrong). So you can either have the special animation play or just let them fade in.
Now, let us suppose that we want to spawn in a monster at a waypoint. And let's say we've collected the following information:
- Object type is creature.
- resref is "gruntburpbelly"
- location is his waypoint which is named "grunt_spawn"
- I don't want an animation.
- I will tag him as GruntBurpbelly.
The script sequence that gets him in the game looks like this:
object oWaypoint = GetWaypointByTag("grunt_spawn");
location lLocation = GetLocation(oWaypoint);
CreateObject(OBJECT_TYPE_CREATURE,"gruntburpbelly",lLocation,FALSE,"GruntBurpbelly");
Doing Things to the Object After Creation
You might want to do things to the object after you create it. This is easily done because CreateObject( ) actually returns a reference to the created object and puts that reference in a variable if you set it up to do so. So, let us say that we want create an orc (from the generic blueprint "nw_orca"), and we're going to give it a tag of "Fred". However, notice that the command has no way to set the display name of the orc. So we want to set it up so that when the player puts his mouse on the orc, it displays "Fred." Plus we want to give him 500 gold pieces.
object oWaypoint = GetWaypointByTag("fred_spawn");
location lLocation = GetLocation(oWaypoint);
object oCreature; //not setting it equal to anything, just getting ready.
oCreature = CreateObject(OBJECT_TYPE_CREATURE,"nw_orca",lLocation,FALSE,"Fred");
SetName(oCreature,"Fred");
GiveGoldToCreature(oCreature,500);
So you see that once the creature was created, the command put a reference to that creature in the variable oCreature. Then in subsequent commands, we just referred to oCreature and the game knew who we were talking about.
|