TGEA Hidden Gem: CameraBookmark
by Ben Sparks - Warspawn · 08/25/2008 (7:20 am) · 3 comments
I was looking into creating some custom mission markers for my AI units when I noticed a class in there besides SpawnSphere and WayPoint. It is called CameraBookmark. Since I didn't remember ever seeing that in the editor, I decided to explore further. It seems that this was put into the engine but never implemented. Perhaps it is in some sort of transition state as GG moves the TGEA codebase to 2.0, but I thought that it could be useful, and decided to implement it now.
All of these changes are script based, no c++ required.
First, if we look at tools/missionEditor/scripts/EditorGui.ed.cs we see that the following functions already exist, they are just never called.
Well, that was easy, so let's call them. I chose to setup a Bookmarks section under the Camera Menu that is already in the editor.
in tools/missionEditor/scripts/menus.ed.cs right after the cameraSpeedMenu is created add:
then a little bit further down in the actual Camera Menu, add the new item:
next, we need to add some functionality to the menu, in tools/missionEditor/scripts/menuHandlers.ed.cs add these functions, they could be anywhere, but I added them to the section commented for the Camera Menu.
now everything should work fine, however it's always nice to be able to see your markers (IMHO) so in scriptsAndAssets/server/scripts/markers.cs add this:
you can also add it to the create function in there, although it won't work with the menu jump, however in case you just want to have another type of marker:
That should be it. Now when you go to the Camera menu and select Add Bookmark, it will create one at the postion of the camera. It will automatically add it to the bookmarks sub menu for jumping. When you click on the bookmark, it'll jump the camera to the postion and rotation that it was marked.
I can think of lots of times that this would be handy, especially if you're dealing with huge missions using mega terrains or atlas. Just open the editor and boom you're half way across the map to the area that you have been testing, no need to move your spawn spheres around or generate special spawn code or mess with the camera speed so you can fly there manually :)
Finally, a couple of issues:
- First, I have no good way to delete the markers. You can delete them in the inspector, but you'll have to reload the mission to get the menu to fix itself. (onRemove is never called by Gamebase objects AFAIK)
- It might be nice to have the ability to enter in a unique name instead of CameraBookmark# .
- Perhaps setting up a Add Bookmark accelerator would be a good thing (CTRL-SHIFT-B ??)
If anyone figures out a good solution to any of those please post them, I'll update this resource with the changes.
All of these changes are script based, no c++ required.
First, if we look at tools/missionEditor/scripts/EditorGui.ed.cs we see that the following functions already exist, they are just never called.
//------------------------------------------------------------------------------
function EditorGui::addCameraBookmark(%this, %name)
{
%obj = new CameraBookmark() {
datablock = CameraBookmarkMarker;
name = %name;
};
// Place into correct group
if(!isObject(CameraBookmarks))
{
%grp = new SimGroup(CameraBookmarks);
$InstantGroup.add(%grp);
}
CameraBookmarks.add(%obj);
%cam = LocalClientConnection.camera.getTransform();
%obj.setTransform(%cam);
}
function EditorGui::removeCameraBookmark(%this, %name)
{
if(!isObject(CameraBookmarks))
return;
%count = CameraBookmarks.getCount();
for(%i=0; %i<%count; %i++)
{
%obj = CameraBookmarks.getObject(%i);
if(%obj.name $= %name)
{
%obj.delete();
return;
}
}
}
function EditorGui::removeCameraBookmarkIndex(%this, %index)
{
if(!isObject(CameraBookmarks))
return;
if(%index < 0 || %index >= CameraBookmarks.getCount())
return;
%obj = CameraBookmarks.getObject(%index);
%obj.delete();
}
function EditorGui::jumpToBookmark(%this, %name)
{
if(!isObject(CameraBookmarks))
return;
%count = CameraBookmarks.getCount();
for(%i=0; %i<%count; %i++)
{
%obj = CameraBookmarks.getObject(%i);
if(%obj.name $= %name)
{
LocalClientConnection.camera.setTransform(%obj.getTransform());
return;
}
}
}
function EditorGui::jumpToBookmarkIndex(%this, %index)
{
if(!isObject(CameraBookmarks))
return;
if(%index < 0 || %index >= CameraBookmarks.getCount())
return;
%trans = CameraBookmarks.getObject(%index).getTransform();
LocalClientConnection.camera.setTransform(%trans);
}Well, that was easy, so let's call them. I chose to setup a Bookmarks section under the Camera Menu that is already in the editor.
in tools/missionEditor/scripts/menus.ed.cs right after the cameraSpeedMenu is created add:
// camera bookmark gem
%this.cameraBookmarkMenu = new PopupMenu()
{
superClass = "MenuBuilder";
class = "EditorCameraBookmarkMenu";
bookmarks = 0; // to keep track of how many bookmarks we have
item[0] = "Add Bookmark"; // no function, handler will test id
Item[1] = "-";
// actual bookmark items dynamically added from bookmarks in mission
};then a little bit further down in the actual Camera Menu, add the new item:
// Camera Menu
new PopupMenu()
{
superClass = "MenuBuilder";
class = "EditorCameraMenu";
barTitle = "Camera";
item[0] = "Drop Camera at Player" TAB "Alt Q" TAB "commandToServer('dropCameraAtPlayer');";
item[1] = "Drop Player at Camera" TAB "Alt W" TAB "commandToServer('DropPlayerAtCamera');";
item[2] = "-";
item[3] = "Toggle Camera" TAB "Alt C" TAB "commandToServer('ToggleCamera');";
item[4] = "-";
item[5] = "Speed" TAB %this.cameraSpeedMenu;
// camera bookmark gem
Item[6] = "Bookmarks" TAB %this.cameraBookmarkMenu;
};next, we need to add some functionality to the menu, in tools/missionEditor/scripts/menuHandlers.ed.cs add these functions, they could be anywhere, but I added them to the section commented for the Camera Menu.
// camera bookmark gem
function EditorCameraBookmarkMenu::onSelectItem(%this, %id, %text)
{
// test the id, the first 2 items are not actual bookmarks
if(%id == 0)
{
// first one is the add bookmark function
%nextIndex = 0;
if(isObject(CameraBookmarks))
{
%nextIndex = CameraBookmarks.getCount();
}
%newBookmark = "CameraBookmark" @ %nextIndex;
EditorGui.addCameraBookmark(%newBookmark);
// add the item to the menu
%this.insertItem(%nextIndex+2, "CameraBookmark" @ %nextIndex);
%this.bookmarks++;
}
// all the bookmarks will be item 2 or greater
if(%id >= 2)
{
// we'll jump to the bookmark index, in this case id - 2
EditorGui.jumpToBookmarkIndex(%id-2);
}
}
function EditorCameraBookmarkMenu::setupDefaultState(%this)
{
// here we need to build the items available from the current mission
if(isObject(CameraBookmarks))
{
%count = CameraBookmarks.getCount();
for(%i=0; %i<%count; %i++)
{
%obj = CameraBookmarks.getObject(%i);
//format for menu items seems to be: name TAB accelerator TAB function
%this.insertItem(%i+2, %obj.name);
%this.bookmarks++;
}
}
Parent::setupDefaultState(%this);
}now everything should work fine, however it's always nice to be able to see your markers (IMHO) so in scriptsAndAssets/server/scripts/markers.cs add this:
datablock MissionMarkerData(CameraBookmarkMarker)
{
category = "Misc";
shapeFile = "~/data/shapes/markers/octahedron.dts";
};you can also add it to the create function in there, although it won't work with the menu jump, however in case you just want to have another type of marker:
//------------------------------------------------------------------------------
// - serveral marker types may share MissionMarker datablock type
function MissionMarkerData::create(%block)
{
switch$(%block)
{
case "WayPointMarker":
%obj = new WayPoint() {
dataBlock = %block;
};
return(%obj);
case "SpawnSphereMarker":
%obj = new SpawnSphere() {
datablock = %block;
};
return(%obj);
case "CameraBookmarkMarker":
%obj = new CameraBookmark() {
datablock = %block;
};
return(%obj);
}
return(-1);
}That should be it. Now when you go to the Camera menu and select Add Bookmark, it will create one at the postion of the camera. It will automatically add it to the bookmarks sub menu for jumping. When you click on the bookmark, it'll jump the camera to the postion and rotation that it was marked.
I can think of lots of times that this would be handy, especially if you're dealing with huge missions using mega terrains or atlas. Just open the editor and boom you're half way across the map to the area that you have been testing, no need to move your spawn spheres around or generate special spawn code or mess with the camera speed so you can fly there manually :)
Finally, a couple of issues:
- First, I have no good way to delete the markers. You can delete them in the inspector, but you'll have to reload the mission to get the menu to fix itself. (onRemove is never called by Gamebase objects AFAIK)
- It might be nice to have the ability to enter in a unique name instead of CameraBookmark# .
- Perhaps setting up a Add Bookmark accelerator would be a good thing (CTRL-SHIFT-B ??)
If anyone figures out a good solution to any of those please post them, I'll update this resource with the changes.
About the author
I'm a web developer by day, hobbyist game developer by night.
#2
10/21/2008 (9:27 am)
I've not tried this resource, but before I do, could this be used for placing surveilance cameras in the game that can be accessed within a GUI ? Is this code also networked for multi-player? 
Torque Owner Netwyrm
Canopic Games