[Tutorial] Using Arrays to Delete Spawned Script Models

Topic created ยท 3 Posts ยท 171 Views
  • In this tutorial, I will be outlining the use of arrays regarding the deletion of spawned script models. I have noticed that many have struggled deleting objects that they have spawned into their servers and I hope this will help. The importance of deleting spawned models lies not only in that of the playability but also the stability of the server.

    First, an array is a data structure that has one element, a key, that values are appended to. Think of an array as a collection of tuples with the target(model) remaining constant, ie; (target, value). By creating an array one is able to save the origins of spawned models and delete them when needed.

    In this example, I will show how to create an array related to spawned care packages.

    spawnCarePackage()
    
    {
    
     origin = bullettrace(self gettagorigin("j_head"), self gettagorigin("j_head") + anglesToForward(self getplayerangles()) * 200, 0, self)["position"];
    
     level.cPack = spawn( "script_model" , origin );
    
     level.cPack setModel("t6_wpn_supply_drop_ally");
    
    }
    

    The script above will spawn care packages in the location in which the player is aiming. As this is an array tutorial I will not be explaining how to implement this function in a menu. In order to create the array, we use the syntax variable name = []; . For the care package example I have used:

    self.spawnedPack =[]; 
    

    and placed it in onplayerspawned within an if statement that runs on the first spawn only (onplayerconnect would work as well with player spawnedPack as the variable). In order to use the array in the function, one would want to index each individual script model spawn and record it in the array as such.

    spawnCarePackage()
    
    {
    
     origin = bullettrace(self gettagorigin("j_head"), self gettagorigin("j_head") + anglesToForward(self getplayerangles()) * 200, 0, self)["position"];
    
     level.cPack = spawn( "script_model" , origin );
    
     level.cPack setModel("t6_wpn_supply_drop_ally");
     
     self.spawnedPack[self.spawnedPack.size] = level.cPack;
    
    }
    

    Now we have successfully linked each script model that spawns to the array. Next, we will discuss ways in which you can delete the script models. The simplest way is to make a deleteAllCarePackages function and use the array_delete capabilities of GSC. Simply calling the function will delete all spawned care packages. An example is as follows.

    deleteAllCarePackages()
    {
    	array_delete(self.spawnedPack);
    }
    

    A more advanced technique would be to index certain spawned models within the array. This can be done by using a for statement and iterating through the array deleting each specified model. An example of this technique is as follows and will delete the first 2 spawned care packages.

    deleteTwoCarePackages(self.spawnedPack)
    {
    	for( i = 0; i < 2; i++)
    	self.spawnedPack[i] delete();
    	
    }
    

    To conclude, there are many possibilities for using arrays and this is only a guideline, get creative and play around with new ideas to create some non-regurgitated mods. Cheers.

  • Great tutorial OP - should be helpful to others who are learning.

  • @Mr-Android Thanks Mr. Android ๐Ÿ˜Š

Log in to reply