variant.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "variant.h"
  31. #include "core_string_names.h"
  32. #include "io/marshalls.h"
  33. #include "math_funcs.h"
  34. #include "print_string.h"
  35. #include "resource.h"
  36. #include "scene/gui/control.h"
  37. #include "scene/main/node.h"
  38. #include "variant_parser.h"
  39. String Variant::get_type_name(Variant::Type p_type) {
  40. switch (p_type) {
  41. case NIL: {
  42. return "Nil";
  43. } break;
  44. // atomic types
  45. case BOOL: {
  46. return "bool";
  47. } break;
  48. case INT: {
  49. return "int";
  50. } break;
  51. case REAL: {
  52. return "float";
  53. } break;
  54. case STRING: {
  55. return "String";
  56. } break;
  57. // math types
  58. case VECTOR2: {
  59. return "Vector2";
  60. } break;
  61. case RECT2: {
  62. return "Rect2";
  63. } break;
  64. case MATRIX32: {
  65. return "Matrix32";
  66. } break;
  67. case VECTOR3: {
  68. return "Vector3";
  69. } break;
  70. case PLANE: {
  71. return "Plane";
  72. } break;
  73. /*
  74. case QUAT: {
  75. } break;*/
  76. case _AABB: {
  77. return "AABB";
  78. } break;
  79. case QUAT: {
  80. return "Quat";
  81. } break;
  82. case MATRIX3: {
  83. return "Matrix3";
  84. } break;
  85. case TRANSFORM: {
  86. return "Transform";
  87. } break;
  88. // misc types
  89. case COLOR: {
  90. return "Color";
  91. } break;
  92. case IMAGE: {
  93. return "Image";
  94. } break;
  95. case _RID: {
  96. return "RID";
  97. } break;
  98. case OBJECT: {
  99. return "Object";
  100. } break;
  101. case NODE_PATH: {
  102. return "NodePath";
  103. } break;
  104. case INPUT_EVENT: {
  105. return "InputEvent";
  106. } break;
  107. case DICTIONARY: {
  108. return "Dictionary";
  109. } break;
  110. case ARRAY: {
  111. return "Array";
  112. } break;
  113. // arrays
  114. case RAW_ARRAY: {
  115. return "RawArray";
  116. } break;
  117. case INT_ARRAY: {
  118. return "IntArray";
  119. } break;
  120. case REAL_ARRAY: {
  121. return "RealArray";
  122. } break;
  123. case STRING_ARRAY: {
  124. return "StringArray";
  125. } break;
  126. case VECTOR2_ARRAY: {
  127. return "Vector2Array";
  128. } break;
  129. case VECTOR3_ARRAY: {
  130. return "Vector3Array";
  131. } break;
  132. case COLOR_ARRAY: {
  133. return "ColorArray";
  134. } break;
  135. default: {}
  136. }
  137. return "";
  138. }
  139. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  140. if (p_type_from == p_type_to)
  141. return true;
  142. if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
  143. return true;
  144. if (p_type_from == NIL) {
  145. return (p_type_to == OBJECT);
  146. };
  147. const Type *valid_types = NULL;
  148. const Type *invalid_types = NULL;
  149. switch (p_type_to) {
  150. case BOOL: {
  151. static const Type valid[] = {
  152. INT,
  153. REAL,
  154. STRING,
  155. NIL,
  156. };
  157. valid_types = valid;
  158. } break;
  159. case INT: {
  160. static const Type valid[] = {
  161. BOOL,
  162. REAL,
  163. STRING,
  164. NIL,
  165. };
  166. valid_types = valid;
  167. } break;
  168. case REAL: {
  169. static const Type valid[] = {
  170. BOOL,
  171. INT,
  172. STRING,
  173. NIL,
  174. };
  175. valid_types = valid;
  176. } break;
  177. case STRING: {
  178. static const Type invalid[] = {
  179. OBJECT,
  180. IMAGE,
  181. NIL
  182. };
  183. invalid_types = invalid;
  184. } break;
  185. case MATRIX32: {
  186. static const Type valid[] = {
  187. TRANSFORM,
  188. NIL
  189. };
  190. valid_types = valid;
  191. } break;
  192. case QUAT: {
  193. static const Type valid[] = {
  194. MATRIX3,
  195. NIL
  196. };
  197. valid_types = valid;
  198. } break;
  199. case MATRIX3: {
  200. static const Type valid[] = {
  201. QUAT,
  202. NIL
  203. };
  204. valid_types = valid;
  205. } break;
  206. case TRANSFORM: {
  207. static const Type valid[] = {
  208. MATRIX32,
  209. QUAT,
  210. MATRIX3,
  211. NIL
  212. };
  213. valid_types = valid;
  214. } break;
  215. case COLOR: {
  216. static const Type valid[] = {
  217. STRING,
  218. INT,
  219. NIL,
  220. };
  221. valid_types = valid;
  222. } break;
  223. case _RID: {
  224. static const Type valid[] = {
  225. OBJECT,
  226. NIL
  227. };
  228. valid_types = valid;
  229. } break;
  230. case OBJECT: {
  231. static const Type valid[] = {
  232. NIL
  233. };
  234. valid_types = valid;
  235. } break;
  236. case NODE_PATH: {
  237. static const Type valid[] = {
  238. STRING,
  239. NIL
  240. };
  241. valid_types = valid;
  242. } break;
  243. case ARRAY: {
  244. static const Type valid[] = {
  245. RAW_ARRAY,
  246. INT_ARRAY,
  247. STRING_ARRAY,
  248. REAL_ARRAY,
  249. COLOR_ARRAY,
  250. VECTOR2_ARRAY,
  251. VECTOR3_ARRAY,
  252. NIL
  253. };
  254. valid_types = valid;
  255. } break;
  256. // arrays
  257. case RAW_ARRAY: {
  258. static const Type valid[] = {
  259. ARRAY,
  260. NIL
  261. };
  262. valid_types = valid;
  263. } break;
  264. case INT_ARRAY: {
  265. static const Type valid[] = {
  266. ARRAY,
  267. NIL
  268. };
  269. valid_types = valid;
  270. } break;
  271. case REAL_ARRAY: {
  272. static const Type valid[] = {
  273. ARRAY,
  274. NIL
  275. };
  276. valid_types = valid;
  277. } break;
  278. case STRING_ARRAY: {
  279. static const Type valid[] = {
  280. ARRAY,
  281. NIL
  282. };
  283. valid_types = valid;
  284. } break;
  285. case VECTOR2_ARRAY: {
  286. static const Type valid[] = {
  287. ARRAY,
  288. NIL
  289. };
  290. valid_types = valid;
  291. } break;
  292. case VECTOR3_ARRAY: {
  293. static const Type valid[] = {
  294. ARRAY,
  295. NIL
  296. };
  297. valid_types = valid;
  298. } break;
  299. case COLOR_ARRAY: {
  300. static const Type valid[] = {
  301. ARRAY,
  302. NIL
  303. };
  304. valid_types = valid;
  305. } break;
  306. default: {}
  307. }
  308. if (valid_types) {
  309. int i = 0;
  310. while (valid_types[i] != NIL) {
  311. if (p_type_from == valid_types[i])
  312. return true;
  313. i++;
  314. }
  315. } else if (invalid_types) {
  316. int i = 0;
  317. while (invalid_types[i] != NIL) {
  318. if (p_type_from == invalid_types[i])
  319. return false;
  320. i++;
  321. }
  322. return true;
  323. }
  324. return false;
  325. }
  326. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  327. if (p_type_from == p_type_to)
  328. return true;
  329. if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
  330. return true;
  331. if (p_type_from == NIL) {
  332. return (p_type_to == OBJECT);
  333. };
  334. const Type *valid_types = NULL;
  335. switch (p_type_to) {
  336. case BOOL: {
  337. static const Type valid[] = {
  338. INT,
  339. REAL,
  340. //STRING,
  341. NIL,
  342. };
  343. valid_types = valid;
  344. } break;
  345. case INT: {
  346. static const Type valid[] = {
  347. BOOL,
  348. REAL,
  349. //STRING,
  350. NIL,
  351. };
  352. valid_types = valid;
  353. } break;
  354. case REAL: {
  355. static const Type valid[] = {
  356. BOOL,
  357. INT,
  358. //STRING,
  359. NIL,
  360. };
  361. valid_types = valid;
  362. } break;
  363. case STRING: {
  364. static const Type valid[] = {
  365. NODE_PATH,
  366. NIL
  367. };
  368. valid_types = valid;
  369. } break;
  370. case MATRIX32: {
  371. static const Type valid[] = {
  372. TRANSFORM,
  373. NIL
  374. };
  375. valid_types = valid;
  376. } break;
  377. case QUAT: {
  378. static const Type valid[] = {
  379. MATRIX3,
  380. NIL
  381. };
  382. valid_types = valid;
  383. } break;
  384. case MATRIX3: {
  385. static const Type valid[] = {
  386. QUAT,
  387. NIL
  388. };
  389. valid_types = valid;
  390. } break;
  391. case TRANSFORM: {
  392. static const Type valid[] = {
  393. MATRIX32,
  394. QUAT,
  395. MATRIX3,
  396. NIL
  397. };
  398. valid_types = valid;
  399. } break;
  400. case COLOR: {
  401. static const Type valid[] = {
  402. STRING,
  403. INT,
  404. NIL,
  405. };
  406. valid_types = valid;
  407. } break;
  408. case _RID: {
  409. static const Type valid[] = {
  410. OBJECT,
  411. NIL
  412. };
  413. valid_types = valid;
  414. } break;
  415. case OBJECT: {
  416. static const Type valid[] = {
  417. NIL
  418. };
  419. valid_types = valid;
  420. } break;
  421. case NODE_PATH: {
  422. static const Type valid[] = {
  423. STRING,
  424. NIL
  425. };
  426. valid_types = valid;
  427. } break;
  428. case ARRAY: {
  429. static const Type valid[] = {
  430. RAW_ARRAY,
  431. INT_ARRAY,
  432. STRING_ARRAY,
  433. REAL_ARRAY,
  434. COLOR_ARRAY,
  435. VECTOR2_ARRAY,
  436. VECTOR3_ARRAY,
  437. NIL
  438. };
  439. valid_types = valid;
  440. } break;
  441. // arrays
  442. case RAW_ARRAY: {
  443. static const Type valid[] = {
  444. ARRAY,
  445. NIL
  446. };
  447. valid_types = valid;
  448. } break;
  449. case INT_ARRAY: {
  450. static const Type valid[] = {
  451. ARRAY,
  452. NIL
  453. };
  454. valid_types = valid;
  455. } break;
  456. case REAL_ARRAY: {
  457. static const Type valid[] = {
  458. ARRAY,
  459. NIL
  460. };
  461. valid_types = valid;
  462. } break;
  463. case STRING_ARRAY: {
  464. static const Type valid[] = {
  465. ARRAY,
  466. NIL
  467. };
  468. valid_types = valid;
  469. } break;
  470. case VECTOR2_ARRAY: {
  471. static const Type valid[] = {
  472. ARRAY,
  473. NIL
  474. };
  475. valid_types = valid;
  476. } break;
  477. case VECTOR3_ARRAY: {
  478. static const Type valid[] = {
  479. ARRAY,
  480. NIL
  481. };
  482. valid_types = valid;
  483. } break;
  484. case COLOR_ARRAY: {
  485. static const Type valid[] = {
  486. ARRAY,
  487. NIL
  488. };
  489. valid_types = valid;
  490. } break;
  491. default: {}
  492. }
  493. if (valid_types) {
  494. int i = 0;
  495. while (valid_types[i] != NIL) {
  496. if (p_type_from == valid_types[i])
  497. return true;
  498. i++;
  499. }
  500. }
  501. return false;
  502. }
  503. bool Variant::operator==(const Variant &p_variant) const {
  504. if (type != p_variant.type) //evaluation of operator== needs to be more strict
  505. return false;
  506. bool v;
  507. Variant r;
  508. evaluate(OP_EQUAL, *this, p_variant, r, v);
  509. return r;
  510. }
  511. bool Variant::operator!=(const Variant &p_variant) const {
  512. if (type != p_variant.type) //evaluation of operator== needs to be more strict
  513. return true;
  514. bool v;
  515. Variant r;
  516. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  517. return r;
  518. }
  519. bool Variant::operator<(const Variant &p_variant) const {
  520. if (type != p_variant.type) //if types differ, then order by type first
  521. return type < p_variant.type;
  522. bool v;
  523. Variant r;
  524. evaluate(OP_LESS, *this, p_variant, r, v);
  525. return r;
  526. }
  527. bool Variant::is_zero() const {
  528. switch (type) {
  529. case NIL: {
  530. return true;
  531. } break;
  532. // atomic types
  533. case BOOL: {
  534. return _data._bool == false;
  535. } break;
  536. case INT: {
  537. return _data._int == 0;
  538. } break;
  539. case REAL: {
  540. return _data._real == 0;
  541. } break;
  542. case STRING: {
  543. return *reinterpret_cast<const String *>(_data._mem) == String();
  544. } break;
  545. // math types
  546. case VECTOR2: {
  547. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  548. } break;
  549. case RECT2: {
  550. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  551. } break;
  552. case MATRIX32: {
  553. return *_data._matrix32 == Matrix32();
  554. } break;
  555. case VECTOR3: {
  556. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  557. } break;
  558. case PLANE: {
  559. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  560. } break;
  561. /*
  562. case QUAT: {
  563. } break;*/
  564. case _AABB: {
  565. return *_data._aabb == AABB();
  566. } break;
  567. case QUAT: {
  568. return *reinterpret_cast<const Quat *>(_data._mem) == Quat();
  569. } break;
  570. case MATRIX3: {
  571. return *_data._matrix3 == Matrix3();
  572. } break;
  573. case TRANSFORM: {
  574. return *_data._transform == Transform();
  575. } break;
  576. // misc types
  577. case COLOR: {
  578. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  579. } break;
  580. case IMAGE: {
  581. return _data._image->empty();
  582. } break;
  583. case _RID: {
  584. return *reinterpret_cast<const RID *>(_data._mem) == RID();
  585. } break;
  586. case OBJECT: {
  587. return _get_obj().obj == NULL;
  588. } break;
  589. case NODE_PATH: {
  590. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  591. } break;
  592. case INPUT_EVENT: {
  593. return _data._input_event->type == InputEvent::NONE;
  594. } break;
  595. case DICTIONARY: {
  596. return reinterpret_cast<const Dictionary *>(_data._mem)->empty();
  597. } break;
  598. case ARRAY: {
  599. return reinterpret_cast<const Array *>(_data._mem)->empty();
  600. } break;
  601. // arrays
  602. case RAW_ARRAY: {
  603. return reinterpret_cast<const DVector<uint8_t> *>(_data._mem)->size() == 0;
  604. } break;
  605. case INT_ARRAY: {
  606. return reinterpret_cast<const DVector<int> *>(_data._mem)->size() == 0;
  607. } break;
  608. case REAL_ARRAY: {
  609. return reinterpret_cast<const DVector<real_t> *>(_data._mem)->size() == 0;
  610. } break;
  611. case STRING_ARRAY: {
  612. return reinterpret_cast<const DVector<String> *>(_data._mem)->size() == 0;
  613. } break;
  614. case VECTOR2_ARRAY: {
  615. return reinterpret_cast<const DVector<Vector2> *>(_data._mem)->size() == 0;
  616. } break;
  617. case VECTOR3_ARRAY: {
  618. return reinterpret_cast<const DVector<Vector3> *>(_data._mem)->size() == 0;
  619. } break;
  620. case COLOR_ARRAY: {
  621. return reinterpret_cast<const DVector<Color> *>(_data._mem)->size() == 0;
  622. } break;
  623. default: {}
  624. }
  625. return false;
  626. }
  627. bool Variant::is_one() const {
  628. switch (type) {
  629. case NIL: {
  630. return true;
  631. } break;
  632. // atomic types
  633. case BOOL: {
  634. return _data._bool == true;
  635. } break;
  636. case INT: {
  637. return _data._int == 1;
  638. } break;
  639. case REAL: {
  640. return _data._real == 1;
  641. } break;
  642. case VECTOR2: {
  643. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  644. } break;
  645. case RECT2: {
  646. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  647. } break;
  648. case VECTOR3: {
  649. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  650. } break;
  651. case PLANE: {
  652. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  653. } break;
  654. case COLOR: {
  655. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  656. } break;
  657. default: { return !is_zero(); }
  658. }
  659. return false;
  660. }
  661. void Variant::reference(const Variant &p_variant) {
  662. if (this == &p_variant)
  663. return;
  664. clear();
  665. type = p_variant.type;
  666. switch (p_variant.type) {
  667. case NIL: {
  668. // none
  669. } break;
  670. // atomic types
  671. case BOOL: {
  672. _data._bool = p_variant._data._bool;
  673. } break;
  674. case INT: {
  675. _data._int = p_variant._data._int;
  676. } break;
  677. case REAL: {
  678. _data._real = p_variant._data._real;
  679. } break;
  680. case STRING: {
  681. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  682. } break;
  683. // math types
  684. case VECTOR2: {
  685. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  686. } break;
  687. case RECT2: {
  688. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  689. } break;
  690. case MATRIX32: {
  691. _data._matrix32 = memnew(Matrix32(*p_variant._data._matrix32));
  692. } break;
  693. case VECTOR3: {
  694. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  695. } break;
  696. case PLANE: {
  697. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  698. } break;
  699. /*
  700. case QUAT: {
  701. } break;*/
  702. case _AABB: {
  703. _data._aabb = memnew(AABB(*p_variant._data._aabb));
  704. } break;
  705. case QUAT: {
  706. memnew_placement(_data._mem, Quat(*reinterpret_cast<const Quat *>(p_variant._data._mem)));
  707. } break;
  708. case MATRIX3: {
  709. _data._matrix3 = memnew(Matrix3(*p_variant._data._matrix3));
  710. } break;
  711. case TRANSFORM: {
  712. _data._transform = memnew(Transform(*p_variant._data._transform));
  713. } break;
  714. // misc types
  715. case COLOR: {
  716. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  717. } break;
  718. case IMAGE: {
  719. _data._image = memnew(Image(*p_variant._data._image));
  720. } break;
  721. case _RID: {
  722. memnew_placement(_data._mem, RID(*reinterpret_cast<const RID *>(p_variant._data._mem)));
  723. } break;
  724. case OBJECT: {
  725. memnew_placement(_data._mem, ObjData(p_variant._get_obj()));
  726. } break;
  727. case NODE_PATH: {
  728. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  729. } break;
  730. case INPUT_EVENT: {
  731. _data._input_event = memnew(InputEvent(*p_variant._data._input_event));
  732. } break;
  733. case DICTIONARY: {
  734. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  735. } break;
  736. case ARRAY: {
  737. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  738. } break;
  739. // arrays
  740. case RAW_ARRAY: {
  741. memnew_placement(_data._mem, DVector<uint8_t>(*reinterpret_cast<const DVector<uint8_t> *>(p_variant._data._mem)));
  742. } break;
  743. case INT_ARRAY: {
  744. memnew_placement(_data._mem, DVector<int>(*reinterpret_cast<const DVector<int> *>(p_variant._data._mem)));
  745. } break;
  746. case REAL_ARRAY: {
  747. memnew_placement(_data._mem, DVector<real_t>(*reinterpret_cast<const DVector<real_t> *>(p_variant._data._mem)));
  748. } break;
  749. case STRING_ARRAY: {
  750. memnew_placement(_data._mem, DVector<String>(*reinterpret_cast<const DVector<String> *>(p_variant._data._mem)));
  751. } break;
  752. case VECTOR2_ARRAY: {
  753. memnew_placement(_data._mem, DVector<Vector2>(*reinterpret_cast<const DVector<Vector2> *>(p_variant._data._mem)));
  754. } break;
  755. case VECTOR3_ARRAY: {
  756. memnew_placement(_data._mem, DVector<Vector3>(*reinterpret_cast<const DVector<Vector3> *>(p_variant._data._mem)));
  757. } break;
  758. case COLOR_ARRAY: {
  759. memnew_placement(_data._mem, DVector<Color>(*reinterpret_cast<const DVector<Color> *>(p_variant._data._mem)));
  760. } break;
  761. default: {}
  762. }
  763. }
  764. void Variant::zero() {
  765. switch (type) {
  766. case NIL: break;
  767. case BOOL: this->_data._bool = false; break;
  768. case INT: this->_data._int = 0; break;
  769. case REAL: this->_data._real = 0; break;
  770. case VECTOR2: *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2(); break;
  771. case RECT2: *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2(); break;
  772. case VECTOR3: *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3(); break;
  773. case PLANE: *reinterpret_cast<Plane *>(this->_data._mem) = Plane(); break;
  774. case QUAT: *reinterpret_cast<Quat *>(this->_data._mem) = Quat(); break;
  775. case COLOR: *reinterpret_cast<Color *>(this->_data._mem) = Color(); break;
  776. default: this->clear(); break;
  777. }
  778. }
  779. void Variant::clear() {
  780. switch (type) {
  781. case STRING: {
  782. reinterpret_cast<String *>(_data._mem)->~String();
  783. } break;
  784. /*
  785. // no point, they don't allocate memory
  786. VECTOR3,
  787. PLANE,
  788. QUAT,
  789. COLOR,
  790. VECTOR2,
  791. RECT2
  792. */
  793. case MATRIX32: {
  794. memdelete(_data._matrix32);
  795. } break;
  796. case _AABB: {
  797. memdelete(_data._aabb);
  798. } break;
  799. case MATRIX3: {
  800. memdelete(_data._matrix3);
  801. } break;
  802. case TRANSFORM: {
  803. memdelete(_data._transform);
  804. } break;
  805. // misc types
  806. case IMAGE: {
  807. memdelete(_data._image);
  808. } break;
  809. case NODE_PATH: {
  810. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  811. } break;
  812. case OBJECT: {
  813. _get_obj().obj = NULL;
  814. _get_obj().ref.unref();
  815. } break;
  816. case _RID: {
  817. // not much need probably
  818. reinterpret_cast<RID *>(_data._mem)->~RID();
  819. } break;
  820. case DICTIONARY: {
  821. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  822. } break;
  823. case ARRAY: {
  824. reinterpret_cast<Array *>(_data._mem)->~Array();
  825. } break;
  826. case INPUT_EVENT: {
  827. memdelete(_data._input_event);
  828. } break;
  829. // arrays
  830. case RAW_ARRAY: {
  831. reinterpret_cast<DVector<uint8_t> *>(_data._mem)->~DVector<uint8_t>();
  832. } break;
  833. case INT_ARRAY: {
  834. reinterpret_cast<DVector<int> *>(_data._mem)->~DVector<int>();
  835. } break;
  836. case REAL_ARRAY: {
  837. reinterpret_cast<DVector<real_t> *>(_data._mem)->~DVector<real_t>();
  838. } break;
  839. case STRING_ARRAY: {
  840. reinterpret_cast<DVector<String> *>(_data._mem)->~DVector<String>();
  841. } break;
  842. case VECTOR2_ARRAY: {
  843. reinterpret_cast<DVector<Vector2> *>(_data._mem)->~DVector<Vector2>();
  844. } break;
  845. case VECTOR3_ARRAY: {
  846. reinterpret_cast<DVector<Vector3> *>(_data._mem)->~DVector<Vector3>();
  847. } break;
  848. case COLOR_ARRAY: {
  849. reinterpret_cast<DVector<Color> *>(_data._mem)->~DVector<Color>();
  850. } break;
  851. default: {} /* not needed */
  852. }
  853. type = NIL;
  854. }
  855. Variant::operator signed int() const {
  856. switch (type) {
  857. case NIL: return 0;
  858. case BOOL: return _data._bool ? 1 : 0;
  859. case INT: return _data._int;
  860. case REAL: return _data._real;
  861. case STRING: return operator String().to_int();
  862. default: {
  863. return 0;
  864. }
  865. }
  866. return 0;
  867. }
  868. Variant::operator unsigned int() const {
  869. switch (type) {
  870. case NIL: return 0;
  871. case BOOL: return _data._bool ? 1 : 0;
  872. case INT: return _data._int;
  873. case REAL: return _data._real;
  874. case STRING: return operator String().to_int();
  875. default: {
  876. return 0;
  877. }
  878. }
  879. return 0;
  880. }
  881. Variant::operator int64_t() const {
  882. switch (type) {
  883. case NIL: return 0;
  884. case BOOL: return _data._bool ? 1 : 0;
  885. case INT: return _data._int;
  886. case REAL: return _data._real;
  887. case STRING: return operator String().to_int();
  888. default: {
  889. return 0;
  890. }
  891. }
  892. return 0;
  893. }
  894. /*
  895. Variant::operator long unsigned int() const {
  896. switch( type ) {
  897. case NIL: return 0;
  898. case BOOL: return _data._bool ? 1 : 0;
  899. case INT: return _data._int;
  900. case REAL: return _data._real;
  901. case STRING: return operator String().to_int();
  902. default: {
  903. return 0;
  904. }
  905. }
  906. return 0;
  907. };
  908. */
  909. Variant::operator uint64_t() const {
  910. switch (type) {
  911. case NIL: return 0;
  912. case BOOL: return _data._bool ? 1 : 0;
  913. case INT: return _data._int;
  914. case REAL: return _data._real;
  915. case STRING: return operator String().to_int();
  916. default: {
  917. return 0;
  918. }
  919. }
  920. return 0;
  921. }
  922. #ifdef NEED_LONG_INT
  923. Variant::operator signed long() const {
  924. switch (type) {
  925. case NIL: return 0;
  926. case BOOL: return _data._bool ? 1 : 0;
  927. case INT: return _data._int;
  928. case REAL: return _data._real;
  929. case STRING: return operator String().to_int();
  930. default: {
  931. return 0;
  932. }
  933. }
  934. return 0;
  935. };
  936. Variant::operator unsigned long() const {
  937. switch (type) {
  938. case NIL: return 0;
  939. case BOOL: return _data._bool ? 1 : 0;
  940. case INT: return _data._int;
  941. case REAL: return _data._real;
  942. case STRING: return operator String().to_int();
  943. default: {
  944. return 0;
  945. }
  946. }
  947. return 0;
  948. };
  949. #endif
  950. Variant::operator signed short() const {
  951. switch (type) {
  952. case NIL: return 0;
  953. case BOOL: return _data._bool ? 1 : 0;
  954. case INT: return _data._int;
  955. case REAL: return _data._real;
  956. case STRING: return operator String().to_int();
  957. default: {
  958. return 0;
  959. }
  960. }
  961. return 0;
  962. }
  963. Variant::operator unsigned short() const {
  964. switch (type) {
  965. case NIL: return 0;
  966. case BOOL: return _data._bool ? 1 : 0;
  967. case INT: return _data._int;
  968. case REAL: return _data._real;
  969. case STRING: return operator String().to_int();
  970. default: {
  971. return 0;
  972. }
  973. }
  974. return 0;
  975. }
  976. Variant::operator signed char() const {
  977. switch (type) {
  978. case NIL: return 0;
  979. case BOOL: return _data._bool ? 1 : 0;
  980. case INT: return _data._int;
  981. case REAL: return _data._real;
  982. case STRING: return operator String().to_int();
  983. default: {
  984. return 0;
  985. }
  986. }
  987. return 0;
  988. }
  989. Variant::operator unsigned char() const {
  990. switch (type) {
  991. case NIL: return 0;
  992. case BOOL: return _data._bool ? 1 : 0;
  993. case INT: return _data._int;
  994. case REAL: return _data._real;
  995. case STRING: return operator String().to_int();
  996. default: {
  997. return 0;
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. Variant::operator CharType() const {
  1003. return operator unsigned int();
  1004. }
  1005. Variant::operator float() const {
  1006. switch (type) {
  1007. case NIL: return 0;
  1008. case BOOL: return _data._bool ? 1.0 : 0.0;
  1009. case INT: return (float)_data._int;
  1010. case REAL: return _data._real;
  1011. case STRING: return operator String().to_double();
  1012. default: {
  1013. return 0;
  1014. }
  1015. }
  1016. return 0;
  1017. }
  1018. Variant::operator double() const {
  1019. switch (type) {
  1020. case NIL: return 0;
  1021. case BOOL: return _data._bool ? 1.0 : 0.0;
  1022. case INT: return (float)_data._int;
  1023. case REAL: return _data._real;
  1024. case STRING: return operator String().to_double();
  1025. default: {
  1026. return 0;
  1027. }
  1028. }
  1029. return true;
  1030. }
  1031. Variant::operator StringName() const {
  1032. if (type == NODE_PATH) {
  1033. return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
  1034. }
  1035. return StringName(operator String());
  1036. }
  1037. struct _VariantStrPair {
  1038. String key;
  1039. String value;
  1040. bool operator<(const _VariantStrPair &p) const {
  1041. return key < p.key;
  1042. }
  1043. };
  1044. Variant::operator String() const {
  1045. switch (type) {
  1046. case NIL: return "";
  1047. case BOOL: return _data._bool ? "True" : "False";
  1048. case INT: return String::num(_data._int);
  1049. case REAL: return String::num(_data._real);
  1050. case STRING: return *reinterpret_cast<const String *>(_data._mem);
  1051. case VECTOR2: return "(" + operator Vector2() + ")";
  1052. case RECT2: return "(" + operator Rect2() + ")";
  1053. case MATRIX32: {
  1054. Matrix32 mat32 = operator Matrix32();
  1055. return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
  1056. } break;
  1057. case VECTOR3: return "(" + operator Vector3() + ")";
  1058. case PLANE:
  1059. return operator Plane();
  1060. //case QUAT:
  1061. case _AABB: return operator AABB();
  1062. case QUAT: return "(" + operator Quat() + ")";
  1063. case MATRIX3: {
  1064. Matrix3 mat3 = operator Matrix3();
  1065. String mtx("(");
  1066. for (int i = 0; i < 3; i++) {
  1067. if (i != 0)
  1068. mtx += ", ";
  1069. mtx += "(";
  1070. for (int j = 0; j < 3; j++) {
  1071. if (j != 0)
  1072. mtx += ", ";
  1073. mtx += Variant(mat3.elements[i][j]).operator String();
  1074. }
  1075. mtx += ")";
  1076. }
  1077. return mtx + ")";
  1078. } break;
  1079. case TRANSFORM: return operator Transform();
  1080. case NODE_PATH: return operator NodePath();
  1081. case INPUT_EVENT: return operator InputEvent();
  1082. case COLOR: return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
  1083. case DICTIONARY: {
  1084. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1085. //const String *K=NULL;
  1086. String str;
  1087. List<Variant> keys;
  1088. d.get_key_list(&keys);
  1089. Vector<_VariantStrPair> pairs;
  1090. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1091. _VariantStrPair sp;
  1092. sp.key = String(E->get());
  1093. sp.value = d[E->get()];
  1094. pairs.push_back(sp);
  1095. }
  1096. pairs.sort();
  1097. for (int i = 0; i < pairs.size(); i++) {
  1098. if (i > 0)
  1099. str += ", ";
  1100. str += "(" + pairs[i].key + ":" + pairs[i].value + ")";
  1101. }
  1102. return str;
  1103. } break;
  1104. case VECTOR2_ARRAY: {
  1105. DVector<Vector2> vec = operator DVector<Vector2>();
  1106. String str("[");
  1107. for (int i = 0; i < vec.size(); i++) {
  1108. if (i > 0)
  1109. str += ", ";
  1110. str = str + Variant(vec[i]);
  1111. }
  1112. str += "]";
  1113. return str;
  1114. } break;
  1115. case VECTOR3_ARRAY: {
  1116. DVector<Vector3> vec = operator DVector<Vector3>();
  1117. String str("[");
  1118. for (int i = 0; i < vec.size(); i++) {
  1119. if (i > 0)
  1120. str += ", ";
  1121. str = str + Variant(vec[i]);
  1122. }
  1123. str += "]";
  1124. return str;
  1125. } break;
  1126. case STRING_ARRAY: {
  1127. DVector<String> vec = operator DVector<String>();
  1128. String str("[");
  1129. for (int i = 0; i < vec.size(); i++) {
  1130. if (i > 0)
  1131. str += ", ";
  1132. str = str + vec[i];
  1133. }
  1134. str += "]";
  1135. return str;
  1136. } break;
  1137. case INT_ARRAY: {
  1138. DVector<int> vec = operator DVector<int>();
  1139. String str("[");
  1140. for (int i = 0; i < vec.size(); i++) {
  1141. if (i > 0)
  1142. str += ", ";
  1143. str = str + itos(vec[i]);
  1144. }
  1145. str += "]";
  1146. return str;
  1147. } break;
  1148. case REAL_ARRAY: {
  1149. DVector<real_t> vec = operator DVector<real_t>();
  1150. String str("[");
  1151. for (int i = 0; i < vec.size(); i++) {
  1152. if (i > 0)
  1153. str += ", ";
  1154. str = str + rtos(vec[i]);
  1155. }
  1156. str += "]";
  1157. return str;
  1158. } break;
  1159. case ARRAY: {
  1160. Array arr = operator Array();
  1161. String str("[");
  1162. for (int i = 0; i < arr.size(); i++) {
  1163. if (i)
  1164. str += ", ";
  1165. str += String(arr[i]);
  1166. };
  1167. str += "]";
  1168. return str;
  1169. } break;
  1170. case OBJECT: {
  1171. if (_get_obj().obj) {
  1172. #ifdef DEBUG_ENABLED
  1173. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
  1174. //only if debugging!
  1175. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1176. return "[Deleted Object]";
  1177. };
  1178. };
  1179. #endif
  1180. return "[" + _get_obj().obj->get_type() + ":" + itos(_get_obj().obj->get_instance_ID()) + "]";
  1181. } else
  1182. return "[Object:null]";
  1183. } break;
  1184. default: {
  1185. return "[" + get_type_name(type) + "]";
  1186. }
  1187. }
  1188. return "";
  1189. }
  1190. Variant::operator Vector2() const {
  1191. if (type == VECTOR2)
  1192. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1193. else if (type == VECTOR3)
  1194. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1195. else
  1196. return Vector2();
  1197. }
  1198. Variant::operator Rect2() const {
  1199. if (type == RECT2)
  1200. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1201. else
  1202. return Rect2();
  1203. }
  1204. Variant::operator Vector3() const {
  1205. if (type == VECTOR3)
  1206. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1207. else
  1208. return Vector3();
  1209. }
  1210. Variant::operator Plane() const {
  1211. if (type == PLANE)
  1212. return *reinterpret_cast<const Plane *>(_data._mem);
  1213. else
  1214. return Plane();
  1215. }
  1216. Variant::operator AABB() const {
  1217. if (type == _AABB)
  1218. return *_data._aabb;
  1219. else
  1220. return AABB();
  1221. }
  1222. Variant::operator Matrix3() const {
  1223. if (type == MATRIX3)
  1224. return *_data._matrix3;
  1225. else if (type == QUAT)
  1226. return *reinterpret_cast<const Quat *>(_data._mem);
  1227. else if (type == TRANSFORM)
  1228. return _data._transform->basis;
  1229. else
  1230. return Matrix3();
  1231. }
  1232. Variant::operator Quat() const {
  1233. if (type == QUAT)
  1234. return *reinterpret_cast<const Quat *>(_data._mem);
  1235. else if (type == MATRIX3)
  1236. return *_data._matrix3;
  1237. else if (type == TRANSFORM)
  1238. return _data._transform->basis;
  1239. else
  1240. return Quat();
  1241. }
  1242. Variant::operator Transform() const {
  1243. if (type == TRANSFORM)
  1244. return *_data._transform;
  1245. else if (type == MATRIX3)
  1246. return Transform(*_data._matrix3, Vector3());
  1247. else if (type == QUAT)
  1248. return Transform(Matrix3(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
  1249. else
  1250. return Transform();
  1251. }
  1252. Variant::operator Matrix32() const {
  1253. if (type == MATRIX32) {
  1254. return *_data._matrix32;
  1255. } else if (type == TRANSFORM) {
  1256. const Transform &t = *_data._transform;
  1257. Matrix32 m;
  1258. m.elements[0][0] = t.basis.elements[0][0];
  1259. m.elements[0][1] = t.basis.elements[1][0];
  1260. m.elements[1][0] = t.basis.elements[0][1];
  1261. m.elements[1][1] = t.basis.elements[1][1];
  1262. m.elements[2][0] = t.origin[0];
  1263. m.elements[2][1] = t.origin[1];
  1264. return m;
  1265. } else
  1266. return Matrix32();
  1267. }
  1268. Variant::operator Color() const {
  1269. if (type == COLOR)
  1270. return *reinterpret_cast<const Color *>(_data._mem);
  1271. else if (type == STRING)
  1272. return Color::html(operator String());
  1273. else if (type == INT)
  1274. return Color::hex(operator int());
  1275. else
  1276. return Color();
  1277. }
  1278. Variant::operator Image() const {
  1279. if (type == IMAGE)
  1280. return *_data._image;
  1281. else
  1282. return Image();
  1283. }
  1284. Variant::operator NodePath() const {
  1285. if (type == NODE_PATH)
  1286. return *reinterpret_cast<const NodePath *>(_data._mem);
  1287. else if (type == STRING)
  1288. return NodePath(operator String());
  1289. else
  1290. return NodePath();
  1291. }
  1292. Variant::operator RefPtr() const {
  1293. if (type == OBJECT)
  1294. return _get_obj().ref;
  1295. else
  1296. return RefPtr();
  1297. }
  1298. Variant::operator RID() const {
  1299. if (type == _RID)
  1300. return *reinterpret_cast<const RID *>(_data._mem);
  1301. else if (type == OBJECT && !_get_obj().ref.is_null()) {
  1302. return _get_obj().ref.get_rid();
  1303. } else if (type == OBJECT && _get_obj().obj) {
  1304. Variant::CallError ce;
  1305. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid, NULL, 0, ce);
  1306. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1307. return ret;
  1308. }
  1309. return RID();
  1310. } else {
  1311. return RID();
  1312. }
  1313. }
  1314. Variant::operator Object *() const {
  1315. if (type == OBJECT)
  1316. return _get_obj().obj;
  1317. else
  1318. return NULL;
  1319. }
  1320. Variant::operator Node *() const {
  1321. if (type == OBJECT)
  1322. return _get_obj().obj ? _get_obj().obj->cast_to<Node>() : NULL;
  1323. else
  1324. return NULL;
  1325. }
  1326. Variant::operator Control *() const {
  1327. if (type == OBJECT)
  1328. return _get_obj().obj ? _get_obj().obj->cast_to<Control>() : NULL;
  1329. else
  1330. return NULL;
  1331. }
  1332. Variant::operator InputEvent() const {
  1333. if (type == INPUT_EVENT)
  1334. return *reinterpret_cast<const InputEvent *>(_data._input_event);
  1335. else
  1336. return InputEvent();
  1337. }
  1338. Variant::operator Dictionary() const {
  1339. if (type == DICTIONARY)
  1340. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1341. else
  1342. return Dictionary();
  1343. }
  1344. template <class DA, class SA>
  1345. inline DA _convert_array(const SA &p_array) {
  1346. DA da;
  1347. da.resize(p_array.size());
  1348. for (int i = 0; i < p_array.size(); i++) {
  1349. da.set(i, Variant(p_array.get(i)));
  1350. }
  1351. return da;
  1352. }
  1353. template <class DA>
  1354. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1355. switch (p_variant.get_type()) {
  1356. case Variant::ARRAY: {
  1357. return _convert_array<DA, Array>(p_variant.operator Array());
  1358. }
  1359. case Variant::RAW_ARRAY: {
  1360. return _convert_array<DA, DVector<uint8_t> >(p_variant.operator DVector<uint8_t>());
  1361. }
  1362. case Variant::INT_ARRAY: {
  1363. return _convert_array<DA, DVector<int> >(p_variant.operator DVector<int>());
  1364. }
  1365. case Variant::REAL_ARRAY: {
  1366. return _convert_array<DA, DVector<real_t> >(p_variant.operator DVector<real_t>());
  1367. }
  1368. case Variant::STRING_ARRAY: {
  1369. return _convert_array<DA, DVector<String> >(p_variant.operator DVector<String>());
  1370. }
  1371. case Variant::VECTOR2_ARRAY: {
  1372. return _convert_array<DA, DVector<Vector2> >(p_variant.operator DVector<Vector2>());
  1373. }
  1374. case Variant::VECTOR3_ARRAY: {
  1375. return _convert_array<DA, DVector<Vector3> >(p_variant.operator DVector<Vector3>());
  1376. }
  1377. case Variant::COLOR_ARRAY: {
  1378. return _convert_array<DA, DVector<Color> >(p_variant.operator DVector<Color>());
  1379. }
  1380. default: { return DA(); }
  1381. }
  1382. return DA();
  1383. }
  1384. Variant::operator Array() const {
  1385. if (type == ARRAY)
  1386. return *reinterpret_cast<const Array *>(_data._mem);
  1387. else
  1388. return _convert_array_from_variant<Array>(*this);
  1389. }
  1390. Variant::operator DVector<uint8_t>() const {
  1391. if (type == RAW_ARRAY)
  1392. return *reinterpret_cast<const DVector<uint8_t> *>(_data._mem);
  1393. else
  1394. return _convert_array_from_variant<DVector<uint8_t> >(*this);
  1395. }
  1396. Variant::operator DVector<int>() const {
  1397. if (type == INT_ARRAY)
  1398. return *reinterpret_cast<const DVector<int> *>(_data._mem);
  1399. else
  1400. return _convert_array_from_variant<DVector<int> >(*this);
  1401. }
  1402. Variant::operator DVector<real_t>() const {
  1403. if (type == REAL_ARRAY)
  1404. return *reinterpret_cast<const DVector<real_t> *>(_data._mem);
  1405. else
  1406. return _convert_array_from_variant<DVector<real_t> >(*this);
  1407. }
  1408. Variant::operator DVector<String>() const {
  1409. if (type == STRING_ARRAY)
  1410. return *reinterpret_cast<const DVector<String> *>(_data._mem);
  1411. else
  1412. return _convert_array_from_variant<DVector<String> >(*this);
  1413. }
  1414. Variant::operator DVector<Vector3>() const {
  1415. if (type == VECTOR3_ARRAY)
  1416. return *reinterpret_cast<const DVector<Vector3> *>(_data._mem);
  1417. else
  1418. return _convert_array_from_variant<DVector<Vector3> >(*this);
  1419. }
  1420. Variant::operator DVector<Vector2>() const {
  1421. if (type == VECTOR2_ARRAY)
  1422. return *reinterpret_cast<const DVector<Vector2> *>(_data._mem);
  1423. else
  1424. return _convert_array_from_variant<DVector<Vector2> >(*this);
  1425. }
  1426. Variant::operator DVector<Color>() const {
  1427. if (type == COLOR_ARRAY)
  1428. return *reinterpret_cast<const DVector<Color> *>(_data._mem);
  1429. else
  1430. return _convert_array_from_variant<DVector<Color> >(*this);
  1431. }
  1432. /* helpers */
  1433. Variant::operator Vector<RID>() const {
  1434. Array va = operator Array();
  1435. Vector<RID> rids;
  1436. rids.resize(va.size());
  1437. for (int i = 0; i < rids.size(); i++)
  1438. rids[i] = va[i];
  1439. return rids;
  1440. }
  1441. Variant::operator Vector<Vector2>() const {
  1442. DVector<Vector2> from = operator DVector<Vector2>();
  1443. Vector<Vector2> to;
  1444. int len = from.size();
  1445. if (len == 0)
  1446. return Vector<Vector2>();
  1447. to.resize(len);
  1448. DVector<Vector2>::Read r = from.read();
  1449. Vector2 *w = &to[0];
  1450. for (int i = 0; i < len; i++) {
  1451. w[i] = r[i];
  1452. }
  1453. return to;
  1454. }
  1455. Variant::operator DVector<Plane>() const {
  1456. Array va = operator Array();
  1457. DVector<Plane> planes;
  1458. int va_size = va.size();
  1459. if (va_size == 0)
  1460. return planes;
  1461. planes.resize(va_size);
  1462. DVector<Plane>::Write w = planes.write();
  1463. for (int i = 0; i < va_size; i++)
  1464. w[i] = va[i];
  1465. return planes;
  1466. }
  1467. Variant::operator DVector<Face3>() const {
  1468. DVector<Vector3> va = operator DVector<Vector3>();
  1469. DVector<Face3> faces;
  1470. int va_size = va.size();
  1471. if (va_size == 0)
  1472. return faces;
  1473. faces.resize(va_size / 3);
  1474. DVector<Face3>::Write w = faces.write();
  1475. DVector<Vector3>::Read r = va.read();
  1476. for (int i = 0; i < va_size; i++)
  1477. w[i / 3].vertex[i % 3] = r[i];
  1478. return faces;
  1479. }
  1480. Variant::operator Vector<Plane>() const {
  1481. Array va = operator Array();
  1482. Vector<Plane> planes;
  1483. int va_size = va.size();
  1484. if (va_size == 0)
  1485. return planes;
  1486. planes.resize(va_size);
  1487. for (int i = 0; i < va_size; i++)
  1488. planes[i] = va[i];
  1489. return planes;
  1490. }
  1491. Variant::operator Vector<Variant>() const {
  1492. Array from = operator Array();
  1493. Vector<Variant> to;
  1494. int len = from.size();
  1495. to.resize(len);
  1496. for (int i = 0; i < len; i++) {
  1497. to[i] = from[i];
  1498. }
  1499. return to;
  1500. }
  1501. Variant::operator Vector<uint8_t>() const {
  1502. DVector<uint8_t> from = operator DVector<uint8_t>();
  1503. Vector<uint8_t> to;
  1504. int len = from.size();
  1505. to.resize(len);
  1506. for (int i = 0; i < len; i++) {
  1507. to[i] = from[i];
  1508. }
  1509. return to;
  1510. }
  1511. Variant::operator Vector<int>() const {
  1512. DVector<int> from = operator DVector<int>();
  1513. Vector<int> to;
  1514. int len = from.size();
  1515. to.resize(len);
  1516. for (int i = 0; i < len; i++) {
  1517. to[i] = from[i];
  1518. }
  1519. return to;
  1520. }
  1521. Variant::operator Vector<real_t>() const {
  1522. DVector<real_t> from = operator DVector<real_t>();
  1523. Vector<real_t> to;
  1524. int len = from.size();
  1525. to.resize(len);
  1526. for (int i = 0; i < len; i++) {
  1527. to[i] = from[i];
  1528. }
  1529. return to;
  1530. }
  1531. Variant::operator Vector<String>() const {
  1532. DVector<String> from = operator DVector<String>();
  1533. Vector<String> to;
  1534. int len = from.size();
  1535. to.resize(len);
  1536. for (int i = 0; i < len; i++) {
  1537. to[i] = from[i];
  1538. }
  1539. return to;
  1540. }
  1541. Variant::operator Vector<Vector3>() const {
  1542. DVector<Vector3> from = operator DVector<Vector3>();
  1543. Vector<Vector3> to;
  1544. int len = from.size();
  1545. if (len == 0)
  1546. return Vector<Vector3>();
  1547. to.resize(len);
  1548. DVector<Vector3>::Read r = from.read();
  1549. Vector3 *w = &to[0];
  1550. for (int i = 0; i < len; i++) {
  1551. w[i] = r[i];
  1552. }
  1553. return to;
  1554. }
  1555. Variant::operator Vector<Color>() const {
  1556. DVector<Color> from = operator DVector<Color>();
  1557. Vector<Color> to;
  1558. int len = from.size();
  1559. if (len == 0)
  1560. return Vector<Color>();
  1561. to.resize(len);
  1562. DVector<Color>::Read r = from.read();
  1563. Color *w = &to[0];
  1564. for (int i = 0; i < len; i++) {
  1565. w[i] = r[i];
  1566. }
  1567. return to;
  1568. }
  1569. Variant::operator Margin() const {
  1570. return (Margin) operator int();
  1571. }
  1572. Variant::operator Orientation() const {
  1573. return (Orientation) operator int();
  1574. }
  1575. Variant::operator IP_Address() const {
  1576. if (type == REAL_ARRAY || type == INT_ARRAY || type == RAW_ARRAY) {
  1577. DVector<int> addr = operator DVector<int>();
  1578. if (addr.size() == 4) {
  1579. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1580. }
  1581. }
  1582. return IP_Address(operator String());
  1583. }
  1584. Variant::Variant(bool p_bool) {
  1585. type = BOOL;
  1586. _data._bool = p_bool;
  1587. }
  1588. /*
  1589. Variant::Variant(long unsigned int p_long) {
  1590. type=INT;
  1591. _data._int=p_long;
  1592. };
  1593. */
  1594. Variant::Variant(signed int p_int) {
  1595. type = INT;
  1596. _data._int = p_int;
  1597. }
  1598. Variant::Variant(unsigned int p_int) {
  1599. type = INT;
  1600. _data._int = p_int;
  1601. }
  1602. #ifdef NEED_LONG_INT
  1603. Variant::Variant(signed long p_int) {
  1604. type = INT;
  1605. _data._int = p_int;
  1606. }
  1607. Variant::Variant(unsigned long p_int) {
  1608. type = INT;
  1609. _data._int = p_int;
  1610. }
  1611. #endif
  1612. Variant::Variant(int64_t p_int) {
  1613. type = INT;
  1614. _data._int = p_int;
  1615. }
  1616. Variant::Variant(uint64_t p_int) {
  1617. type = INT;
  1618. _data._int = p_int;
  1619. }
  1620. Variant::Variant(signed short p_short) {
  1621. type = INT;
  1622. _data._int = p_short;
  1623. }
  1624. Variant::Variant(unsigned short p_short) {
  1625. type = INT;
  1626. _data._int = p_short;
  1627. }
  1628. Variant::Variant(signed char p_char) {
  1629. type = INT;
  1630. _data._int = p_char;
  1631. }
  1632. Variant::Variant(unsigned char p_char) {
  1633. type = INT;
  1634. _data._int = p_char;
  1635. }
  1636. Variant::Variant(float p_float) {
  1637. type = REAL;
  1638. _data._real = p_float;
  1639. }
  1640. Variant::Variant(double p_double) {
  1641. type = REAL;
  1642. _data._real = p_double;
  1643. }
  1644. Variant::Variant(const StringName &p_string) {
  1645. type = STRING;
  1646. memnew_placement(_data._mem, String(p_string.operator String()));
  1647. }
  1648. Variant::Variant(const String &p_string) {
  1649. type = STRING;
  1650. memnew_placement(_data._mem, String(p_string));
  1651. }
  1652. Variant::Variant(const char *const p_cstring) {
  1653. type = STRING;
  1654. memnew_placement(_data._mem, String((const char *)p_cstring));
  1655. }
  1656. Variant::Variant(const CharType *p_wstring) {
  1657. type = STRING;
  1658. memnew_placement(_data._mem, String(p_wstring));
  1659. }
  1660. Variant::Variant(const Vector3 &p_vector3) {
  1661. type = VECTOR3;
  1662. memnew_placement(_data._mem, Vector3(p_vector3));
  1663. }
  1664. Variant::Variant(const Vector2 &p_vector2) {
  1665. type = VECTOR2;
  1666. memnew_placement(_data._mem, Vector2(p_vector2));
  1667. }
  1668. Variant::Variant(const Rect2 &p_rect2) {
  1669. type = RECT2;
  1670. memnew_placement(_data._mem, Rect2(p_rect2));
  1671. }
  1672. Variant::Variant(const Plane &p_plane) {
  1673. type = PLANE;
  1674. memnew_placement(_data._mem, Plane(p_plane));
  1675. }
  1676. Variant::Variant(const AABB &p_aabb) {
  1677. type = _AABB;
  1678. _data._aabb = memnew(AABB(p_aabb));
  1679. }
  1680. Variant::Variant(const Matrix3 &p_matrix) {
  1681. type = MATRIX3;
  1682. _data._matrix3 = memnew(Matrix3(p_matrix));
  1683. }
  1684. Variant::Variant(const Quat &p_quat) {
  1685. type = QUAT;
  1686. memnew_placement(_data._mem, Quat(p_quat));
  1687. }
  1688. Variant::Variant(const Transform &p_transform) {
  1689. type = TRANSFORM;
  1690. _data._transform = memnew(Transform(p_transform));
  1691. }
  1692. Variant::Variant(const Matrix32 &p_transform) {
  1693. type = MATRIX32;
  1694. _data._matrix32 = memnew(Matrix32(p_transform));
  1695. }
  1696. Variant::Variant(const Color &p_color) {
  1697. type = COLOR;
  1698. memnew_placement(_data._mem, Color(p_color));
  1699. }
  1700. Variant::Variant(const Image &p_image) {
  1701. type = IMAGE;
  1702. _data._image = memnew(Image(p_image));
  1703. }
  1704. Variant::Variant(const NodePath &p_node_path) {
  1705. type = NODE_PATH;
  1706. memnew_placement(_data._mem, NodePath(p_node_path));
  1707. }
  1708. Variant::Variant(const InputEvent &p_input_event) {
  1709. type = INPUT_EVENT;
  1710. _data._input_event = memnew(InputEvent(p_input_event));
  1711. }
  1712. Variant::Variant(const RefPtr &p_resource) {
  1713. type = OBJECT;
  1714. memnew_placement(_data._mem, ObjData);
  1715. REF ref = p_resource;
  1716. _get_obj().obj = ref.ptr();
  1717. _get_obj().ref = p_resource;
  1718. }
  1719. Variant::Variant(const RID &p_rid) {
  1720. type = _RID;
  1721. memnew_placement(_data._mem, RID(p_rid));
  1722. }
  1723. Variant::Variant(const Object *p_object) {
  1724. type = OBJECT;
  1725. memnew_placement(_data._mem, ObjData);
  1726. _get_obj().obj = const_cast<Object *>(p_object);
  1727. }
  1728. Variant::Variant(const Dictionary &p_dictionary) {
  1729. type = DICTIONARY;
  1730. memnew_placement(_data._mem, (Dictionary)(p_dictionary));
  1731. }
  1732. Variant::Variant(const Array &p_array) {
  1733. type = ARRAY;
  1734. memnew_placement(_data._mem, Array(p_array));
  1735. }
  1736. Variant::Variant(const DVector<Plane> &p_array) {
  1737. type = ARRAY;
  1738. Array *plane_array = memnew_placement(_data._mem, Array);
  1739. plane_array->resize(p_array.size());
  1740. for (int i = 0; i < p_array.size(); i++) {
  1741. plane_array->operator[](i) = Variant(p_array[i]);
  1742. }
  1743. }
  1744. Variant::Variant(const Vector<Plane> &p_array) {
  1745. type = ARRAY;
  1746. Array *plane_array = memnew_placement(_data._mem, Array);
  1747. plane_array->resize(p_array.size());
  1748. for (int i = 0; i < p_array.size(); i++) {
  1749. plane_array->operator[](i) = Variant(p_array[i]);
  1750. }
  1751. }
  1752. Variant::Variant(const Vector<RID> &p_array) {
  1753. type = ARRAY;
  1754. Array *rid_array = memnew_placement(_data._mem, Array);
  1755. rid_array->resize(p_array.size());
  1756. for (int i = 0; i < p_array.size(); i++) {
  1757. rid_array->set(i, Variant(p_array[i]));
  1758. }
  1759. }
  1760. Variant::Variant(const Vector<Vector2> &p_array) {
  1761. type = NIL;
  1762. DVector<Vector2> v;
  1763. int len = p_array.size();
  1764. if (len > 0) {
  1765. v.resize(len);
  1766. DVector<Vector2>::Write w = v.write();
  1767. const Vector2 *r = p_array.ptr();
  1768. for (int i = 0; i < len; i++)
  1769. w[i] = r[i];
  1770. }
  1771. *this = v;
  1772. }
  1773. Variant::Variant(const DVector<uint8_t> &p_raw_array) {
  1774. type = RAW_ARRAY;
  1775. memnew_placement(_data._mem, DVector<uint8_t>(p_raw_array));
  1776. }
  1777. Variant::Variant(const DVector<int> &p_int_array) {
  1778. type = INT_ARRAY;
  1779. memnew_placement(_data._mem, DVector<int>(p_int_array));
  1780. }
  1781. Variant::Variant(const DVector<real_t> &p_real_array) {
  1782. type = REAL_ARRAY;
  1783. memnew_placement(_data._mem, DVector<real_t>(p_real_array));
  1784. }
  1785. Variant::Variant(const DVector<String> &p_string_array) {
  1786. type = STRING_ARRAY;
  1787. memnew_placement(_data._mem, DVector<String>(p_string_array));
  1788. }
  1789. Variant::Variant(const DVector<Vector3> &p_vector3_array) {
  1790. type = VECTOR3_ARRAY;
  1791. memnew_placement(_data._mem, DVector<Vector3>(p_vector3_array));
  1792. }
  1793. Variant::Variant(const DVector<Vector2> &p_vector2_array) {
  1794. type = VECTOR2_ARRAY;
  1795. memnew_placement(_data._mem, DVector<Vector2>(p_vector2_array));
  1796. }
  1797. Variant::Variant(const DVector<Color> &p_color_array) {
  1798. type = COLOR_ARRAY;
  1799. memnew_placement(_data._mem, DVector<Color>(p_color_array));
  1800. }
  1801. Variant::Variant(const DVector<Face3> &p_face_array) {
  1802. DVector<Vector3> vertices;
  1803. int face_count = p_face_array.size();
  1804. vertices.resize(face_count * 3);
  1805. if (face_count) {
  1806. DVector<Face3>::Read r = p_face_array.read();
  1807. DVector<Vector3>::Write w = vertices.write();
  1808. for (int i = 0; i < face_count; i++) {
  1809. for (int j = 0; j < 3; j++)
  1810. w[i * 3 + j] = r[i].vertex[j];
  1811. }
  1812. r = DVector<Face3>::Read();
  1813. w = DVector<Vector3>::Write();
  1814. }
  1815. type = NIL;
  1816. *this = vertices;
  1817. }
  1818. /* helpers */
  1819. Variant::Variant(const Vector<Variant> &p_array) {
  1820. type = NIL;
  1821. Array v;
  1822. int len = p_array.size();
  1823. v.resize(len);
  1824. for (int i = 0; i < len; i++)
  1825. v.set(i, p_array[i]);
  1826. *this = v;
  1827. }
  1828. Variant::Variant(const Vector<uint8_t> &p_array) {
  1829. type = NIL;
  1830. DVector<uint8_t> v;
  1831. int len = p_array.size();
  1832. v.resize(len);
  1833. for (int i = 0; i < len; i++)
  1834. v.set(i, p_array[i]);
  1835. *this = v;
  1836. }
  1837. Variant::Variant(const Vector<int> &p_array) {
  1838. type = NIL;
  1839. DVector<int> v;
  1840. int len = p_array.size();
  1841. v.resize(len);
  1842. for (int i = 0; i < len; i++)
  1843. v.set(i, p_array[i]);
  1844. *this = v;
  1845. }
  1846. Variant::Variant(const Vector<real_t> &p_array) {
  1847. type = NIL;
  1848. DVector<real_t> v;
  1849. int len = p_array.size();
  1850. v.resize(len);
  1851. for (int i = 0; i < len; i++)
  1852. v.set(i, p_array[i]);
  1853. *this = v;
  1854. }
  1855. Variant::Variant(const Vector<String> &p_array) {
  1856. type = NIL;
  1857. DVector<String> v;
  1858. int len = p_array.size();
  1859. v.resize(len);
  1860. for (int i = 0; i < len; i++)
  1861. v.set(i, p_array[i]);
  1862. *this = v;
  1863. }
  1864. Variant::Variant(const Vector<Vector3> &p_array) {
  1865. type = NIL;
  1866. DVector<Vector3> v;
  1867. int len = p_array.size();
  1868. if (len > 0) {
  1869. v.resize(len);
  1870. DVector<Vector3>::Write w = v.write();
  1871. const Vector3 *r = p_array.ptr();
  1872. for (int i = 0; i < len; i++)
  1873. w[i] = r[i];
  1874. }
  1875. *this = v;
  1876. }
  1877. Variant::Variant(const Vector<Color> &p_array) {
  1878. type = NIL;
  1879. DVector<Color> v;
  1880. int len = p_array.size();
  1881. v.resize(len);
  1882. for (int i = 0; i < len; i++)
  1883. v.set(i, p_array[i]);
  1884. *this = v;
  1885. }
  1886. void Variant::operator=(const Variant &p_variant) {
  1887. reference(p_variant);
  1888. }
  1889. Variant::Variant(const IP_Address &p_address) {
  1890. type = STRING;
  1891. memnew_placement(_data._mem, String(p_address));
  1892. }
  1893. Variant::Variant(const Variant &p_variant) {
  1894. type = NIL;
  1895. reference(p_variant);
  1896. }
  1897. /*
  1898. Variant::~Variant() {
  1899. clear();
  1900. }*/
  1901. uint32_t Variant::hash() const {
  1902. switch (type) {
  1903. case NIL: {
  1904. return 0;
  1905. } break;
  1906. case BOOL: {
  1907. return _data._bool ? 1 : 0;
  1908. } break;
  1909. case INT: {
  1910. return _data._int;
  1911. } break;
  1912. case REAL: {
  1913. return hash_djb2_one_float(_data._real);
  1914. } break;
  1915. case STRING: {
  1916. return reinterpret_cast<const String *>(_data._mem)->hash();
  1917. } break;
  1918. // math types
  1919. case VECTOR2: {
  1920. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  1921. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  1922. } break;
  1923. case RECT2: {
  1924. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->pos.x);
  1925. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->pos.y, hash);
  1926. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  1927. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  1928. } break;
  1929. case MATRIX32: {
  1930. uint32_t hash = 5831;
  1931. for (int i = 0; i < 3; i++) {
  1932. for (int j = 0; j < 2; j++) {
  1933. hash = hash_djb2_one_float(_data._matrix32->elements[i][j], hash);
  1934. }
  1935. }
  1936. return hash;
  1937. } break;
  1938. case VECTOR3: {
  1939. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  1940. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  1941. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  1942. } break;
  1943. case PLANE: {
  1944. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  1945. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  1946. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  1947. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  1948. } break;
  1949. /*
  1950. case QUAT: {
  1951. } break;*/
  1952. case _AABB: {
  1953. uint32_t hash = 5831;
  1954. for (int i = 0; i < 3; i++) {
  1955. hash = hash_djb2_one_float(_data._aabb->pos[i], hash);
  1956. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  1957. }
  1958. return hash;
  1959. } break;
  1960. case QUAT: {
  1961. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  1962. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  1963. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  1964. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  1965. } break;
  1966. case MATRIX3: {
  1967. uint32_t hash = 5831;
  1968. for (int i = 0; i < 3; i++) {
  1969. for (int j = 0; j < 3; j++) {
  1970. hash = hash_djb2_one_float(_data._matrix3->elements[i][j], hash);
  1971. }
  1972. }
  1973. return hash;
  1974. } break;
  1975. case TRANSFORM: {
  1976. uint32_t hash = 5831;
  1977. for (int i = 0; i < 3; i++) {
  1978. for (int j = 0; j < 3; j++) {
  1979. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  1980. }
  1981. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  1982. }
  1983. return hash;
  1984. } break;
  1985. // misc types
  1986. case COLOR: {
  1987. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  1988. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  1989. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  1990. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  1991. } break;
  1992. case IMAGE: {
  1993. return 0;
  1994. } break;
  1995. case _RID: {
  1996. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  1997. } break;
  1998. case OBJECT: {
  1999. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  2000. } break;
  2001. case NODE_PATH: {
  2002. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2003. } break;
  2004. case INPUT_EVENT: {
  2005. return hash_djb2_buffer((uint8_t *)_data._input_event, sizeof(InputEvent));
  2006. } break;
  2007. case DICTIONARY: {
  2008. return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
  2009. } break;
  2010. case ARRAY: {
  2011. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2012. return arr.hash();
  2013. } break;
  2014. case RAW_ARRAY: {
  2015. const DVector<uint8_t> &arr = *reinterpret_cast<const DVector<uint8_t> *>(_data._mem);
  2016. int len = arr.size();
  2017. DVector<uint8_t>::Read r = arr.read();
  2018. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2019. } break;
  2020. case INT_ARRAY: {
  2021. const DVector<int> &arr = *reinterpret_cast<const DVector<int> *>(_data._mem);
  2022. int len = arr.size();
  2023. DVector<int>::Read r = arr.read();
  2024. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2025. } break;
  2026. case REAL_ARRAY: {
  2027. const DVector<real_t> &arr = *reinterpret_cast<const DVector<real_t> *>(_data._mem);
  2028. int len = arr.size();
  2029. DVector<real_t>::Read r = arr.read();
  2030. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2031. } break;
  2032. case STRING_ARRAY: {
  2033. uint32_t hash = 5831;
  2034. const DVector<String> &arr = *reinterpret_cast<const DVector<String> *>(_data._mem);
  2035. int len = arr.size();
  2036. DVector<String>::Read r = arr.read();
  2037. for (int i = 0; i < len; i++) {
  2038. hash = hash_djb2_one_32(r[i].hash(), hash);
  2039. }
  2040. return hash;
  2041. } break;
  2042. case VECTOR2_ARRAY: {
  2043. uint32_t hash = 5831;
  2044. const DVector<Vector2> &arr = *reinterpret_cast<const DVector<Vector2> *>(_data._mem);
  2045. int len = arr.size();
  2046. DVector<Vector2>::Read r = arr.read();
  2047. for (int i = 0; i < len; i++) {
  2048. hash = hash_djb2_one_float(r[i].x, hash);
  2049. hash = hash_djb2_one_float(r[i].y, hash);
  2050. }
  2051. return hash;
  2052. } break;
  2053. case VECTOR3_ARRAY: {
  2054. uint32_t hash = 5831;
  2055. const DVector<Vector3> &arr = *reinterpret_cast<const DVector<Vector3> *>(_data._mem);
  2056. int len = arr.size();
  2057. DVector<Vector3>::Read r = arr.read();
  2058. for (int i = 0; i < len; i++) {
  2059. hash = hash_djb2_one_float(r[i].x, hash);
  2060. hash = hash_djb2_one_float(r[i].y, hash);
  2061. hash = hash_djb2_one_float(r[i].z, hash);
  2062. }
  2063. return hash;
  2064. } break;
  2065. case COLOR_ARRAY: {
  2066. uint32_t hash = 5831;
  2067. const DVector<Color> &arr = *reinterpret_cast<const DVector<Color> *>(_data._mem);
  2068. int len = arr.size();
  2069. DVector<Color>::Read r = arr.read();
  2070. for (int i = 0; i < len; i++) {
  2071. hash = hash_djb2_one_float(r[i].r, hash);
  2072. hash = hash_djb2_one_float(r[i].g, hash);
  2073. hash = hash_djb2_one_float(r[i].b, hash);
  2074. hash = hash_djb2_one_float(r[i].a, hash);
  2075. }
  2076. return hash;
  2077. } break;
  2078. default: {}
  2079. }
  2080. return 0;
  2081. }
  2082. #define hash_compare_scalar(p_lhs, p_rhs) \
  2083. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
  2084. #define hash_compare_vector2(p_lhs, p_rhs) \
  2085. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2086. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2087. #define hash_compare_vector3(p_lhs, p_rhs) \
  2088. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2089. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2090. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2091. #define hash_compare_quat(p_lhs, p_rhs) \
  2092. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2093. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2094. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2095. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2096. #define hash_compare_color(p_lhs, p_rhs) \
  2097. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2098. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2099. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2100. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2101. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2102. const DVector<p_type> &l = *reinterpret_cast<const DVector<p_type> *>(p_lhs); \
  2103. const DVector<p_type> &r = *reinterpret_cast<const DVector<p_type> *>(p_rhs); \
  2104. \
  2105. if (l.size() != r.size()) \
  2106. return false; \
  2107. \
  2108. DVector<p_type>::Read lr = l.read(); \
  2109. DVector<p_type>::Read rr = r.read(); \
  2110. \
  2111. for (int i = 0; i < l.size(); ++i) { \
  2112. if (!p_compare_func((lr[0]), (rr[0]))) \
  2113. return false; \
  2114. } \
  2115. \
  2116. return true
  2117. bool Variant::hash_compare(const Variant &p_variant) const {
  2118. if (type != p_variant.type)
  2119. return false;
  2120. switch (type) {
  2121. case REAL: {
  2122. return hash_compare_scalar(_data._real, p_variant._data._real);
  2123. } break;
  2124. case VECTOR2: {
  2125. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2126. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2127. return hash_compare_vector2(*l, *r);
  2128. } break;
  2129. case RECT2: {
  2130. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2131. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2132. return (hash_compare_vector2(l->pos, r->pos)) &&
  2133. (hash_compare_vector2(l->size, r->size));
  2134. } break;
  2135. case MATRIX32: {
  2136. Matrix32 *l = _data._matrix32;
  2137. Matrix32 *r = p_variant._data._matrix32;
  2138. for (int i = 0; i < 3; i++) {
  2139. if (!(hash_compare_vector2(l->elements[i], r->elements[i])))
  2140. return false;
  2141. }
  2142. return true;
  2143. } break;
  2144. case VECTOR3: {
  2145. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2146. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2147. return hash_compare_vector3(*l, *r);
  2148. } break;
  2149. case PLANE: {
  2150. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2151. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2152. return (hash_compare_vector3(l->normal, r->normal)) &&
  2153. (hash_compare_scalar(l->d, r->d));
  2154. } break;
  2155. case _AABB: {
  2156. const AABB *l = _data._aabb;
  2157. const AABB *r = p_variant._data._aabb;
  2158. return (hash_compare_vector3(l->pos, r->pos) &&
  2159. (hash_compare_vector3(l->size, r->size)));
  2160. } break;
  2161. case QUAT: {
  2162. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2163. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2164. return hash_compare_quat(*l, *r);
  2165. } break;
  2166. case MATRIX3: {
  2167. const Matrix3 *l = _data._matrix3;
  2168. const Matrix3 *r = p_variant._data._matrix3;
  2169. for (int i = 0; i < 3; i++) {
  2170. if (!(hash_compare_vector3(l->elements[i], r->elements[i])))
  2171. return false;
  2172. }
  2173. return true;
  2174. } break;
  2175. case TRANSFORM: {
  2176. const Transform *l = _data._transform;
  2177. const Transform *r = p_variant._data._transform;
  2178. for (int i = 0; i < 3; i++) {
  2179. if (!(hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])))
  2180. return false;
  2181. }
  2182. return hash_compare_vector3(l->origin, r->origin);
  2183. } break;
  2184. case COLOR: {
  2185. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2186. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2187. return hash_compare_color(*l, *r);
  2188. } break;
  2189. case ARRAY: {
  2190. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2191. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2192. if (l.size() != r.size())
  2193. return false;
  2194. for (int i = 0; i < l.size(); ++i) {
  2195. if (!l[0].hash_compare(r[0]))
  2196. return false;
  2197. }
  2198. return true;
  2199. } break;
  2200. case REAL_ARRAY: {
  2201. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2202. } break;
  2203. case VECTOR2_ARRAY: {
  2204. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2205. } break;
  2206. case VECTOR3_ARRAY: {
  2207. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2208. } break;
  2209. case COLOR_ARRAY: {
  2210. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2211. } break;
  2212. default:
  2213. bool v;
  2214. Variant r;
  2215. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2216. return r;
  2217. }
  2218. return false;
  2219. }
  2220. bool Variant::is_ref() const {
  2221. return type == OBJECT && !_get_obj().ref.is_null();
  2222. }
  2223. Vector<Variant> varray() {
  2224. return Vector<Variant>();
  2225. }
  2226. Vector<Variant> varray(const Variant &p_arg1) {
  2227. Vector<Variant> v;
  2228. v.push_back(p_arg1);
  2229. return v;
  2230. }
  2231. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2232. Vector<Variant> v;
  2233. v.push_back(p_arg1);
  2234. v.push_back(p_arg2);
  2235. return v;
  2236. }
  2237. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2238. Vector<Variant> v;
  2239. v.push_back(p_arg1);
  2240. v.push_back(p_arg2);
  2241. v.push_back(p_arg3);
  2242. return v;
  2243. }
  2244. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2245. Vector<Variant> v;
  2246. v.push_back(p_arg1);
  2247. v.push_back(p_arg2);
  2248. v.push_back(p_arg3);
  2249. v.push_back(p_arg4);
  2250. return v;
  2251. }
  2252. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2253. Vector<Variant> v;
  2254. v.push_back(p_arg1);
  2255. v.push_back(p_arg2);
  2256. v.push_back(p_arg3);
  2257. v.push_back(p_arg4);
  2258. v.push_back(p_arg5);
  2259. return v;
  2260. }
  2261. void Variant::static_assign(const Variant &p_variant) {
  2262. }
  2263. bool Variant::is_shared() const {
  2264. switch (type) {
  2265. case OBJECT: return true;
  2266. case ARRAY: return reinterpret_cast<const Array *>(_data._mem)->is_shared();
  2267. case DICTIONARY: return reinterpret_cast<const Dictionary *>(_data._mem)->is_shared();
  2268. default: {}
  2269. }
  2270. return false;
  2271. }
  2272. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2273. VARIANT_ARGPTRS;
  2274. int argc = 0;
  2275. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2276. if (argptr[i]->get_type() == Variant::NIL)
  2277. break;
  2278. argc++;
  2279. }
  2280. CallError error;
  2281. Variant ret = call(p_method, argptr, argc, error);
  2282. switch (error.error) {
  2283. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2284. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2285. ERR_PRINT(err.utf8().get_data());
  2286. } break;
  2287. case CallError::CALL_ERROR_INVALID_METHOD: {
  2288. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2289. ERR_PRINT(err.utf8().get_data());
  2290. } break;
  2291. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2292. String err = "Too many arguments for method '" + p_method + "'";
  2293. ERR_PRINT(err.utf8().get_data());
  2294. } break;
  2295. default: {}
  2296. }
  2297. return ret;
  2298. }
  2299. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2300. r_value = Variant();
  2301. }
  2302. String Variant::get_construct_string() const {
  2303. String vars;
  2304. VariantWriter::write_to_string(*this, vars);
  2305. return vars;
  2306. }
  2307. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2308. String err_text;
  2309. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2310. int errorarg = ce.argument;
  2311. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(ce.expected) + ".";
  2312. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2313. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2314. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2315. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2316. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2317. err_text = "Method not found.";
  2318. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2319. err_text = "Instance is null";
  2320. } else if (ce.error == Variant::CallError::CALL_OK) {
  2321. return "Call OK";
  2322. }
  2323. String class_name = p_base->get_type();
  2324. Ref<Script> script = p_base->get_script();
  2325. if (script.is_valid() && script->get_path().is_resource_file()) {
  2326. class_name += "(" + script->get_path().get_file() + ")";
  2327. }
  2328. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2329. }
  2330. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2331. Array args;
  2332. if (p1.get_type() != Variant::NIL) {
  2333. args.push_back(p1);
  2334. if (p2.get_type() != Variant::NIL) {
  2335. args.push_back(p2);
  2336. if (p3.get_type() != Variant::NIL) {
  2337. args.push_back(p3);
  2338. if (p4.get_type() != Variant::NIL) {
  2339. args.push_back(p4);
  2340. if (p5.get_type() != Variant::NIL) {
  2341. args.push_back(p5);
  2342. }
  2343. }
  2344. }
  2345. }
  2346. }
  2347. bool error = false;
  2348. String fmt = p_text.sprintf(args, &error);
  2349. ERR_FAIL_COND_V(error, String());
  2350. return fmt;
  2351. }