variant.cpp 64 KB

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