variant.cpp 67 KB

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