variant.cpp 64 KB

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