gdscript_functions.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. /*************************************************************************/
  2. /* gdscript_functions.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_functions.h"
  31. #include "core/class_db.h"
  32. #include "core/func_ref.h"
  33. #include "core/io/json.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/math/math_funcs.h"
  36. #include "core/os/os.h"
  37. #include "core/reference.h"
  38. #include "core/variant_parser.h"
  39. #include "gdscript.h"
  40. const char *GDScriptFunctions::get_func_name(Function p_func) {
  41. ERR_FAIL_INDEX_V(p_func, FUNC_MAX, "");
  42. static const char *_names[FUNC_MAX] = {
  43. "sin",
  44. "cos",
  45. "tan",
  46. "sinh",
  47. "cosh",
  48. "tanh",
  49. "asin",
  50. "acos",
  51. "atan",
  52. "atan2",
  53. "sqrt",
  54. "fmod",
  55. "fposmod",
  56. "floor",
  57. "ceil",
  58. "round",
  59. "abs",
  60. "sign",
  61. "pow",
  62. "log",
  63. "exp",
  64. "is_nan",
  65. "is_inf",
  66. "ease",
  67. "decimals",
  68. "stepify",
  69. "lerp",
  70. "inverse_lerp",
  71. "range_lerp",
  72. "smoothstep",
  73. "dectime",
  74. "randomize",
  75. "randi",
  76. "randf",
  77. "rand_range",
  78. "seed",
  79. "rand_seed",
  80. "deg2rad",
  81. "rad2deg",
  82. "linear2db",
  83. "db2linear",
  84. "polar2cartesian",
  85. "cartesian2polar",
  86. "wrapi",
  87. "wrapf",
  88. "max",
  89. "min",
  90. "clamp",
  91. "nearest_po2",
  92. "weakref",
  93. "funcref",
  94. "convert",
  95. "typeof",
  96. "type_exists",
  97. "char",
  98. "str",
  99. "print",
  100. "printt",
  101. "prints",
  102. "printerr",
  103. "printraw",
  104. "print_debug",
  105. "push_error",
  106. "push_warning",
  107. "var2str",
  108. "str2var",
  109. "var2bytes",
  110. "bytes2var",
  111. "range",
  112. "load",
  113. "inst2dict",
  114. "dict2inst",
  115. "validate_json",
  116. "parse_json",
  117. "to_json",
  118. "hash",
  119. "Color8",
  120. "ColorN",
  121. "print_stack",
  122. "get_stack",
  123. "instance_from_id",
  124. "len",
  125. "is_instance_valid",
  126. };
  127. return _names[p_func];
  128. }
  129. void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_count, Variant &r_ret, Variant::CallError &r_error) {
  130. r_error.error = Variant::CallError::CALL_OK;
  131. #ifdef DEBUG_ENABLED
  132. #define VALIDATE_ARG_COUNT(m_count) \
  133. if (p_arg_count < m_count) { \
  134. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \
  135. r_error.argument = m_count; \
  136. r_ret = Variant(); \
  137. return; \
  138. } \
  139. if (p_arg_count > m_count) { \
  140. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \
  141. r_error.argument = m_count; \
  142. r_ret = Variant(); \
  143. return; \
  144. }
  145. #define VALIDATE_ARG_NUM(m_arg) \
  146. if (!p_args[m_arg]->is_num()) { \
  147. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  148. r_error.argument = m_arg; \
  149. r_error.expected = Variant::REAL; \
  150. r_ret = Variant(); \
  151. return; \
  152. }
  153. #else
  154. #define VALIDATE_ARG_COUNT(m_count)
  155. #define VALIDATE_ARG_NUM(m_arg)
  156. #endif
  157. //using a switch, so the compiler generates a jumptable
  158. switch (p_func) {
  159. case MATH_SIN: {
  160. VALIDATE_ARG_COUNT(1);
  161. VALIDATE_ARG_NUM(0);
  162. r_ret = Math::sin((double)*p_args[0]);
  163. } break;
  164. case MATH_COS: {
  165. VALIDATE_ARG_COUNT(1);
  166. VALIDATE_ARG_NUM(0);
  167. r_ret = Math::cos((double)*p_args[0]);
  168. } break;
  169. case MATH_TAN: {
  170. VALIDATE_ARG_COUNT(1);
  171. VALIDATE_ARG_NUM(0);
  172. r_ret = Math::tan((double)*p_args[0]);
  173. } break;
  174. case MATH_SINH: {
  175. VALIDATE_ARG_COUNT(1);
  176. VALIDATE_ARG_NUM(0);
  177. r_ret = Math::sinh((double)*p_args[0]);
  178. } break;
  179. case MATH_COSH: {
  180. VALIDATE_ARG_COUNT(1);
  181. VALIDATE_ARG_NUM(0);
  182. r_ret = Math::cosh((double)*p_args[0]);
  183. } break;
  184. case MATH_TANH: {
  185. VALIDATE_ARG_COUNT(1);
  186. VALIDATE_ARG_NUM(0);
  187. r_ret = Math::tanh((double)*p_args[0]);
  188. } break;
  189. case MATH_ASIN: {
  190. VALIDATE_ARG_COUNT(1);
  191. VALIDATE_ARG_NUM(0);
  192. r_ret = Math::asin((double)*p_args[0]);
  193. } break;
  194. case MATH_ACOS: {
  195. VALIDATE_ARG_COUNT(1);
  196. VALIDATE_ARG_NUM(0);
  197. r_ret = Math::acos((double)*p_args[0]);
  198. } break;
  199. case MATH_ATAN: {
  200. VALIDATE_ARG_COUNT(1);
  201. VALIDATE_ARG_NUM(0);
  202. r_ret = Math::atan((double)*p_args[0]);
  203. } break;
  204. case MATH_ATAN2: {
  205. VALIDATE_ARG_COUNT(2);
  206. VALIDATE_ARG_NUM(0);
  207. VALIDATE_ARG_NUM(1);
  208. r_ret = Math::atan2((double)*p_args[0], (double)*p_args[1]);
  209. } break;
  210. case MATH_SQRT: {
  211. VALIDATE_ARG_COUNT(1);
  212. VALIDATE_ARG_NUM(0);
  213. r_ret = Math::sqrt((double)*p_args[0]);
  214. } break;
  215. case MATH_FMOD: {
  216. VALIDATE_ARG_COUNT(2);
  217. VALIDATE_ARG_NUM(0);
  218. VALIDATE_ARG_NUM(1);
  219. r_ret = Math::fmod((double)*p_args[0], (double)*p_args[1]);
  220. } break;
  221. case MATH_FPOSMOD: {
  222. VALIDATE_ARG_COUNT(2);
  223. VALIDATE_ARG_NUM(0);
  224. VALIDATE_ARG_NUM(1);
  225. r_ret = Math::fposmod((double)*p_args[0], (double)*p_args[1]);
  226. } break;
  227. case MATH_FLOOR: {
  228. VALIDATE_ARG_COUNT(1);
  229. VALIDATE_ARG_NUM(0);
  230. r_ret = Math::floor((double)*p_args[0]);
  231. } break;
  232. case MATH_CEIL: {
  233. VALIDATE_ARG_COUNT(1);
  234. VALIDATE_ARG_NUM(0);
  235. r_ret = Math::ceil((double)*p_args[0]);
  236. } break;
  237. case MATH_ROUND: {
  238. VALIDATE_ARG_COUNT(1);
  239. VALIDATE_ARG_NUM(0);
  240. r_ret = Math::round((double)*p_args[0]);
  241. } break;
  242. case MATH_ABS: {
  243. VALIDATE_ARG_COUNT(1);
  244. if (p_args[0]->get_type() == Variant::INT) {
  245. int64_t i = *p_args[0];
  246. r_ret = ABS(i);
  247. } else if (p_args[0]->get_type() == Variant::REAL) {
  248. double r = *p_args[0];
  249. r_ret = Math::abs(r);
  250. } else {
  251. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  252. r_error.argument = 0;
  253. r_error.expected = Variant::REAL;
  254. r_ret = Variant();
  255. }
  256. } break;
  257. case MATH_SIGN: {
  258. VALIDATE_ARG_COUNT(1);
  259. if (p_args[0]->get_type() == Variant::INT) {
  260. int64_t i = *p_args[0];
  261. r_ret = i < 0 ? -1 : (i > 0 ? +1 : 0);
  262. } else if (p_args[0]->get_type() == Variant::REAL) {
  263. real_t r = *p_args[0];
  264. r_ret = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0);
  265. } else {
  266. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  267. r_error.argument = 0;
  268. r_error.expected = Variant::REAL;
  269. r_ret = Variant();
  270. }
  271. } break;
  272. case MATH_POW: {
  273. VALIDATE_ARG_COUNT(2);
  274. VALIDATE_ARG_NUM(0);
  275. VALIDATE_ARG_NUM(1);
  276. r_ret = Math::pow((double)*p_args[0], (double)*p_args[1]);
  277. } break;
  278. case MATH_LOG: {
  279. VALIDATE_ARG_COUNT(1);
  280. VALIDATE_ARG_NUM(0);
  281. r_ret = Math::log((double)*p_args[0]);
  282. } break;
  283. case MATH_EXP: {
  284. VALIDATE_ARG_COUNT(1);
  285. VALIDATE_ARG_NUM(0);
  286. r_ret = Math::exp((double)*p_args[0]);
  287. } break;
  288. case MATH_ISNAN: {
  289. VALIDATE_ARG_COUNT(1);
  290. VALIDATE_ARG_NUM(0);
  291. r_ret = Math::is_nan((double)*p_args[0]);
  292. } break;
  293. case MATH_ISINF: {
  294. VALIDATE_ARG_COUNT(1);
  295. VALIDATE_ARG_NUM(0);
  296. r_ret = Math::is_inf((double)*p_args[0]);
  297. } break;
  298. case MATH_EASE: {
  299. VALIDATE_ARG_COUNT(2);
  300. VALIDATE_ARG_NUM(0);
  301. VALIDATE_ARG_NUM(1);
  302. r_ret = Math::ease((double)*p_args[0], (double)*p_args[1]);
  303. } break;
  304. case MATH_DECIMALS: {
  305. VALIDATE_ARG_COUNT(1);
  306. VALIDATE_ARG_NUM(0);
  307. r_ret = Math::step_decimals((double)*p_args[0]);
  308. } break;
  309. case MATH_STEPIFY: {
  310. VALIDATE_ARG_COUNT(2);
  311. VALIDATE_ARG_NUM(0);
  312. VALIDATE_ARG_NUM(1);
  313. r_ret = Math::stepify((double)*p_args[0], (double)*p_args[1]);
  314. } break;
  315. case MATH_LERP: {
  316. VALIDATE_ARG_COUNT(3);
  317. VALIDATE_ARG_NUM(2);
  318. const double t = (double)*p_args[2];
  319. switch (p_args[0]->get_type() == p_args[1]->get_type() ? p_args[0]->get_type() : Variant::REAL) {
  320. case Variant::VECTOR2: {
  321. r_ret = ((Vector2)*p_args[0]).linear_interpolate((Vector2)*p_args[1], t);
  322. } break;
  323. case Variant::VECTOR3: {
  324. r_ret = ((Vector3)*p_args[0]).linear_interpolate((Vector3)*p_args[1], t);
  325. } break;
  326. case Variant::COLOR: {
  327. r_ret = ((Color)*p_args[0]).linear_interpolate((Color)*p_args[1], t);
  328. } break;
  329. default: {
  330. VALIDATE_ARG_NUM(0);
  331. VALIDATE_ARG_NUM(1);
  332. r_ret = Math::lerp((double)*p_args[0], (double)*p_args[1], t);
  333. } break;
  334. }
  335. } break;
  336. case MATH_INVERSE_LERP: {
  337. VALIDATE_ARG_COUNT(3);
  338. VALIDATE_ARG_NUM(0);
  339. VALIDATE_ARG_NUM(1);
  340. VALIDATE_ARG_NUM(2);
  341. r_ret = Math::inverse_lerp((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  342. } break;
  343. case MATH_RANGE_LERP: {
  344. VALIDATE_ARG_COUNT(5);
  345. VALIDATE_ARG_NUM(0);
  346. VALIDATE_ARG_NUM(1);
  347. VALIDATE_ARG_NUM(2);
  348. VALIDATE_ARG_NUM(3);
  349. VALIDATE_ARG_NUM(4);
  350. r_ret = Math::range_lerp((double)*p_args[0], (double)*p_args[1], (double)*p_args[2], (double)*p_args[3], (double)*p_args[4]);
  351. } break;
  352. case MATH_SMOOTHSTEP: {
  353. VALIDATE_ARG_COUNT(3);
  354. VALIDATE_ARG_NUM(0);
  355. VALIDATE_ARG_NUM(1);
  356. VALIDATE_ARG_NUM(2);
  357. r_ret = Math::smoothstep((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  358. } break;
  359. case MATH_DECTIME: {
  360. VALIDATE_ARG_COUNT(3);
  361. VALIDATE_ARG_NUM(0);
  362. VALIDATE_ARG_NUM(1);
  363. VALIDATE_ARG_NUM(2);
  364. r_ret = Math::dectime((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  365. } break;
  366. case MATH_RANDOMIZE: {
  367. VALIDATE_ARG_COUNT(0);
  368. Math::randomize();
  369. r_ret = Variant();
  370. } break;
  371. case MATH_RAND: {
  372. VALIDATE_ARG_COUNT(0);
  373. r_ret = Math::rand();
  374. } break;
  375. case MATH_RANDF: {
  376. VALIDATE_ARG_COUNT(0);
  377. r_ret = Math::randf();
  378. } break;
  379. case MATH_RANDOM: {
  380. VALIDATE_ARG_COUNT(2);
  381. VALIDATE_ARG_NUM(0);
  382. VALIDATE_ARG_NUM(1);
  383. r_ret = Math::random((double)*p_args[0], (double)*p_args[1]);
  384. } break;
  385. case MATH_SEED: {
  386. VALIDATE_ARG_COUNT(1);
  387. VALIDATE_ARG_NUM(0);
  388. uint64_t seed = *p_args[0];
  389. Math::seed(seed);
  390. r_ret = Variant();
  391. } break;
  392. case MATH_RANDSEED: {
  393. VALIDATE_ARG_COUNT(1);
  394. VALIDATE_ARG_NUM(0);
  395. uint64_t seed = *p_args[0];
  396. int ret = Math::rand_from_seed(&seed);
  397. Array reta;
  398. reta.push_back(ret);
  399. reta.push_back(seed);
  400. r_ret = reta;
  401. } break;
  402. case MATH_DEG2RAD: {
  403. VALIDATE_ARG_COUNT(1);
  404. VALIDATE_ARG_NUM(0);
  405. r_ret = Math::deg2rad((double)*p_args[0]);
  406. } break;
  407. case MATH_RAD2DEG: {
  408. VALIDATE_ARG_COUNT(1);
  409. VALIDATE_ARG_NUM(0);
  410. r_ret = Math::rad2deg((double)*p_args[0]);
  411. } break;
  412. case MATH_LINEAR2DB: {
  413. VALIDATE_ARG_COUNT(1);
  414. VALIDATE_ARG_NUM(0);
  415. r_ret = Math::linear2db((double)*p_args[0]);
  416. } break;
  417. case MATH_DB2LINEAR: {
  418. VALIDATE_ARG_COUNT(1);
  419. VALIDATE_ARG_NUM(0);
  420. r_ret = Math::db2linear((double)*p_args[0]);
  421. } break;
  422. case MATH_POLAR2CARTESIAN: {
  423. VALIDATE_ARG_COUNT(2);
  424. VALIDATE_ARG_NUM(0);
  425. VALIDATE_ARG_NUM(1);
  426. double r = *p_args[0];
  427. double th = *p_args[1];
  428. r_ret = Vector2(r * Math::cos(th), r * Math::sin(th));
  429. } break;
  430. case MATH_CARTESIAN2POLAR: {
  431. VALIDATE_ARG_COUNT(2);
  432. VALIDATE_ARG_NUM(0);
  433. VALIDATE_ARG_NUM(1);
  434. double x = *p_args[0];
  435. double y = *p_args[1];
  436. r_ret = Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x));
  437. } break;
  438. case MATH_WRAP: {
  439. VALIDATE_ARG_COUNT(3);
  440. r_ret = Math::wrapi((int64_t)*p_args[0], (int64_t)*p_args[1], (int64_t)*p_args[2]);
  441. } break;
  442. case MATH_WRAPF: {
  443. VALIDATE_ARG_COUNT(3);
  444. r_ret = Math::wrapf((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  445. } break;
  446. case LOGIC_MAX: {
  447. VALIDATE_ARG_COUNT(2);
  448. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT) {
  449. int64_t a = *p_args[0];
  450. int64_t b = *p_args[1];
  451. r_ret = MAX(a, b);
  452. } else {
  453. VALIDATE_ARG_NUM(0);
  454. VALIDATE_ARG_NUM(1);
  455. real_t a = *p_args[0];
  456. real_t b = *p_args[1];
  457. r_ret = MAX(a, b);
  458. }
  459. } break;
  460. case LOGIC_MIN: {
  461. VALIDATE_ARG_COUNT(2);
  462. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT) {
  463. int64_t a = *p_args[0];
  464. int64_t b = *p_args[1];
  465. r_ret = MIN(a, b);
  466. } else {
  467. VALIDATE_ARG_NUM(0);
  468. VALIDATE_ARG_NUM(1);
  469. real_t a = *p_args[0];
  470. real_t b = *p_args[1];
  471. r_ret = MIN(a, b);
  472. }
  473. } break;
  474. case LOGIC_CLAMP: {
  475. VALIDATE_ARG_COUNT(3);
  476. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT && p_args[2]->get_type() == Variant::INT) {
  477. int64_t a = *p_args[0];
  478. int64_t b = *p_args[1];
  479. int64_t c = *p_args[2];
  480. r_ret = CLAMP(a, b, c);
  481. } else {
  482. VALIDATE_ARG_NUM(0);
  483. VALIDATE_ARG_NUM(1);
  484. VALIDATE_ARG_NUM(2);
  485. real_t a = *p_args[0];
  486. real_t b = *p_args[1];
  487. real_t c = *p_args[2];
  488. r_ret = CLAMP(a, b, c);
  489. }
  490. } break;
  491. case LOGIC_NEAREST_PO2: {
  492. VALIDATE_ARG_COUNT(1);
  493. VALIDATE_ARG_NUM(0);
  494. int64_t num = *p_args[0];
  495. r_ret = next_power_of_2(num);
  496. } break;
  497. case OBJ_WEAKREF: {
  498. VALIDATE_ARG_COUNT(1);
  499. if (p_args[0]->get_type() != Variant::OBJECT) {
  500. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  501. r_error.argument = 0;
  502. r_error.expected = Variant::OBJECT;
  503. r_ret = Variant();
  504. return;
  505. }
  506. if (p_args[0]->is_ref()) {
  507. REF r = *p_args[0];
  508. if (!r.is_valid()) {
  509. r_ret = Variant();
  510. return;
  511. }
  512. Ref<WeakRef> wref = memnew(WeakRef);
  513. wref->set_ref(r);
  514. r_ret = wref;
  515. } else {
  516. Object *obj = *p_args[0];
  517. if (!obj) {
  518. r_ret = Variant();
  519. return;
  520. }
  521. Ref<WeakRef> wref = memnew(WeakRef);
  522. wref->set_obj(obj);
  523. r_ret = wref;
  524. }
  525. } break;
  526. case FUNC_FUNCREF: {
  527. VALIDATE_ARG_COUNT(2);
  528. if (p_args[0]->get_type() != Variant::OBJECT) {
  529. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  530. r_error.argument = 0;
  531. r_error.expected = Variant::OBJECT;
  532. r_ret = Variant();
  533. return;
  534. }
  535. if (p_args[1]->get_type() != Variant::STRING && p_args[1]->get_type() != Variant::NODE_PATH) {
  536. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  537. r_error.argument = 1;
  538. r_error.expected = Variant::STRING;
  539. r_ret = Variant();
  540. return;
  541. }
  542. Ref<FuncRef> fr = memnew(FuncRef);
  543. fr->set_instance(*p_args[0]);
  544. fr->set_function(*p_args[1]);
  545. r_ret = fr;
  546. } break;
  547. case TYPE_CONVERT: {
  548. VALIDATE_ARG_COUNT(2);
  549. VALIDATE_ARG_NUM(1);
  550. int type = *p_args[1];
  551. if (type < 0 || type >= Variant::VARIANT_MAX) {
  552. r_ret = RTR("Invalid type argument to convert(), use TYPE_* constants.");
  553. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  554. r_error.argument = 0;
  555. r_error.expected = Variant::INT;
  556. return;
  557. } else {
  558. r_ret = Variant::construct(Variant::Type(type), p_args, 1, r_error);
  559. }
  560. } break;
  561. case TYPE_OF: {
  562. VALIDATE_ARG_COUNT(1);
  563. r_ret = p_args[0]->get_type();
  564. } break;
  565. case TYPE_EXISTS: {
  566. VALIDATE_ARG_COUNT(1);
  567. r_ret = ClassDB::class_exists(*p_args[0]);
  568. } break;
  569. case TEXT_CHAR: {
  570. VALIDATE_ARG_COUNT(1);
  571. VALIDATE_ARG_NUM(0);
  572. CharType result[2] = { *p_args[0], 0 };
  573. r_ret = String(result);
  574. } break;
  575. case TEXT_STR: {
  576. if (p_arg_count < 1) {
  577. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  578. r_error.argument = 1;
  579. r_ret = Variant();
  580. return;
  581. }
  582. String str;
  583. for (int i = 0; i < p_arg_count; i++) {
  584. String os = p_args[i]->operator String();
  585. if (i == 0)
  586. str = os;
  587. else
  588. str += os;
  589. }
  590. r_ret = str;
  591. } break;
  592. case TEXT_PRINT: {
  593. String str;
  594. for (int i = 0; i < p_arg_count; i++) {
  595. str += p_args[i]->operator String();
  596. }
  597. print_line(str);
  598. r_ret = Variant();
  599. } break;
  600. case TEXT_PRINT_TABBED: {
  601. String str;
  602. for (int i = 0; i < p_arg_count; i++) {
  603. if (i)
  604. str += "\t";
  605. str += p_args[i]->operator String();
  606. }
  607. print_line(str);
  608. r_ret = Variant();
  609. } break;
  610. case TEXT_PRINT_SPACED: {
  611. String str;
  612. for (int i = 0; i < p_arg_count; i++) {
  613. if (i)
  614. str += " ";
  615. str += p_args[i]->operator String();
  616. }
  617. print_line(str);
  618. r_ret = Variant();
  619. } break;
  620. case TEXT_PRINTERR: {
  621. String str;
  622. for (int i = 0; i < p_arg_count; i++) {
  623. str += p_args[i]->operator String();
  624. }
  625. print_error(str);
  626. r_ret = Variant();
  627. } break;
  628. case TEXT_PRINTRAW: {
  629. String str;
  630. for (int i = 0; i < p_arg_count; i++) {
  631. str += p_args[i]->operator String();
  632. }
  633. OS::get_singleton()->print("%s", str.utf8().get_data());
  634. r_ret = Variant();
  635. } break;
  636. case TEXT_PRINT_DEBUG: {
  637. String str;
  638. for (int i = 0; i < p_arg_count; i++) {
  639. str += p_args[i]->operator String();
  640. }
  641. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  642. if (script->debug_get_stack_level_count() > 0) {
  643. str += "\n At: " + script->debug_get_stack_level_source(0) + ":" + itos(script->debug_get_stack_level_line(0)) + ":" + script->debug_get_stack_level_function(0) + "()";
  644. }
  645. print_line(str);
  646. r_ret = Variant();
  647. } break;
  648. case PUSH_ERROR: {
  649. VALIDATE_ARG_COUNT(1);
  650. if (p_args[0]->get_type() != Variant::STRING) {
  651. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  652. r_error.argument = 0;
  653. r_error.expected = Variant::STRING;
  654. r_ret = Variant();
  655. break;
  656. }
  657. String message = *p_args[0];
  658. ERR_PRINTS(message);
  659. r_ret = Variant();
  660. } break;
  661. case PUSH_WARNING: {
  662. VALIDATE_ARG_COUNT(1);
  663. if (p_args[0]->get_type() != Variant::STRING) {
  664. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  665. r_error.argument = 0;
  666. r_error.expected = Variant::STRING;
  667. r_ret = Variant();
  668. break;
  669. }
  670. String message = *p_args[0];
  671. WARN_PRINTS(message);
  672. r_ret = Variant();
  673. } break;
  674. case VAR_TO_STR: {
  675. VALIDATE_ARG_COUNT(1);
  676. String vars;
  677. VariantWriter::write_to_string(*p_args[0], vars);
  678. r_ret = vars;
  679. } break;
  680. case STR_TO_VAR: {
  681. VALIDATE_ARG_COUNT(1);
  682. if (p_args[0]->get_type() != Variant::STRING) {
  683. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  684. r_error.argument = 0;
  685. r_error.expected = Variant::STRING;
  686. r_ret = Variant();
  687. return;
  688. }
  689. r_ret = *p_args[0];
  690. VariantParser::StreamString ss;
  691. ss.s = *p_args[0];
  692. String errs;
  693. int line;
  694. (void)VariantParser::parse(&ss, r_ret, errs, line);
  695. } break;
  696. case VAR_TO_BYTES: {
  697. bool full_objects = false;
  698. if (p_arg_count < 1) {
  699. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  700. r_error.argument = 1;
  701. r_ret = Variant();
  702. return;
  703. } else if (p_arg_count > 2) {
  704. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  705. r_error.argument = 2;
  706. r_ret = Variant();
  707. } else if (p_arg_count == 2) {
  708. if (p_args[1]->get_type() != Variant::BOOL) {
  709. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  710. r_error.argument = 1;
  711. r_error.expected = Variant::BOOL;
  712. r_ret = Variant();
  713. return;
  714. }
  715. full_objects = *p_args[1];
  716. }
  717. PoolByteArray barr;
  718. int len;
  719. Error err = encode_variant(*p_args[0], NULL, len, full_objects);
  720. if (err) {
  721. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  722. r_error.argument = 0;
  723. r_error.expected = Variant::NIL;
  724. r_ret = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
  725. return;
  726. }
  727. barr.resize(len);
  728. {
  729. PoolByteArray::Write w = barr.write();
  730. encode_variant(*p_args[0], w.ptr(), len, full_objects);
  731. }
  732. r_ret = barr;
  733. } break;
  734. case BYTES_TO_VAR: {
  735. bool allow_objects = false;
  736. if (p_arg_count < 1) {
  737. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  738. r_error.argument = 1;
  739. r_ret = Variant();
  740. return;
  741. } else if (p_arg_count > 2) {
  742. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  743. r_error.argument = 2;
  744. r_ret = Variant();
  745. } else if (p_arg_count == 2) {
  746. if (p_args[1]->get_type() != Variant::BOOL) {
  747. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  748. r_error.argument = 1;
  749. r_error.expected = Variant::BOOL;
  750. r_ret = Variant();
  751. return;
  752. }
  753. allow_objects = *p_args[1];
  754. }
  755. if (p_args[0]->get_type() != Variant::POOL_BYTE_ARRAY) {
  756. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  757. r_error.argument = 1;
  758. r_error.expected = Variant::POOL_BYTE_ARRAY;
  759. r_ret = Variant();
  760. return;
  761. }
  762. PoolByteArray varr = *p_args[0];
  763. Variant ret;
  764. {
  765. PoolByteArray::Read r = varr.read();
  766. Error err = decode_variant(ret, r.ptr(), varr.size(), NULL, allow_objects);
  767. if (err != OK) {
  768. r_ret = RTR("Not enough bytes for decoding bytes, or invalid format.");
  769. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  770. r_error.argument = 0;
  771. r_error.expected = Variant::POOL_BYTE_ARRAY;
  772. return;
  773. }
  774. }
  775. r_ret = ret;
  776. } break;
  777. case GEN_RANGE: {
  778. switch (p_arg_count) {
  779. case 0: {
  780. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  781. r_error.argument = 1;
  782. r_ret = Variant();
  783. } break;
  784. case 1: {
  785. VALIDATE_ARG_NUM(0);
  786. int count = *p_args[0];
  787. Array arr;
  788. if (count <= 0) {
  789. r_ret = arr;
  790. return;
  791. }
  792. Error err = arr.resize(count);
  793. if (err != OK) {
  794. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  795. r_ret = Variant();
  796. return;
  797. }
  798. for (int i = 0; i < count; i++) {
  799. arr[i] = i;
  800. }
  801. r_ret = arr;
  802. } break;
  803. case 2: {
  804. VALIDATE_ARG_NUM(0);
  805. VALIDATE_ARG_NUM(1);
  806. int from = *p_args[0];
  807. int to = *p_args[1];
  808. Array arr;
  809. if (from >= to) {
  810. r_ret = arr;
  811. return;
  812. }
  813. Error err = arr.resize(to - from);
  814. if (err != OK) {
  815. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  816. r_ret = Variant();
  817. return;
  818. }
  819. for (int i = from; i < to; i++)
  820. arr[i - from] = i;
  821. r_ret = arr;
  822. } break;
  823. case 3: {
  824. VALIDATE_ARG_NUM(0);
  825. VALIDATE_ARG_NUM(1);
  826. VALIDATE_ARG_NUM(2);
  827. int from = *p_args[0];
  828. int to = *p_args[1];
  829. int incr = *p_args[2];
  830. if (incr == 0) {
  831. r_ret = RTR("Step argument is zero!");
  832. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  833. return;
  834. }
  835. Array arr;
  836. if (from >= to && incr > 0) {
  837. r_ret = arr;
  838. return;
  839. }
  840. if (from <= to && incr < 0) {
  841. r_ret = arr;
  842. return;
  843. }
  844. //calculate how many
  845. int count = 0;
  846. if (incr > 0) {
  847. count = ((to - from - 1) / incr) + 1;
  848. } else {
  849. count = ((from - to - 1) / -incr) + 1;
  850. }
  851. Error err = arr.resize(count);
  852. if (err != OK) {
  853. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  854. r_ret = Variant();
  855. return;
  856. }
  857. if (incr > 0) {
  858. int idx = 0;
  859. for (int i = from; i < to; i += incr) {
  860. arr[idx++] = i;
  861. }
  862. } else {
  863. int idx = 0;
  864. for (int i = from; i > to; i += incr) {
  865. arr[idx++] = i;
  866. }
  867. }
  868. r_ret = arr;
  869. } break;
  870. default: {
  871. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  872. r_error.argument = 3;
  873. r_ret = Variant();
  874. } break;
  875. }
  876. } break;
  877. case RESOURCE_LOAD: {
  878. VALIDATE_ARG_COUNT(1);
  879. if (p_args[0]->get_type() != Variant::STRING) {
  880. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  881. r_error.argument = 0;
  882. r_error.expected = Variant::STRING;
  883. r_ret = Variant();
  884. } else {
  885. r_ret = ResourceLoader::load(*p_args[0]);
  886. }
  887. } break;
  888. case INST2DICT: {
  889. VALIDATE_ARG_COUNT(1);
  890. if (p_args[0]->get_type() == Variant::NIL) {
  891. r_ret = Variant();
  892. } else if (p_args[0]->get_type() != Variant::OBJECT) {
  893. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  894. r_error.argument = 0;
  895. r_ret = Variant();
  896. } else {
  897. Object *obj = *p_args[0];
  898. if (!obj) {
  899. r_ret = Variant();
  900. } else if (!obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton()) {
  901. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  902. r_error.argument = 0;
  903. r_error.expected = Variant::DICTIONARY;
  904. r_ret = RTR("Not a script with an instance");
  905. return;
  906. } else {
  907. GDScriptInstance *ins = static_cast<GDScriptInstance *>(obj->get_script_instance());
  908. Ref<GDScript> base = ins->get_script();
  909. if (base.is_null()) {
  910. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  911. r_error.argument = 0;
  912. r_error.expected = Variant::DICTIONARY;
  913. r_ret = RTR("Not based on a script");
  914. return;
  915. }
  916. GDScript *p = base.ptr();
  917. Vector<StringName> sname;
  918. while (p->_owner) {
  919. sname.push_back(p->name);
  920. p = p->_owner;
  921. }
  922. sname.invert();
  923. if (!p->path.is_resource_file()) {
  924. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  925. r_error.argument = 0;
  926. r_error.expected = Variant::DICTIONARY;
  927. r_ret = Variant();
  928. r_ret = RTR("Not based on a resource file");
  929. return;
  930. }
  931. NodePath cp(sname, Vector<StringName>(), false);
  932. Dictionary d;
  933. d["@subpath"] = cp;
  934. d["@path"] = p->path;
  935. p = base.ptr();
  936. while (p) {
  937. for (Set<StringName>::Element *E = p->members.front(); E; E = E->next()) {
  938. Variant value;
  939. if (ins->get(E->get(), value)) {
  940. String k = E->get();
  941. if (!d.has(k)) {
  942. d[k] = value;
  943. }
  944. }
  945. }
  946. p = p->_base;
  947. }
  948. r_ret = d;
  949. }
  950. }
  951. } break;
  952. case DICT2INST: {
  953. VALIDATE_ARG_COUNT(1);
  954. if (p_args[0]->get_type() != Variant::DICTIONARY) {
  955. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  956. r_error.argument = 0;
  957. r_error.expected = Variant::DICTIONARY;
  958. r_ret = Variant();
  959. return;
  960. }
  961. Dictionary d = *p_args[0];
  962. if (!d.has("@path")) {
  963. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  964. r_error.argument = 0;
  965. r_error.expected = Variant::OBJECT;
  966. r_ret = RTR("Invalid instance dictionary format (missing @path)");
  967. return;
  968. }
  969. Ref<Script> scr = ResourceLoader::load(d["@path"]);
  970. if (!scr.is_valid()) {
  971. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  972. r_error.argument = 0;
  973. r_error.expected = Variant::OBJECT;
  974. r_ret = RTR("Invalid instance dictionary format (can't load script at @path)");
  975. return;
  976. }
  977. Ref<GDScript> gdscr = scr;
  978. if (!gdscr.is_valid()) {
  979. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  980. r_error.argument = 0;
  981. r_error.expected = Variant::OBJECT;
  982. r_ret = Variant();
  983. r_ret = RTR("Invalid instance dictionary format (invalid script at @path)");
  984. return;
  985. }
  986. NodePath sub;
  987. if (d.has("@subpath")) {
  988. sub = d["@subpath"];
  989. }
  990. for (int i = 0; i < sub.get_name_count(); i++) {
  991. gdscr = gdscr->subclasses[sub.get_name(i)];
  992. if (!gdscr.is_valid()) {
  993. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  994. r_error.argument = 0;
  995. r_error.expected = Variant::OBJECT;
  996. r_ret = Variant();
  997. r_ret = RTR("Invalid instance dictionary (invalid subclasses)");
  998. return;
  999. }
  1000. }
  1001. r_ret = gdscr->_new(NULL, 0, r_error);
  1002. GDScriptInstance *ins = static_cast<GDScriptInstance *>(static_cast<Object *>(r_ret)->get_script_instance());
  1003. Ref<GDScript> gd_ref = ins->get_script();
  1004. for (Map<StringName, GDScript::MemberInfo>::Element *E = gd_ref->member_indices.front(); E; E = E->next()) {
  1005. if (d.has(E->key())) {
  1006. ins->members.write[E->get().index] = d[E->key()];
  1007. }
  1008. }
  1009. } break;
  1010. case VALIDATE_JSON: {
  1011. VALIDATE_ARG_COUNT(1);
  1012. if (p_args[0]->get_type() != Variant::STRING) {
  1013. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1014. r_error.argument = 0;
  1015. r_error.expected = Variant::STRING;
  1016. r_ret = Variant();
  1017. return;
  1018. }
  1019. String errs;
  1020. int errl;
  1021. Error err = JSON::parse(*p_args[0], r_ret, errs, errl);
  1022. if (err != OK) {
  1023. r_ret = itos(errl) + ":" + errs;
  1024. } else {
  1025. r_ret = "";
  1026. }
  1027. } break;
  1028. case PARSE_JSON: {
  1029. VALIDATE_ARG_COUNT(1);
  1030. if (p_args[0]->get_type() != Variant::STRING) {
  1031. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1032. r_error.argument = 0;
  1033. r_error.expected = Variant::STRING;
  1034. r_ret = Variant();
  1035. return;
  1036. }
  1037. String errs;
  1038. int errl;
  1039. Error err = JSON::parse(*p_args[0], r_ret, errs, errl);
  1040. if (err != OK) {
  1041. r_ret = Variant();
  1042. }
  1043. } break;
  1044. case TO_JSON: {
  1045. VALIDATE_ARG_COUNT(1);
  1046. r_ret = JSON::print(*p_args[0]);
  1047. } break;
  1048. case HASH: {
  1049. VALIDATE_ARG_COUNT(1);
  1050. r_ret = p_args[0]->hash();
  1051. } break;
  1052. case COLOR8: {
  1053. if (p_arg_count < 3) {
  1054. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1055. r_error.argument = 3;
  1056. r_ret = Variant();
  1057. return;
  1058. }
  1059. if (p_arg_count > 4) {
  1060. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1061. r_error.argument = 4;
  1062. r_ret = Variant();
  1063. return;
  1064. }
  1065. VALIDATE_ARG_NUM(0);
  1066. VALIDATE_ARG_NUM(1);
  1067. VALIDATE_ARG_NUM(2);
  1068. Color color((float)*p_args[0] / 255.0f, (float)*p_args[1] / 255.0f, (float)*p_args[2] / 255.0f);
  1069. if (p_arg_count == 4) {
  1070. VALIDATE_ARG_NUM(3);
  1071. color.a = (float)*p_args[3] / 255.0f;
  1072. }
  1073. r_ret = color;
  1074. } break;
  1075. case COLORN: {
  1076. if (p_arg_count < 1) {
  1077. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1078. r_error.argument = 1;
  1079. r_ret = Variant();
  1080. return;
  1081. }
  1082. if (p_arg_count > 2) {
  1083. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1084. r_error.argument = 2;
  1085. r_ret = Variant();
  1086. return;
  1087. }
  1088. if (p_args[0]->get_type() != Variant::STRING) {
  1089. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1090. r_error.argument = 0;
  1091. r_ret = Variant();
  1092. } else {
  1093. Color color = Color::named(*p_args[0]);
  1094. if (p_arg_count == 2) {
  1095. VALIDATE_ARG_NUM(1);
  1096. color.a = *p_args[1];
  1097. }
  1098. r_ret = color;
  1099. }
  1100. } break;
  1101. case PRINT_STACK: {
  1102. VALIDATE_ARG_COUNT(0);
  1103. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  1104. for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
  1105. print_line("Frame " + itos(i) + " - " + script->debug_get_stack_level_source(i) + ":" + itos(script->debug_get_stack_level_line(i)) + " in function '" + script->debug_get_stack_level_function(i) + "'");
  1106. };
  1107. } break;
  1108. case GET_STACK: {
  1109. VALIDATE_ARG_COUNT(0);
  1110. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  1111. Array ret;
  1112. for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
  1113. Dictionary frame;
  1114. frame["source"] = script->debug_get_stack_level_source(i);
  1115. frame["function"] = script->debug_get_stack_level_function(i);
  1116. frame["line"] = script->debug_get_stack_level_line(i);
  1117. ret.push_back(frame);
  1118. };
  1119. r_ret = ret;
  1120. } break;
  1121. case INSTANCE_FROM_ID: {
  1122. VALIDATE_ARG_COUNT(1);
  1123. if (p_args[0]->get_type() != Variant::INT && p_args[0]->get_type() != Variant::REAL) {
  1124. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1125. r_error.argument = 0;
  1126. r_error.expected = Variant::INT;
  1127. r_ret = Variant();
  1128. break;
  1129. }
  1130. uint32_t id = *p_args[0];
  1131. r_ret = ObjectDB::get_instance(id);
  1132. } break;
  1133. case LEN: {
  1134. VALIDATE_ARG_COUNT(1);
  1135. switch (p_args[0]->get_type()) {
  1136. case Variant::STRING: {
  1137. String d = *p_args[0];
  1138. r_ret = d.length();
  1139. } break;
  1140. case Variant::DICTIONARY: {
  1141. Dictionary d = *p_args[0];
  1142. r_ret = d.size();
  1143. } break;
  1144. case Variant::ARRAY: {
  1145. Array d = *p_args[0];
  1146. r_ret = d.size();
  1147. } break;
  1148. case Variant::POOL_BYTE_ARRAY: {
  1149. PoolVector<uint8_t> d = *p_args[0];
  1150. r_ret = d.size();
  1151. } break;
  1152. case Variant::POOL_INT_ARRAY: {
  1153. PoolVector<int> d = *p_args[0];
  1154. r_ret = d.size();
  1155. } break;
  1156. case Variant::POOL_REAL_ARRAY: {
  1157. PoolVector<real_t> d = *p_args[0];
  1158. r_ret = d.size();
  1159. } break;
  1160. case Variant::POOL_STRING_ARRAY: {
  1161. PoolVector<String> d = *p_args[0];
  1162. r_ret = d.size();
  1163. } break;
  1164. case Variant::POOL_VECTOR2_ARRAY: {
  1165. PoolVector<Vector2> d = *p_args[0];
  1166. r_ret = d.size();
  1167. } break;
  1168. case Variant::POOL_VECTOR3_ARRAY: {
  1169. PoolVector<Vector3> d = *p_args[0];
  1170. r_ret = d.size();
  1171. } break;
  1172. case Variant::POOL_COLOR_ARRAY: {
  1173. PoolVector<Color> d = *p_args[0];
  1174. r_ret = d.size();
  1175. } break;
  1176. default: {
  1177. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1178. r_error.argument = 0;
  1179. r_error.expected = Variant::OBJECT;
  1180. r_ret = Variant();
  1181. r_ret = RTR("Object can't provide a length.");
  1182. }
  1183. }
  1184. } break;
  1185. case IS_INSTANCE_VALID: {
  1186. VALIDATE_ARG_COUNT(1);
  1187. if (p_args[0]->get_type() != Variant::OBJECT) {
  1188. r_ret = false;
  1189. } else {
  1190. Object *obj = *p_args[0];
  1191. r_ret = ObjectDB::instance_validate(obj);
  1192. }
  1193. } break;
  1194. case FUNC_MAX: {
  1195. ERR_FAIL();
  1196. } break;
  1197. }
  1198. }
  1199. bool GDScriptFunctions::is_deterministic(Function p_func) {
  1200. //man i couldn't have chosen a worse function name,
  1201. //way too controversial..
  1202. switch (p_func) {
  1203. case MATH_SIN:
  1204. case MATH_COS:
  1205. case MATH_TAN:
  1206. case MATH_SINH:
  1207. case MATH_COSH:
  1208. case MATH_TANH:
  1209. case MATH_ASIN:
  1210. case MATH_ACOS:
  1211. case MATH_ATAN:
  1212. case MATH_ATAN2:
  1213. case MATH_SQRT:
  1214. case MATH_FMOD:
  1215. case MATH_FPOSMOD:
  1216. case MATH_FLOOR:
  1217. case MATH_CEIL:
  1218. case MATH_ROUND:
  1219. case MATH_ABS:
  1220. case MATH_SIGN:
  1221. case MATH_POW:
  1222. case MATH_LOG:
  1223. case MATH_EXP:
  1224. case MATH_ISNAN:
  1225. case MATH_ISINF:
  1226. case MATH_EASE:
  1227. case MATH_DECIMALS:
  1228. case MATH_STEPIFY:
  1229. case MATH_LERP:
  1230. case MATH_INVERSE_LERP:
  1231. case MATH_RANGE_LERP:
  1232. case MATH_SMOOTHSTEP:
  1233. case MATH_DECTIME:
  1234. case MATH_DEG2RAD:
  1235. case MATH_RAD2DEG:
  1236. case MATH_LINEAR2DB:
  1237. case MATH_DB2LINEAR:
  1238. case MATH_POLAR2CARTESIAN:
  1239. case MATH_CARTESIAN2POLAR:
  1240. case MATH_WRAP:
  1241. case MATH_WRAPF:
  1242. case LOGIC_MAX:
  1243. case LOGIC_MIN:
  1244. case LOGIC_CLAMP:
  1245. case LOGIC_NEAREST_PO2:
  1246. case TYPE_CONVERT:
  1247. case TYPE_OF:
  1248. case TYPE_EXISTS:
  1249. case TEXT_CHAR:
  1250. case TEXT_STR:
  1251. case COLOR8:
  1252. case LEN:
  1253. // enable for debug only, otherwise not desirable - case GEN_RANGE:
  1254. return true;
  1255. default:
  1256. return false;
  1257. }
  1258. return false;
  1259. }
  1260. MethodInfo GDScriptFunctions::get_info(Function p_func) {
  1261. #ifdef DEBUG_ENABLED
  1262. //using a switch, so the compiler generates a jumptable
  1263. switch (p_func) {
  1264. case MATH_SIN: {
  1265. MethodInfo mi("sin", PropertyInfo(Variant::REAL, "s"));
  1266. mi.return_val.type = Variant::REAL;
  1267. return mi;
  1268. } break;
  1269. case MATH_COS: {
  1270. MethodInfo mi("cos", PropertyInfo(Variant::REAL, "s"));
  1271. mi.return_val.type = Variant::REAL;
  1272. return mi;
  1273. } break;
  1274. case MATH_TAN: {
  1275. MethodInfo mi("tan", PropertyInfo(Variant::REAL, "s"));
  1276. mi.return_val.type = Variant::REAL;
  1277. return mi;
  1278. } break;
  1279. case MATH_SINH: {
  1280. MethodInfo mi("sinh", PropertyInfo(Variant::REAL, "s"));
  1281. mi.return_val.type = Variant::REAL;
  1282. return mi;
  1283. } break;
  1284. case MATH_COSH: {
  1285. MethodInfo mi("cosh", PropertyInfo(Variant::REAL, "s"));
  1286. mi.return_val.type = Variant::REAL;
  1287. return mi;
  1288. } break;
  1289. case MATH_TANH: {
  1290. MethodInfo mi("tanh", PropertyInfo(Variant::REAL, "s"));
  1291. mi.return_val.type = Variant::REAL;
  1292. return mi;
  1293. } break;
  1294. case MATH_ASIN: {
  1295. MethodInfo mi("asin", PropertyInfo(Variant::REAL, "s"));
  1296. mi.return_val.type = Variant::REAL;
  1297. return mi;
  1298. } break;
  1299. case MATH_ACOS: {
  1300. MethodInfo mi("acos", PropertyInfo(Variant::REAL, "s"));
  1301. mi.return_val.type = Variant::REAL;
  1302. return mi;
  1303. } break;
  1304. case MATH_ATAN: {
  1305. MethodInfo mi("atan", PropertyInfo(Variant::REAL, "s"));
  1306. mi.return_val.type = Variant::REAL;
  1307. return mi;
  1308. } break;
  1309. case MATH_ATAN2: {
  1310. MethodInfo mi("atan2", PropertyInfo(Variant::REAL, "y"), PropertyInfo(Variant::REAL, "x"));
  1311. mi.return_val.type = Variant::REAL;
  1312. return mi;
  1313. } break;
  1314. case MATH_SQRT: {
  1315. MethodInfo mi("sqrt", PropertyInfo(Variant::REAL, "s"));
  1316. mi.return_val.type = Variant::REAL;
  1317. return mi;
  1318. } break;
  1319. case MATH_FMOD: {
  1320. MethodInfo mi("fmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1321. mi.return_val.type = Variant::REAL;
  1322. return mi;
  1323. } break;
  1324. case MATH_FPOSMOD: {
  1325. MethodInfo mi("fposmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1326. mi.return_val.type = Variant::REAL;
  1327. return mi;
  1328. } break;
  1329. case MATH_FLOOR: {
  1330. MethodInfo mi("floor", PropertyInfo(Variant::REAL, "s"));
  1331. mi.return_val.type = Variant::REAL;
  1332. return mi;
  1333. } break;
  1334. case MATH_CEIL: {
  1335. MethodInfo mi("ceil", PropertyInfo(Variant::REAL, "s"));
  1336. mi.return_val.type = Variant::REAL;
  1337. return mi;
  1338. } break;
  1339. case MATH_ROUND: {
  1340. MethodInfo mi("round", PropertyInfo(Variant::REAL, "s"));
  1341. mi.return_val.type = Variant::REAL;
  1342. return mi;
  1343. } break;
  1344. case MATH_ABS: {
  1345. MethodInfo mi("abs", PropertyInfo(Variant::REAL, "s"));
  1346. mi.return_val.type = Variant::REAL;
  1347. return mi;
  1348. } break;
  1349. case MATH_SIGN: {
  1350. MethodInfo mi("sign", PropertyInfo(Variant::REAL, "s"));
  1351. mi.return_val.type = Variant::REAL;
  1352. return mi;
  1353. } break;
  1354. case MATH_POW: {
  1355. MethodInfo mi("pow", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1356. mi.return_val.type = Variant::REAL;
  1357. return mi;
  1358. } break;
  1359. case MATH_LOG: {
  1360. MethodInfo mi("log", PropertyInfo(Variant::REAL, "s"));
  1361. mi.return_val.type = Variant::REAL;
  1362. return mi;
  1363. } break;
  1364. case MATH_EXP: {
  1365. MethodInfo mi("exp", PropertyInfo(Variant::REAL, "s"));
  1366. mi.return_val.type = Variant::REAL;
  1367. return mi;
  1368. } break;
  1369. case MATH_ISNAN: {
  1370. MethodInfo mi("is_nan", PropertyInfo(Variant::REAL, "s"));
  1371. mi.return_val.type = Variant::BOOL;
  1372. return mi;
  1373. } break;
  1374. case MATH_ISINF: {
  1375. MethodInfo mi("is_inf", PropertyInfo(Variant::REAL, "s"));
  1376. mi.return_val.type = Variant::BOOL;
  1377. return mi;
  1378. } break;
  1379. case MATH_EASE: {
  1380. MethodInfo mi("ease", PropertyInfo(Variant::REAL, "s"), PropertyInfo(Variant::REAL, "curve"));
  1381. mi.return_val.type = Variant::REAL;
  1382. return mi;
  1383. } break;
  1384. case MATH_DECIMALS: {
  1385. MethodInfo mi("decimals", PropertyInfo(Variant::REAL, "step"));
  1386. mi.return_val.type = Variant::REAL;
  1387. return mi;
  1388. } break;
  1389. case MATH_STEPIFY: {
  1390. MethodInfo mi("stepify", PropertyInfo(Variant::REAL, "s"), PropertyInfo(Variant::REAL, "step"));
  1391. mi.return_val.type = Variant::REAL;
  1392. return mi;
  1393. } break;
  1394. case MATH_LERP: {
  1395. MethodInfo mi("lerp", PropertyInfo(Variant::NIL, "from"), PropertyInfo(Variant::NIL, "to"), PropertyInfo(Variant::REAL, "weight"));
  1396. mi.return_val.type = Variant::NIL;
  1397. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1398. return mi;
  1399. } break;
  1400. case MATH_INVERSE_LERP: {
  1401. MethodInfo mi("inverse_lerp", PropertyInfo(Variant::REAL, "from"), PropertyInfo(Variant::REAL, "to"), PropertyInfo(Variant::REAL, "weight"));
  1402. mi.return_val.type = Variant::REAL;
  1403. return mi;
  1404. } break;
  1405. case MATH_RANGE_LERP: {
  1406. MethodInfo mi("range_lerp", PropertyInfo(Variant::REAL, "value"), PropertyInfo(Variant::REAL, "istart"), PropertyInfo(Variant::REAL, "istop"), PropertyInfo(Variant::REAL, "ostart"), PropertyInfo(Variant::REAL, "ostop"));
  1407. mi.return_val.type = Variant::REAL;
  1408. return mi;
  1409. } break;
  1410. case MATH_SMOOTHSTEP: {
  1411. MethodInfo mi("smoothstep", PropertyInfo(Variant::REAL, "from"), PropertyInfo(Variant::REAL, "to"), PropertyInfo(Variant::REAL, "weight"));
  1412. mi.return_val.type = Variant::REAL;
  1413. return mi;
  1414. } break;
  1415. case MATH_DECTIME: {
  1416. MethodInfo mi("dectime", PropertyInfo(Variant::REAL, "value"), PropertyInfo(Variant::REAL, "amount"), PropertyInfo(Variant::REAL, "step"));
  1417. mi.return_val.type = Variant::REAL;
  1418. return mi;
  1419. } break;
  1420. case MATH_RANDOMIZE: {
  1421. MethodInfo mi("randomize");
  1422. mi.return_val.type = Variant::NIL;
  1423. return mi;
  1424. } break;
  1425. case MATH_RAND: {
  1426. MethodInfo mi("randi");
  1427. mi.return_val.type = Variant::INT;
  1428. return mi;
  1429. } break;
  1430. case MATH_RANDF: {
  1431. MethodInfo mi("randf");
  1432. mi.return_val.type = Variant::REAL;
  1433. return mi;
  1434. } break;
  1435. case MATH_RANDOM: {
  1436. MethodInfo mi("rand_range", PropertyInfo(Variant::REAL, "from"), PropertyInfo(Variant::REAL, "to"));
  1437. mi.return_val.type = Variant::REAL;
  1438. return mi;
  1439. } break;
  1440. case MATH_SEED: {
  1441. MethodInfo mi("seed", PropertyInfo(Variant::INT, "seed"));
  1442. mi.return_val.type = Variant::NIL;
  1443. return mi;
  1444. } break;
  1445. case MATH_RANDSEED: {
  1446. MethodInfo mi("rand_seed", PropertyInfo(Variant::INT, "seed"));
  1447. mi.return_val.type = Variant::ARRAY;
  1448. return mi;
  1449. } break;
  1450. case MATH_DEG2RAD: {
  1451. MethodInfo mi("deg2rad", PropertyInfo(Variant::REAL, "deg"));
  1452. mi.return_val.type = Variant::REAL;
  1453. return mi;
  1454. } break;
  1455. case MATH_RAD2DEG: {
  1456. MethodInfo mi("rad2deg", PropertyInfo(Variant::REAL, "rad"));
  1457. mi.return_val.type = Variant::REAL;
  1458. return mi;
  1459. } break;
  1460. case MATH_LINEAR2DB: {
  1461. MethodInfo mi("linear2db", PropertyInfo(Variant::REAL, "nrg"));
  1462. mi.return_val.type = Variant::REAL;
  1463. return mi;
  1464. } break;
  1465. case MATH_DB2LINEAR: {
  1466. MethodInfo mi("db2linear", PropertyInfo(Variant::REAL, "db"));
  1467. mi.return_val.type = Variant::REAL;
  1468. return mi;
  1469. } break;
  1470. case MATH_POLAR2CARTESIAN: {
  1471. MethodInfo mi("polar2cartesian", PropertyInfo(Variant::REAL, "r"), PropertyInfo(Variant::REAL, "th"));
  1472. mi.return_val.type = Variant::VECTOR2;
  1473. return mi;
  1474. } break;
  1475. case MATH_CARTESIAN2POLAR: {
  1476. MethodInfo mi("cartesian2polar", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1477. mi.return_val.type = Variant::VECTOR2;
  1478. return mi;
  1479. } break;
  1480. case MATH_WRAP: {
  1481. MethodInfo mi("wrapi", PropertyInfo(Variant::INT, "value"), PropertyInfo(Variant::INT, "min"), PropertyInfo(Variant::INT, "max"));
  1482. mi.return_val.type = Variant::INT;
  1483. return mi;
  1484. } break;
  1485. case MATH_WRAPF: {
  1486. MethodInfo mi("wrapf", PropertyInfo(Variant::REAL, "value"), PropertyInfo(Variant::REAL, "min"), PropertyInfo(Variant::REAL, "max"));
  1487. mi.return_val.type = Variant::REAL;
  1488. return mi;
  1489. } break;
  1490. case LOGIC_MAX: {
  1491. MethodInfo mi("max", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
  1492. mi.return_val.type = Variant::REAL;
  1493. return mi;
  1494. } break;
  1495. case LOGIC_MIN: {
  1496. MethodInfo mi("min", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
  1497. mi.return_val.type = Variant::REAL;
  1498. return mi;
  1499. } break;
  1500. case LOGIC_CLAMP: {
  1501. MethodInfo mi("clamp", PropertyInfo(Variant::REAL, "value"), PropertyInfo(Variant::REAL, "min"), PropertyInfo(Variant::REAL, "max"));
  1502. mi.return_val.type = Variant::REAL;
  1503. return mi;
  1504. } break;
  1505. case LOGIC_NEAREST_PO2: {
  1506. MethodInfo mi("nearest_po2", PropertyInfo(Variant::INT, "value"));
  1507. mi.return_val.type = Variant::INT;
  1508. return mi;
  1509. } break;
  1510. case OBJ_WEAKREF: {
  1511. MethodInfo mi("weakref", PropertyInfo(Variant::OBJECT, "obj"));
  1512. mi.return_val.type = Variant::OBJECT;
  1513. mi.return_val.class_name = "WeakRef";
  1514. return mi;
  1515. } break;
  1516. case FUNC_FUNCREF: {
  1517. MethodInfo mi("funcref", PropertyInfo(Variant::OBJECT, "instance"), PropertyInfo(Variant::STRING, "funcname"));
  1518. mi.return_val.type = Variant::OBJECT;
  1519. mi.return_val.class_name = "FuncRef";
  1520. return mi;
  1521. } break;
  1522. case TYPE_CONVERT: {
  1523. MethodInfo mi("convert", PropertyInfo(Variant::NIL, "what", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::INT, "type"));
  1524. mi.return_val.type = Variant::NIL;
  1525. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1526. return mi;
  1527. } break;
  1528. case TYPE_OF: {
  1529. MethodInfo mi("typeof", PropertyInfo(Variant::NIL, "what", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1530. mi.return_val.type = Variant::INT;
  1531. return mi;
  1532. } break;
  1533. case TYPE_EXISTS: {
  1534. MethodInfo mi("type_exists", PropertyInfo(Variant::STRING, "type"));
  1535. mi.return_val.type = Variant::BOOL;
  1536. return mi;
  1537. } break;
  1538. case TEXT_CHAR: {
  1539. MethodInfo mi("char", PropertyInfo(Variant::INT, "ascii"));
  1540. mi.return_val.type = Variant::STRING;
  1541. return mi;
  1542. } break;
  1543. case TEXT_STR: {
  1544. MethodInfo mi("str");
  1545. mi.return_val.type = Variant::STRING;
  1546. mi.flags |= METHOD_FLAG_VARARG;
  1547. return mi;
  1548. } break;
  1549. case TEXT_PRINT: {
  1550. MethodInfo mi("print");
  1551. mi.return_val.type = Variant::NIL;
  1552. mi.flags |= METHOD_FLAG_VARARG;
  1553. return mi;
  1554. } break;
  1555. case TEXT_PRINT_TABBED: {
  1556. MethodInfo mi("printt");
  1557. mi.return_val.type = Variant::NIL;
  1558. mi.flags |= METHOD_FLAG_VARARG;
  1559. return mi;
  1560. } break;
  1561. case TEXT_PRINT_SPACED: {
  1562. MethodInfo mi("prints");
  1563. mi.return_val.type = Variant::NIL;
  1564. mi.flags |= METHOD_FLAG_VARARG;
  1565. return mi;
  1566. } break;
  1567. case TEXT_PRINTERR: {
  1568. MethodInfo mi("printerr");
  1569. mi.return_val.type = Variant::NIL;
  1570. mi.flags |= METHOD_FLAG_VARARG;
  1571. return mi;
  1572. } break;
  1573. case TEXT_PRINTRAW: {
  1574. MethodInfo mi("printraw");
  1575. mi.return_val.type = Variant::NIL;
  1576. mi.flags |= METHOD_FLAG_VARARG;
  1577. return mi;
  1578. } break;
  1579. case TEXT_PRINT_DEBUG: {
  1580. MethodInfo mi("print_debug");
  1581. mi.return_val.type = Variant::NIL;
  1582. mi.flags |= METHOD_FLAG_VARARG;
  1583. return mi;
  1584. } break;
  1585. case PUSH_ERROR: {
  1586. MethodInfo mi(Variant::NIL, "push_error", PropertyInfo(Variant::STRING, "message"));
  1587. mi.return_val.type = Variant::NIL;
  1588. return mi;
  1589. } break;
  1590. case PUSH_WARNING: {
  1591. MethodInfo mi(Variant::NIL, "push_warning", PropertyInfo(Variant::STRING, "message"));
  1592. mi.return_val.type = Variant::NIL;
  1593. return mi;
  1594. } break;
  1595. case VAR_TO_STR: {
  1596. MethodInfo mi("var2str", PropertyInfo(Variant::NIL, "var", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1597. mi.return_val.type = Variant::STRING;
  1598. return mi;
  1599. } break;
  1600. case STR_TO_VAR: {
  1601. MethodInfo mi(Variant::NIL, "str2var", PropertyInfo(Variant::STRING, "string"));
  1602. mi.return_val.type = Variant::NIL;
  1603. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1604. return mi;
  1605. } break;
  1606. case VAR_TO_BYTES: {
  1607. MethodInfo mi("var2bytes", PropertyInfo(Variant::NIL, "var", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::BOOL, "full_objects"));
  1608. mi.default_arguments.push_back(false);
  1609. mi.return_val.type = Variant::POOL_BYTE_ARRAY;
  1610. return mi;
  1611. } break;
  1612. case BYTES_TO_VAR: {
  1613. MethodInfo mi(Variant::NIL, "bytes2var", PropertyInfo(Variant::POOL_BYTE_ARRAY, "bytes"), PropertyInfo(Variant::BOOL, "allow_objects"));
  1614. mi.default_arguments.push_back(false);
  1615. mi.return_val.type = Variant::NIL;
  1616. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1617. return mi;
  1618. } break;
  1619. case GEN_RANGE: {
  1620. MethodInfo mi("range");
  1621. mi.return_val.type = Variant::ARRAY;
  1622. mi.flags |= METHOD_FLAG_VARARG;
  1623. return mi;
  1624. } break;
  1625. case RESOURCE_LOAD: {
  1626. MethodInfo mi("load", PropertyInfo(Variant::STRING, "path"));
  1627. mi.return_val.type = Variant::OBJECT;
  1628. mi.return_val.class_name = "Resource";
  1629. return mi;
  1630. } break;
  1631. case INST2DICT: {
  1632. MethodInfo mi("inst2dict", PropertyInfo(Variant::OBJECT, "inst"));
  1633. mi.return_val.type = Variant::DICTIONARY;
  1634. return mi;
  1635. } break;
  1636. case DICT2INST: {
  1637. MethodInfo mi("dict2inst", PropertyInfo(Variant::DICTIONARY, "dict"));
  1638. mi.return_val.type = Variant::OBJECT;
  1639. return mi;
  1640. } break;
  1641. case VALIDATE_JSON: {
  1642. MethodInfo mi("validate_json", PropertyInfo(Variant::STRING, "json"));
  1643. mi.return_val.type = Variant::STRING;
  1644. return mi;
  1645. } break;
  1646. case PARSE_JSON: {
  1647. MethodInfo mi(Variant::NIL, "parse_json", PropertyInfo(Variant::STRING, "json"));
  1648. mi.return_val.type = Variant::NIL;
  1649. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1650. return mi;
  1651. } break;
  1652. case TO_JSON: {
  1653. MethodInfo mi("to_json", PropertyInfo(Variant::NIL, "var", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1654. mi.return_val.type = Variant::STRING;
  1655. return mi;
  1656. } break;
  1657. case HASH: {
  1658. MethodInfo mi("hash", PropertyInfo(Variant::NIL, "var", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1659. mi.return_val.type = Variant::INT;
  1660. return mi;
  1661. } break;
  1662. case COLOR8: {
  1663. MethodInfo mi("Color8", PropertyInfo(Variant::INT, "r8"), PropertyInfo(Variant::INT, "g8"), PropertyInfo(Variant::INT, "b8"), PropertyInfo(Variant::INT, "a8"));
  1664. mi.default_arguments.push_back(255);
  1665. mi.return_val.type = Variant::COLOR;
  1666. return mi;
  1667. } break;
  1668. case COLORN: {
  1669. MethodInfo mi("ColorN", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::REAL, "alpha"));
  1670. mi.default_arguments.push_back(1.0f);
  1671. mi.return_val.type = Variant::COLOR;
  1672. return mi;
  1673. } break;
  1674. case PRINT_STACK: {
  1675. MethodInfo mi("print_stack");
  1676. mi.return_val.type = Variant::NIL;
  1677. return mi;
  1678. } break;
  1679. case GET_STACK: {
  1680. MethodInfo mi("get_stack");
  1681. mi.return_val.type = Variant::ARRAY;
  1682. return mi;
  1683. } break;
  1684. case INSTANCE_FROM_ID: {
  1685. MethodInfo mi("instance_from_id", PropertyInfo(Variant::INT, "instance_id"));
  1686. mi.return_val.type = Variant::OBJECT;
  1687. return mi;
  1688. } break;
  1689. case LEN: {
  1690. MethodInfo mi("len", PropertyInfo(Variant::NIL, "var", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1691. mi.return_val.type = Variant::INT;
  1692. return mi;
  1693. } break;
  1694. case IS_INSTANCE_VALID: {
  1695. MethodInfo mi("is_instance_valid", PropertyInfo(Variant::OBJECT, "instance"));
  1696. mi.return_val.type = Variant::BOOL;
  1697. return mi;
  1698. } break;
  1699. case FUNC_MAX: {
  1700. ERR_FAIL_V(MethodInfo());
  1701. } break;
  1702. }
  1703. #endif
  1704. return MethodInfo();
  1705. }