Respawning Objects in GTA 2 Mission Scripts
The example starts a thread where the objects respawn. This leaves your main loop free to create missions and game modes.
How it Works
Every 30 seconds, we destroy the object and then recreate it. We check whether any players can see the area before doing this. We don’t check the specific object, because that adds a lot of code.
|Creates the type of object you want initially.|
|Lets you respawn an object at regular intervals. Also avoids respawning too many objects at the same time.|
|Starts the respawn code and lets it run separately from the main mission loop.|
|This is the type of trigger, chosen so it always runs. Just make sure it covers the Player 1 start point!|
|Checks whether any player can see a particular location, with a single line of code!|
|Kills the object, so the game can recycle it.|
|This removes the object immediately. Only works after you have exploded the object, though!|
|Makes a new object to replace the old one.|
Add to Existing Levels
You can simply Copy and Paste these examples before
LEVELSTART, with some changes:
- Adjust the object types so you get the things you want.
- Change the co-ordinates so they appear in the right place.
- Change the object names so they make sense in your level.
- Be especially careful with
COUNTERnames to avoid conflicts with other loops.
Sadly you cannot check if most object types have been knocked over. The best workaround is to respawn it every 30 seconds. If you make sure no players can see the area this creates a realistic effect, as if city workers are cleaning up and repairing things after you leave the area for a while.
This requires an invisible timer which we can create using the
COUNTER object. We’ll increase the count every frame and reset it every 30 seconds.
For realism we’ll add a check to make sure the player cannot see the area. If you only have one object in each area, you can check whether they can see that specific object directly. Light objects might be pushed far outside of the area but if you have a lot of objects, checking each one adds a lot of code.
Example of the Objects
Just like a normal scenery object, I create the red and white police roadblock barrier:
OBJ_DATA turn1_1 = (036.2,075.5,255.0) 090 ROADBLOCK
This will appear straight away, at the start of the game. But we also need timing and a
COUNTER for the loop:
// Respawning Objects COUNTER respawn_timer ONSCREEN_COUNTER respawn_timer_onscreen // Monaco Barriers OBJ_DATA turn1_1 = (036.2,075.5,255.0) 090 ROADBLOCK // Loop COUNTER stage = 1
DELETE_ITEM command should remove the object. But that doesn’t work! You’ll just get more and more of them stacked up on that same spot, until the game crashes.
Elypter discovered that if you
EXPLODE_SMALL the object, then
DELETE_ITEM it, that will remove it. Hooray!
However, seeing the object explode is a bit weird. This is another reason to only respawn an object when nobody can see it.
Complete Example with Working Code
LEVELSTART but after all your other objects, add the respawn code:
// Upward Timer // http://gtamp.com/forum/posting.php?f=4&t=147#p5857 FORWARD upward_timer: COUNTER timer = 1 // count frames from 1-30 every second TIMER_DATA time_taken // display the time taken THREAD_TRIGGER upward_trigger = THREAD_WAIT_FOR_CHAR_IN_AREA_ANY_MEANS( p1, 175.0,063.2,1.0, 4.0,4.0, upward_timer:) upward_timer: // Setup DELAY_HERE (330) // wait for Time Limit to go away // ADD_ONSCREEN_COUNTER (respawn_timer_onscreen, respawn_timer) // test DISPLAY_TIMER (time_taken, 11) // 330 frames = 11 seconds // Respawning Objects // Note: Barricades get knocked around, so respawn them. ++respawn_timer // count 1 frame // Respawning Objects for Monaco IF (respawn_timer = 300) // after 10 seconds // Note: respawn_timer continues for Suzuka objects // Turn 1: IF (NOT (IS_POINT_ONSCREEN(036.2,075.5,1.0)) ) EXPLODE_SMALL (turn1_1) DELETE_ITEM (turn1_1) turn1_1 = CREATE_OBJ (036.2,075.5,255.0) 090 ROADBLOCK END ENDIF ENDWHILE RETURN
This carries out all the respawn requirements.
Tested in single player and multiplayer many times. It works perfectly if you set it up correctly.