ModmeDTZxPorter: Goodbye

New Modme platform coming soon!
Abnormal202 Scripting Tutorial 6: Properties
Viewed 156 time(s)
Topic created on 1533878761
Original post
Modme User
64 wins



See ALL Tutorials




Properties are very similar to variables, and I probably would've talked about them in the variables tutorial, however I thought that one was long enough and didn't want to overwhelm.


Properties are just more ways we can store information into a variable, and you'll see them quite a lot.





You can create a property on a variable simply by putting a "." at the end of it and then adding the name to your property, and finally defining it.


andrew = GetEnt("andrew","targetname");

andrew.gender = "male";



In the above example, I made a variable andrew (with some ent in radiant stored in it), and then gave it the property of "gender", which I defined as "male".


Just like with variables, properties can store numbers, strings, booleans, and even entities.


andrew.clip = GetEnt("andrew_clip","targetname");



We can do all the same things we can do with variables with properties. For example using in functions:


andrew.clip Delete();


It's important to note if this code was executed, the entity in radiant with targetname: "andrew_clip" would be deleted, not the entity with targetname: "andrew", because the property is being sent to the Delete() function, not the actual andrew variable.


Properties are also often used to check for certain conditions in If Statements:


if(isdefined(andrew.gender) && andrew.gender == "male")
	IPrintLnBold("By golly, you're a man!");


Really the sky is limit. You can even make properties of properties:


andrew = GetEnt("andrew","targetname"); = "on"; = "t_shirt"; = "red";



It should be noted that passing a variable that has properties on it will retain all properties on it in the function it is being passed to.


for example:

	andrew = GetEnt("andrew","targetname"); = "on"; = "t_shirt"; = "red";
	andrew thread clothing_appraisal();
function clothing_appraisal()
	if(isdefined( && == "on")
		if(isdefined( && == "t_shirt")
			if(isdefined( && == "red")
				IPrintLnBold("I think red looks rather nice on you");




Remember that variable I talked about earlier, that can be accessed from anywhere and is the same entity according to all scripts? that variable is called level, and there's a reason Sublime highlights it in purple.


Because it's the same everywhere, setting properties on the level variable makes it easy to access said properties wherever, from any script/function/you name it. Using level properties is the quick & easy solution to passing data, without having to call, thread, or pass arguments.


However there's a reason I've introduced this a bit late, in tutorial #6, because I see a lot of novice scripters overuse to death the level properties, myself included. Because of how easy it was to use, I used it for a bunch of things that using self, or arguments would have done fine for, and that led to me not knowing how to pass data in other ways than level properties.


Other than that, Why shouldn't you use level properties?

- You don't want to accidentally create a property that has the same name as one already defined in Treyarch scripts, as you could really break stuff by overriding a Treyarch property, or having yours overridden. (though this is also a problem with setting properties on players, or any common entity that's going to be in the game regardless of the map)

- Setting tons of level properties (for things that don't need it) can make your scripts a lot harder to follow, for other people or yourself later. 


See ALL Tutorials