Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
/VM/station wiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
SS13 for experienced programmers
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== [http://www.byond.com/members/?command=reference&path=var Variables] === Variables are very general, Byond makes no difference in the declaration of strings, integers, etc. (Similar to PHP) ==== [http://www.byond.com/members/?command=reference&path=var Predefined variables] ==== There is a lot of predefined variables for objects in BYOND, but the most important are: *'''[http://www.byond.com/members/?command=reference&path=proc%2Fvar%2Fsrc src]''' - a variable equal to the object containing the proc or verb. It is defined to have the same type as that object. (Similar to "this" in Java or C++) *'''[http://www.byond.com/members/?command=reference&path=proc%2Fvar%2Fusr usr]''' - a mob variable (var/mob/usr) containing the mob of the player who executed the current verb, or whose action ultimately called the current proc. '''A good rule of thumb is to never put usr in a proc.''' If src would not be the correct choice, it is better to send another argument to your proc with the information it needs. *'''[http://www.byond.com/members/?command=reference&path=proc%2Fvar%2Fargs args]''' - a list of the arguments passed to the proc or verb. *'''[http://www.byond.com/members/?command=reference&path=datum%2Fvar%2Fvars vars]''' - a list of object variables. If the variable name is used as an index into the list, the value of that variable is accessed. For more SS13 specific variables see [[#SS13 common variable meanings|SS13 common variable meanings]] ==== Variable definition ==== =====Basic definitions===== var/j var/i = 4 var/a, b, c =====Complex definitions===== The general syntax is [http://www.byond.com/members/?command=reference&path=var var/type/variable_name = value] Examples: var/obj/item/I = new/obj/item [http://www.byond.com/members/?command=reference&path=operator%2F%40dt%3B I.name] = "Some item" Datum definition and declaration of a variable of that datum type: datum/test_datum var/test_variable = 0 //declaration of the test_variable var proc/set_as(var/i) //proc definition within the test_datum datum test_variable = i //set the test_variable var to the value in the argument var/datum/test_datum/TD = new/datum/test_datum //TD will now be a reference to a datum of type test_datum TD.test_variable = 4 //Byond doesn't know of private variables, so you can set any variables like this [http://www.byond.com/members/?command=reference&path=world world] [http://www.byond.com/members/?command=reference&path=operator%2F%26lt%3B%26lt%3B <<] TD.test_variable //will output 4 to all connected users TD.set_as(10) //Will call the set_as proc in the datum with the argument 10 TD.test_variable //will output 10 to all connected users ==== [[Binary flags|Bitflags]] ==== See the main article at [[Binary flags]]. Bitflags are variables which use bits instead of numbers to determine conditions. The bit operators are [http://www.byond.com/members/?command=reference&path=operator%2F%26 &] and [http://www.byond.com/members/?command=reference&path=operator%2F%7C |]. For now, you should know that bitflag operators use the binary value of numbers to determine the result. So [http://www.byond.com/members/?command=reference&path=operator%2F%26 13 & 3] will result in 1. ([http://www.byond.com/members/?command=reference&path=operator%2F%26 1101 & 0011 = 0001]) and [http://www.byond.com/members/?command=reference&path=operator%2F%7C 13 | 3 = 15] ([http://www.byond.com/members/?command=reference&path=operator%2F%7C 1101 | 0011 = 1111]) see [[#If|if]] for uses ==== Variable types ==== [http://www.byond.com/members/?command=reference&path=var reference] *[http://www.byond.com/members/?command=reference&path=datum datum] - ordinary object type (class in java) *[http://www.byond.com/members/?command=reference&path=atom atom] - atom derives into obj, turf, mob and area *[http://www.byond.com/members/?command=reference&path=turf turf] - tiles which make up the floors, walls and space on SS13 *[http://www.byond.com/members/?command=reference&path=area area] - areas are grouped locations. They combine many turfs and it gives some common properties. Power, atmosphere, etc. are determined by areas *[http://www.byond.com/members/?command=reference&path=mob mob] - an object with life, be it game controlled or player controlled. *[http://www.byond.com/members/?command=reference&path=obj obj] - objects which can be placed on the map *[http://www.byond.com/members/?command=reference&path=client client] - a new client object is created for each connected player *[http://www.byond.com/members/?command=reference&path=list list] - a list of elements. The first element in a list called L is L[1] *[http://www.byond.com/members/?command=reference&path=world world] - this is a variable where some global variables for the entire world can be set. World's contents var contains all atoms currently in the game world. ==== [http://www.byond.com/members/?command=reference&path=operator%2F%26lt%3B%26lt%3B%2Foutput Outputting messages] ==== The most basic way of outputting messages is with the [http://www.byond.com/members/?command=reference&path=operator%2F%26lt%3B%26lt%3B%2Foutput '<<' output operator]. [http://www.byond.com/members/?command=reference&path=world world] [http://www.byond.com/members/?command=reference&path=operator%2F%26lt%3B%26lt%3B <<] "Hello World!" //Outputs a message to all clients in the world [http://www.byond.com/members/?command=reference&path=proc%2Fvar%2Fusr usr] [http://www.byond.com/members/?command=reference&path=operator%2F%26lt%3B%26lt%3B <<] "Hello usr" //Outputs a message to only the user who is tied to the calling of the proc which contains this. ===== Output with variables ===== var/s1 = "Hello" var/s2 = "World" var/i = 2011 [http://www.byond.com/members/?command=reference&path=world world] [http://www.byond.com/members/?command=reference&path=operator%2F%26lt%3B%26lt%3B <<] "[s1] [s2], this guide was written in [i]" //Returns "Hello World, this guide was written in 2011" ==== Determining variable types in code ==== The [http://www.byond.com/members/?command=reference&path=proc%2Fistype istype()] proc will come in handy Example var/obj/item/weapon/W = new/obj/item/weapon/baton if([http://www.byond.com/members/?command=reference&path=proc%2Fistype istype](W,/obj/item/weapon/baton)) [http://www.byond.com/members/?command=reference&path=world world] << "It's a baton!" The second argument is optional, if it's omitted, the variable will be checked against its declared type, like var/obj/item/weapon/W = new/obj/item/weapon/baton if([http://www.byond.com/members/?command=reference&path=proc%2Fistype istype](W)) [http://www.byond.com/members/?command=reference&path=world world] << "It's a weapon!" Standard code for getting specific arguments from variables which have a type that is a subclass of the type the current proc treats them with (see any attackby() proc for examples). Note that the example below is of a proc which is globaly defined, not tied to the object. It doesn't make much sense to do it like this but it works for the purposes of the example. /obj objects don't have the 'amount' variable, it's defined in /obj/item/stack (as ilustrated by the oversimplified definition of classes below. Also note where var/ is used and where it isn't). /obj var/name = "Object" /obj/item name = "Item" /obj/item/stack name = "Stack" var/amount = 50 proc/get_amount(var/obj/O) if([http://www.byond.com/members/?command=reference&path=proc%2Fistype istype](O,/obj/item/stack)) var/obj/item/stack/S = O return [http://www.byond.com/members/?command=reference&path=operator%2F%40dt%3B S.amount] There is another way of doing this. I'll show you that it exists but it is NOT TO BE USED. proc/get_aount(var/obj/S) if([http://www.byond.com/members/?command=reference&path=proc%2Fistype istype](O,/obj/item/stack)) return [http://www.byond.com/members/?command=reference&path=operator%2F%3A O:amount] The [http://www.byond.com/members/?command=reference&path=operator%2F%3A colon operator] (:) in the example above tells the byond compiler: "I know what I'm doing, so ignore the fact the object doesn't have this variable, I'll make sure it works myself." The problem is that people will revise your code and use it in ways you never planed for. This means something might eventually make the O:amount throw exceptions in the form of runtime errors. Some variables might eventually need to be removed or replaced and this is impossible when they are used with object:variable as the compiler will not throw an error when the variable is removed. The error will only become apparent after the game is ran on the live server which might cause it to crash. So just don't use this method, ever. There are some shortcuts to [http://www.byond.com/members/?command=reference&path=proc%2Fistype istype()] proc:<br> [http://www.byond.com/members/?command=reference&path=proc%2Fisarea isarea(variable)] = istype(variable, /area)<br> [http://www.byond.com/members/?command=reference&path=proc%2Fisicon isicon(variable)] = istype(variable, /icon)<br> [http://www.byond.com/members/?command=reference&path=proc%2Fismob ismob(variable)] = istype(variable, /mob)<br> [http://www.byond.com/members/?command=reference&path=proc%2Fisobj isobj(variable)] = istype(variable, /obj)<br> [http://www.byond.com/members/?command=reference&path=proc%2Fisturf isturf(variable)] = istype(variable, /turf)<br> [http://www.byond.com/members/?command=reference&path=proc%2Fisloc isloc(variable)] = ismob(variable) || isobj(variable) || isturf(variable) || isarea(variable) ==== Switching between variable types in code ==== Byond defined:<br> [http://www.byond.com/members/?command=reference&path=proc%2Fascii2text ascii2text]<br> [http://www.byond.com/members/?command=reference&path=proc%2Ffile2text file2text]<br> [http://www.byond.com/members/?command=reference&path=proc%2Flist2params list2params]<br> [http://www.byond.com/members/?command=reference&path=proc%2Fnum2text num2text]<br> [http://www.byond.com/members/?command=reference&path=proc%2Fparams2list params2list]<br> [http://www.byond.com/members/?command=reference&path=proc%2Ftext2ascii text2ascii]<br> [http://www.byond.com/members/?command=reference&path=proc%2Ftext2file text2file]<br> [http://www.byond.com/members/?command=reference&path=proc%2Ftext2num text2num]<br> [http://www.byond.com/members/?command=reference&path=proc%2Ftext2path text2path]<br> [http://www.byond.com/members/?command=reference&path=proc%2Ftime2text time2text]<br> SS13 Defined:<br> text2dir(direction)<br> dir2text(direction)<br> dd_file2list(file_path, separator)<br> dd_text2list(text, separator, var/list/withinList)<br> dd_text2List(text, separator, var/list/withinList)<br> dd_list2text(var/list/the_list, separator)<br> angle2dir(var/degree)<br> angle2text(var/degree)<br> (Defined in [https://github.com/tgstation/-tg-station/blob/master/code/__HELPERS/type2type.dm code/__HELPERS/type2type.dm]) For more useful procs see<br> [https://github.com/tgstation/-tg-station/tree/master/code/__HELPERS code/__HELPERS/] <br> <s>[http://code.google.com/p/tgstation13/source/browse/trunk/code/defines/procs/helpers.dm code/defines/procs/helpers.dm]</s> (Dead link)<br> <s>[http://code.google.com/p/tgstation13/source/browse/trunk/code/game/objects/items/helper_procs.dm code/game/objects/items/helper_procs.dm]</s> (Dead link) <br>
Summary:
Please note that all contributions to /VM/station wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
/VM/station wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width