variant.cpp 67 KB

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