Sie sind auf Seite 1von 17

Introduction GDScript is a high level, dynamically typed programming language used to create content.

It uses a syntax that is very similar to the Python language (blocks are indentbased and it!s goal is to be very optimal and tigthly integrated "ith the engine, allo"ing great #lexibility #or content creation and integration. $istory Initially, Godot "as designed to support multiple scripting languages (this ability still exists today . $o"ever, only GDScript is in use right no". %here is a little history behind this. In the early days, the engine used the &ua' scripting language. &ua is #ast, but creating bindings to an ob(ect oriented system (by using #allbacks "as complex and slo" and took an enormous amount o# code. )#ter some experiments "ith Python', it also proved di##icult to embed. %he last third party scripting language that "as used #or shipped games "as S*uirrel', but it "as also dropped too. )t that point, it became evident that Godot "ould "ork more optimally by using a built-in scripting language, as the #ollo"ing barriers "ere met+ Godot embeds scripts in nodes, most languages are not designed "ith this in mind. Godot uses several built-in datatypes #or ,D and -D math, script languages do not provide this, and binding them is ine##icient. Godot uses threads heavily #or li#ting and initiali.ing data #rom the net or disk, script interpreters #or common languages are not #riendly to this. Godot already has a memory management model #or resources, most script languages provide their o"n, "hich resulted in duplicate e##ort and bugs. /inding code is al"ays messy and results in several #ailure points, unexpected bugs and general unmaintaniability. 0inally, GDScript "as "ritten. %he language and interpreter #or it ended up being smaller than the binding code itsel# #or &ua and S*uirrel, and e*ually as #unctional. 1ith time, having a built in language ended up being a huge advantage. 2xample Some people can learn better by (ust taking a look at the syntax, so here!s a simple example o# ho" it looks. 3a #ile is a class4

3 inheritance

extends /ase5lass

3 member variables

var a67 var s68$ello8 var arr69:,,,-; var dict6<8key8+8value8, ,+-=

3 constants

const ans"er6>, const thename685harly8

3 built-in vector types

var v, 6 ?ector,(:,, var v- 6 ?ector-(:,,,-

3 #unction

#unc some@#unction(param:,param, +

var local@var67

i# param: A local@var+ print(param: eli# param, B 7+ print(param, else+ print(8#ail48

#or i in range(,C + print(i

"hile(param,46C + param,-6:

var local@var, 6 param:Dreturn local@var,

3 subclass

class Something+

var a6:C

3 constructor

#unc @init( + print(8constructed48 var lv 6 Something.ne"( print(lv.a

3pass

#unc empty#unc( + pass 3 pass does nothing, allo"s an empty #unction

&anguage Identi#iers )ny string that restricts itsel# to alphabetic characters (!a! to !.! and !)! to !E! , digits (!C! to !F! and !@! *uali#ies as an identi#ier. )s an extra restriction, identi#iers must not begin "ith a digit. Identi#iers are case-sensitive (!#oo! is di##erent to !0GG! . Hey"ords %he #ollo"ing is the list o# key"ords supported by the language. Since key"ords are reserved "ords (tokens , they can!t be used as identi#iers+ and break class const continue eli# else enum export extends #alse #or #unc i# in null or pass return sel# tool true var "hile Gperators

%he #ollo"ing is the list o# supported operators and their precedence (%GDG, change since this "as made to re#lect python operators Gperator x9index; x.attribute extends J -x LMN DAA BB P Q S A B 66 46 B6 A6 in 4 not and PP or SS Description Subscription, $ighest Priority )ttribute Ie#erence Instance %ype 5hecker /it"ise KG% Kegative Oult M Div M Iemainder )ddition M Substraction /it Shi#ting /it"ise )KD /it"ise RGI /it"ise GI 5omparisons 5ontent %est /oolean KG% /oolean )KD /oolean GI

6 D6 -6 L6 M6 N6 P6 )ssignment, &o"est Priority S6 &iterals &iteral >7 CxT07: -.:>, 7T.:e-:C !$ello!, V$iW Kame /ase :C Integer /ase :U (hex Integer 0loating Point Kumber (real Strings

XVKodeM&abel Kode Path or StringKame W

5omments )nything #rom a !3! to the end o# the line is ignored and is considered a comment. 3 %his is a comment /uilt-in %ypes /asic /ult-In %ypes ) variable in GDScript can be assigned many o# several built-in types. null !null! is a data type that contains no in#ormation, nothing assigned, and it!s (ust empy. It can only be set to one value+ !null!. bool /oolean data type, can only contain !true! or !#alse!. int Integer data type, can only contain integer numbers, negative and positive. #loat contains a #loating point value (real . String Se*uence o# characters in unicode #ormat. Strings can contain the standard 5 escape se*uences. ?ector /uilt-In %ypes ?ector,MSi.e, ,D vector type, containing x and y #ields. 5an alternatively access #ields as "idth and height #or readability. 5an also be accessed as array. Iect, ,D Iectangle type. 5ontains , vectors #ields, VposW and si.e!!. )lternatively contains an VendW #ield "hich is VposDsi.eW. ?ector-D vector type. 5ontains x, y and . #ields. 5an also be accessed as array.

Oatrix-, -Y, matrix used #or ,D trans#orms. Plane -D Plane type in normali.ed #orm. 5ontains a VnormalW vector #ield and a VdW scalar distance. Zuat Zuaternion, datatype used #or representing a -D rotation. It!s use#ul #or interpolating rotations. ))//M/ox)xis )ligned bounding box (or alternatively, -D box . 5ontains , vectors #ields, VposW and si.e!!. )lternatively contains an VendW #ield "hich is VposDsi.eW. Oatrix-Y- matrix used #or -D rotation and scale. 5ontains - vector #ields x,y and .. 5an also be accessed as array o# -D vectors. %rans#orm -D %rans#orm, contains a Oatrix- #ield VbasisW and a ?ector- #ield VoriginW. 2ngine /uilt-In %ypes 5olor 5olor datatype, contains r,g,b,a #ields. 5an also be accessed as h,s,v #or hueMsaturationMvalue. Image 5ontains a ,D Image o# custom #ormat and allo"s direct access to the pixels. KodePath 5ompiled path to a node, used mainly in the scene system. 5an be easily asigned #romMto a String. IID Iesource ID (IID . Servers use generic IIDs to re#erence opa*ue data. Gb(ect

/ase class #or anything not a built-in type. Input2vent 2vents #rom input devices are contained in very compact #orm in Input2vent ob(ects. Due to #act they can be received in high amounts #rom #rame to #rame, they are optimi.ed in their o"n datatype. 5ontainer /uilt-In %ypes )rray Generic se*uence o# ob(ects. It!s si.e can be changed to anything and starts #rom index C. var arr69; arr69:,,,-; arr9C;68$i48 )rrays are allocated linearly in memory, so they are #ast, but very large arrays (more than tens o# thousands o# elements may cause #ragmentation. %here are speciali.ed arrays (listed belo" #or some built-in datatypes "hich do not su##er #rom this and use less memory, but they are atomic and generally run a little slo"er, so they are only (usti#ied #or very large amount o# data. Dictionary )ssociative container "hich contains values re#erenced by uni*ue keys. var d6<>+7, 8a key8+8a value8, ,T+9:,,,-;= d98$i48;6C &ua-style table syntax is also supported, given it[s easier to "rite and read+ var d6 < somekey6,, otherkey69,,-,>;, morekey68$ello8

= /yte)rray )rray o# bytes. 5an only contains bytes (integers #rom C to ,77 . Gptimi.ed #or memory usage, can!t #ragment the memory. Int)rray )rray o# integers. 5an only contain integers. Gptimi.ed #or memory usage, can!t #ragment the memory. 0loat)rray )rray o# #loats, can only contain #loats. Gptimi.ed #or memory usage, can!t #ragment the memory. String)rray )rray o# strings, can only contain strings. Gptimi.ed #or memory usage, can!t #ragment the memory. ?ector,)rray )rray o# ?ector,, can only contain ,D ?ectors. Gptimi.ed #or memory usage, can!t #ragment the memory. ?ector-)rray )rray o# ?ector-, can only contain -D ?ectors. Gptimi.ed #or memory usage, can!t #ragment the memory. 5olor)rray )rray o# 5olor, can only contains colors. Gptimi.ed #or memory usage, can!t #ragment the memory. Data ?ariables ?ariables can exist as class members or local to #unctions. %hey are created "ith the VvarW key"ord and may be, optionally, be assigned a value upon initiali.ation. var a 3 datatype is null by de#ault var b 6 7

var c 6 -.T var d 6 bDc 3 variables are al"ays initiali.ed in order 5onstants 5onstants are similar to variables, but must be constants or constant expressions and must be assigned on initiali.ation. const a 6 7 const b 6 ?ector,(,C,,C const c 6 :CD,C 3 constant expression const d 6 ?ector,(,C,-C .x 3 constant expression+ ,C const e 6 9:,,,-,>;9C; 3 constant expression+ : const # 6 sin(,C 3 sin( can be used in constant expression const g 6 xD,C 3 invalid, not a constant expression4 0unctions 0unctions al"ays belong to a class. %he scope priority #or variable look-up is+ local \ class member \ global. Vsel#W is provided as an option #or accessing class members but is not re*uired al"ays (and must not be de#ined as #irst parameter, like in Python . 0or per#ormance reasons, #unctions are not considered class members, so they can!t be re#erenced directly. ) #unction can return at any point. %he de#ault return value is null. #unc my#unction(a,b + print(a print(b return aDb 3 return is optional, other"ise null is returned Statements and 5ontrol 0lo" Statements are standard, and can be assignments, #unction calls, control #lo" structures, etc (see belo" . V]W as separator is entirely optional. i#MelseMeli#

Simple conditions are created by using the i#MelseMeli# syntax. Parenthesis around statements is allo"ed but not re*uiered. Given the nature o# the tab-based indentation, eli# can be used instead o# else+Mi#+ to mantain a level o# indentation. i# 9expression;+ statement(s eli# 9expression;+ statement(s else+ statement(s "hile Simple loops are created by using "hile syntax. &oops can be broken using break, or continued using continue+ "hile 9expression;+ statement(s #or %o iterate a range, array or table a #or loop is used. 0or loops store the index in the loop variable on each iteration. #or i in 9C,:,,;+ statement 3 loop iterates - times, i being C,: and ,

var dict 6 <8a8+C, 8b8+:, 8c8+ ,= #or i in dict+ print(dict9i; 3 loop iterates the keys, i being 8a8,8b8 and c8. It prints C, : and ,.

#or i in range(- + statement 3 similar to 9C,:,,; but does not allocate an array

#or i in range(:,- + statement 3 similar to 9:,,; but does not allocate an array

#or i in range(,,T,, + statement 3 similar to 9,,>,U; but does not allocate an array 5lasses /y de#ault, the body o# a script #ile is an unnamed class, and it can only be re#erenced externally as a resource or #ile. 5lass syntax is meant to be very compact and can only contain member variables or #unctions. Static #unctions are allo"ed, but not static members (in the spirit o# thread sa#ety, since scripts can be initiali.ed in separate threads "ithout the user kno"ing . In the same "ay, member variables (including arrays and dictionaries are initiali.ed every time an instance is created. 5lass 0ile 2xample 2xample o# a class #ile, imagine it being stored in a #ile like myclass.gd. var a67

#unc print@value@o#@a( + print(a Inheritance ) class-#ile can inherit #rom a global class, another #ile or a subclass inside another #ile. Oultiple inheritance is not allo"ed. %he VextendsW syntax is used+ 3 extend #rom some class (global extends Some5lass

3 optionally, extend #rom another #ile extends 8some#ile.gd8

3 extend #rom a subclass in another #ile extends 8some#ile.gd8.Subclass Inheritance %esting It is possible to check i# an instance inherits #rom a given class. 0or this the VextendsW key"ord can be used as an operator instead+ const enemy@class 6 preload(8enemy.gd8 3 cache the enemy class

9..;

i# ( entity extends enemy@class + entity.apply@damage( 5onstructor ) class can have an optional constructor, a #unction named V@initW that is called "hen the class is instanced. Sub 5lasses ) class #ile can have subclasses. Syntax should be straight#or"ard+ class SomeSub5lass+ var a67 #unc print@value@o#@a( + print(a

#unc @init( + var sc 6 SomeSub5lass.ne"( 3instance by calling built-in ne" sc.print@value@o#@a( 5lasses as Gb(ects It may be desired at some point to load a class #rom a #ile and then instance it. Since the global scope does not exist, classes must be loaded as a resource. Instancing is done by calling the Vne"W #unction in a class ob(ect+ 3load the class (loaded every time the script is instanced var Oy5lass 6 load(8myclass.gd8

3 alternatively, using the preload( #unction preloads the class at compile time var Oy5lass, 6 preload(8myclass.gd8

#unc @init( + var a 6 Oy5lass.ne"( a.some#unction( 2xports 5lass members can be exported. %his means their value gets saved along "ith a scene. I# class members have initiali.ers to constant expressions, they "ill be available #or editing in the property editor. 2xporting is done by using the export key"ord+ extends /utton

export var data 3 value "ill be saved export var number67 3 also available to the property editor

Gne o# the #undamental bene#its o# exporting member variables is to have them visible in the property editor. %his "ay artists and game designers can modi#y values that later in#luence ho" the program runs. 0or this, a special export syntax is provided #or more detail in the exported variables+ 3i# the exported value assigns a constant or constant expression, the type "ill be in#ered and used in the editor

export var number67

3 export can take a basic datatype as argument, "hich "ill be used in the editor

export(int var number

3 export can also take a resource type as hint

export(%exture var character@#ace

3 integers and strings hint enumerated values

export(int,81arrior8,8Oagician8,8%hie#8 var character@class 3 (editor "ill set them as C,: and , export(String,8Iebecca8,8Oary8,8&eah8 var character@name

3 strings as paths

export(String,0I&2 var # 3 string is a path to a #ile export(String,DII var # 3 string is a path to a directory export(String,0I&2,8L.txt8 var # 3 string is a path to a #ile, custom #ilter provided as hint

3 integers and #loats hint ranges

export(int,,C var i 3 C to ,C allo"ed export(int,-:C,,C var ( 3 -:C to ,C allo"ed export(#loat,-:C,,C,C., var k 3 -:C to ,C allo"ed, "ith stepping o# C.,

3 color can hint availability o# alpha

export(5olor,IG/ var col 3 5olor is IG/ export(5olor,IG/) var col 3 5olor is IG/) It must be noted that even i# the script is not being run "hile at the editor, the exported properties are still editable (see belo" #or VtoolW . Static 0unctions ) #unction can be declared static. 1hen static, it has no access to the instance member variables or Vsel#W. %his is mainly use#ul to make libraries o# helper #unctions+ static #unc sum,(a,b + return aDb %ool Oode Scripts by de#ault don!t run inside the editor, and only the exported properties can be changed. In some cases it is desired that they do (as long as they don!t execute game code or manually avoid doing so . 0or this, the VtoolW key"ord exists, and must be placed at

the top o# the #ile+ tool extends /utton

#unc @init( + print(8$ello8 Oemory Oanagement I# a class inherits #rom Ie#erence, then instances "ill be #reed "hen no longer in use. Ko garbage collector exists, (ust simple re#erence counting. /y de#ault, all classes that don!t de#ine inheritance extend Ie#erence. I# this is not desired, then a class must inherit Gb(ect manually and must call instance.#ree( . %o avoid re#erence cycles that can!t be #reed, a "eakre#( #unction is provided #or creating "eak re#erences. 0unction Ie#erences 0unctions can!t be re#erenced because they are not treated as class members. %here are t"o alternatives to this, though. %he VcallW #unction or the #uncre#( helper. instance.call(8#uncname8,args 3 call a #unction by bane

var #r 6 #uncre#(instance,8#uncname8 3create a #unction re# #r.exec(args

Das könnte Ihnen auch gefallen