gd_functions.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*************************************************************************/
  2. /* gd_functions.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gd_functions.h"
  31. #include "func_ref.h"
  32. #include "gd_script.h"
  33. #include "io/marshalls.h"
  34. #include "math_funcs.h"
  35. #include "object_type_db.h"
  36. #include "os/os.h"
  37. #include "reference.h"
  38. #include "variant_parser.h"
  39. const char *GDFunctions::get_func_name(Function p_func) {
  40. ERR_FAIL_INDEX_V(p_func, FUNC_MAX, "");
  41. static const char *_names[FUNC_MAX] = {
  42. "sin",
  43. "cos",
  44. "tan",
  45. "sinh",
  46. "cosh",
  47. "tanh",
  48. "asin",
  49. "acos",
  50. "atan",
  51. "atan2",
  52. "sqrt",
  53. "fmod",
  54. "fposmod",
  55. "floor",
  56. "ceil",
  57. "round",
  58. "abs",
  59. "sign",
  60. "pow",
  61. "log",
  62. "exp",
  63. "is_nan",
  64. "is_inf",
  65. "ease",
  66. "decimals",
  67. "stepify",
  68. "lerp",
  69. "dectime",
  70. "randomize",
  71. "randi",
  72. "randf",
  73. "rand_range",
  74. "seed",
  75. "rand_seed",
  76. "deg2rad",
  77. "rad2deg",
  78. "linear2db",
  79. "db2linear",
  80. "max",
  81. "min",
  82. "clamp",
  83. "nearest_po2",
  84. "weakref",
  85. "funcref",
  86. "convert",
  87. "typeof",
  88. "type_exists",
  89. "str",
  90. "print",
  91. "printt",
  92. "prints",
  93. "printerr",
  94. "printraw",
  95. "var2str",
  96. "str2var",
  97. "var2bytes",
  98. "bytes2var",
  99. "range",
  100. "load",
  101. "inst2dict",
  102. "dict2inst",
  103. "hash",
  104. "Color8",
  105. "ColorN",
  106. "print_stack",
  107. "instance_from_id",
  108. };
  109. return _names[p_func];
  110. }
  111. void GDFunctions::call(Function p_func, const Variant **p_args, int p_arg_count, Variant &r_ret, Variant::CallError &r_error) {
  112. r_error.error = Variant::CallError::CALL_OK;
  113. #ifdef DEBUG_ENABLED
  114. #define VALIDATE_ARG_COUNT(m_count) \
  115. if (p_arg_count < m_count) { \
  116. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \
  117. r_error.argument = m_count; \
  118. r_ret = Variant(); \
  119. return; \
  120. } \
  121. if (p_arg_count > m_count) { \
  122. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \
  123. r_error.argument = m_count; \
  124. r_ret = Variant(); \
  125. return; \
  126. }
  127. #define VALIDATE_ARG_NUM(m_arg) \
  128. if (!p_args[m_arg]->is_num()) { \
  129. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  130. r_error.argument = m_arg; \
  131. r_error.expected = Variant::REAL; \
  132. r_ret = Variant(); \
  133. return; \
  134. }
  135. #else
  136. #define VALIDATE_ARG_COUNT(m_count)
  137. #define VALIDATE_ARG_NUM(m_arg)
  138. #endif
  139. //using a switch, so the compiler generates a jumptable
  140. switch (p_func) {
  141. case MATH_SIN: {
  142. VALIDATE_ARG_COUNT(1);
  143. VALIDATE_ARG_NUM(0);
  144. r_ret = Math::sin(*p_args[0]);
  145. } break;
  146. case MATH_COS: {
  147. VALIDATE_ARG_COUNT(1);
  148. VALIDATE_ARG_NUM(0);
  149. r_ret = Math::cos(*p_args[0]);
  150. } break;
  151. case MATH_TAN: {
  152. VALIDATE_ARG_COUNT(1);
  153. VALIDATE_ARG_NUM(0);
  154. r_ret = Math::tan(*p_args[0]);
  155. } break;
  156. case MATH_SINH: {
  157. VALIDATE_ARG_COUNT(1);
  158. VALIDATE_ARG_NUM(0);
  159. r_ret = Math::sinh(*p_args[0]);
  160. } break;
  161. case MATH_COSH: {
  162. VALIDATE_ARG_COUNT(1);
  163. VALIDATE_ARG_NUM(0);
  164. r_ret = Math::cosh(*p_args[0]);
  165. } break;
  166. case MATH_TANH: {
  167. VALIDATE_ARG_COUNT(1);
  168. VALIDATE_ARG_NUM(0);
  169. r_ret = Math::tanh(*p_args[0]);
  170. } break;
  171. case MATH_ASIN: {
  172. VALIDATE_ARG_COUNT(1);
  173. VALIDATE_ARG_NUM(0);
  174. r_ret = Math::asin(*p_args[0]);
  175. } break;
  176. case MATH_ACOS: {
  177. VALIDATE_ARG_COUNT(1);
  178. VALIDATE_ARG_NUM(0);
  179. r_ret = Math::acos(*p_args[0]);
  180. } break;
  181. case MATH_ATAN: {
  182. VALIDATE_ARG_COUNT(1);
  183. VALIDATE_ARG_NUM(0);
  184. r_ret = Math::atan(*p_args[0]);
  185. } break;
  186. case MATH_ATAN2: {
  187. VALIDATE_ARG_COUNT(2);
  188. VALIDATE_ARG_NUM(0);
  189. VALIDATE_ARG_NUM(1);
  190. r_ret = Math::atan2(*p_args[0], *p_args[1]);
  191. } break;
  192. case MATH_SQRT: {
  193. VALIDATE_ARG_COUNT(1);
  194. VALIDATE_ARG_NUM(0);
  195. r_ret = Math::sqrt(*p_args[0]);
  196. } break;
  197. case MATH_FMOD: {
  198. VALIDATE_ARG_COUNT(2);
  199. VALIDATE_ARG_NUM(0);
  200. VALIDATE_ARG_NUM(1);
  201. r_ret = Math::fmod(*p_args[0], *p_args[1]);
  202. } break;
  203. case MATH_FPOSMOD: {
  204. VALIDATE_ARG_COUNT(2);
  205. VALIDATE_ARG_NUM(0);
  206. VALIDATE_ARG_NUM(1);
  207. r_ret = Math::fposmod(*p_args[0], *p_args[1]);
  208. } break;
  209. case MATH_FLOOR: {
  210. VALIDATE_ARG_COUNT(1);
  211. VALIDATE_ARG_NUM(0);
  212. r_ret = Math::floor(*p_args[0]);
  213. } break;
  214. case MATH_CEIL: {
  215. VALIDATE_ARG_COUNT(1);
  216. VALIDATE_ARG_NUM(0);
  217. r_ret = Math::ceil(*p_args[0]);
  218. } break;
  219. case MATH_ROUND: {
  220. VALIDATE_ARG_COUNT(1);
  221. VALIDATE_ARG_NUM(0);
  222. r_ret = Math::round(*p_args[0]);
  223. } break;
  224. case MATH_ABS: {
  225. VALIDATE_ARG_COUNT(1);
  226. if (p_args[0]->get_type() == Variant::INT) {
  227. int64_t i = *p_args[0];
  228. r_ret = ABS(i);
  229. } else if (p_args[0]->get_type() == Variant::REAL) {
  230. real_t r = *p_args[0];
  231. r_ret = Math::abs(r);
  232. } else {
  233. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  234. r_error.argument = 0;
  235. r_error.expected = Variant::REAL;
  236. r_ret = Variant();
  237. }
  238. } break;
  239. case MATH_SIGN: {
  240. VALIDATE_ARG_COUNT(1);
  241. if (p_args[0]->get_type() == Variant::INT) {
  242. int64_t i = *p_args[0];
  243. r_ret = i < 0 ? -1 : (i > 0 ? +1 : 0);
  244. } else if (p_args[0]->get_type() == Variant::REAL) {
  245. real_t r = *p_args[0];
  246. r_ret = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0);
  247. } else {
  248. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  249. r_error.argument = 0;
  250. r_error.expected = Variant::REAL;
  251. r_ret = Variant();
  252. }
  253. } break;
  254. case MATH_POW: {
  255. VALIDATE_ARG_COUNT(2);
  256. VALIDATE_ARG_NUM(0);
  257. VALIDATE_ARG_NUM(1);
  258. r_ret = Math::pow(*p_args[0], *p_args[1]);
  259. } break;
  260. case MATH_LOG: {
  261. VALIDATE_ARG_COUNT(1);
  262. VALIDATE_ARG_NUM(0);
  263. r_ret = Math::log(*p_args[0]);
  264. } break;
  265. case MATH_EXP: {
  266. VALIDATE_ARG_COUNT(1);
  267. VALIDATE_ARG_NUM(0);
  268. r_ret = Math::exp(*p_args[0]);
  269. } break;
  270. case MATH_ISNAN: {
  271. VALIDATE_ARG_COUNT(1);
  272. VALIDATE_ARG_NUM(0);
  273. r_ret = Math::is_nan(*p_args[0]);
  274. } break;
  275. case MATH_ISINF: {
  276. VALIDATE_ARG_COUNT(1);
  277. VALIDATE_ARG_NUM(0);
  278. r_ret = Math::is_inf(*p_args[0]);
  279. } break;
  280. case MATH_EASE: {
  281. VALIDATE_ARG_COUNT(2);
  282. VALIDATE_ARG_NUM(0);
  283. VALIDATE_ARG_NUM(1);
  284. r_ret = Math::ease(*p_args[0], *p_args[1]);
  285. } break;
  286. case MATH_DECIMALS: {
  287. VALIDATE_ARG_COUNT(1);
  288. VALIDATE_ARG_NUM(0);
  289. r_ret = Math::step_decimals(*p_args[0]);
  290. } break;
  291. case MATH_STEPIFY: {
  292. VALIDATE_ARG_COUNT(2);
  293. VALIDATE_ARG_NUM(0);
  294. VALIDATE_ARG_NUM(1);
  295. r_ret = Math::stepify(*p_args[0], *p_args[1]);
  296. } break;
  297. case MATH_LERP: {
  298. VALIDATE_ARG_COUNT(3);
  299. VALIDATE_ARG_NUM(0);
  300. VALIDATE_ARG_NUM(1);
  301. VALIDATE_ARG_NUM(2);
  302. r_ret = Math::lerp(*p_args[0], *p_args[1], *p_args[2]);
  303. } break;
  304. case MATH_DECTIME: {
  305. VALIDATE_ARG_COUNT(3);
  306. VALIDATE_ARG_NUM(0);
  307. VALIDATE_ARG_NUM(1);
  308. VALIDATE_ARG_NUM(2);
  309. r_ret = Math::dectime(*p_args[0], *p_args[1], *p_args[2]);
  310. } break;
  311. case MATH_RANDOMIZE: {
  312. Math::randomize();
  313. r_ret = Variant();
  314. } break;
  315. case MATH_RAND: {
  316. r_ret = Math::rand();
  317. } break;
  318. case MATH_RANDF: {
  319. r_ret = Math::randf();
  320. } break;
  321. case MATH_RANDOM: {
  322. VALIDATE_ARG_COUNT(2);
  323. VALIDATE_ARG_NUM(0);
  324. VALIDATE_ARG_NUM(1);
  325. r_ret = Math::random(*p_args[0], *p_args[1]);
  326. } break;
  327. case MATH_SEED: {
  328. VALIDATE_ARG_COUNT(1);
  329. VALIDATE_ARG_NUM(0);
  330. uint32_t seed = *p_args[0];
  331. Math::seed(seed);
  332. r_ret = Variant();
  333. } break;
  334. case MATH_RANDSEED: {
  335. VALIDATE_ARG_COUNT(1);
  336. VALIDATE_ARG_NUM(0);
  337. uint32_t seed = *p_args[0];
  338. int ret = Math::rand_from_seed(&seed);
  339. Array reta;
  340. reta.push_back(ret);
  341. reta.push_back(seed);
  342. r_ret = reta;
  343. } break;
  344. case MATH_DEG2RAD: {
  345. VALIDATE_ARG_COUNT(1);
  346. VALIDATE_ARG_NUM(0);
  347. r_ret = Math::deg2rad(*p_args[0]);
  348. } break;
  349. case MATH_RAD2DEG: {
  350. VALIDATE_ARG_COUNT(1);
  351. VALIDATE_ARG_NUM(0);
  352. r_ret = Math::rad2deg(*p_args[0]);
  353. } break;
  354. case MATH_LINEAR2DB: {
  355. VALIDATE_ARG_COUNT(1);
  356. VALIDATE_ARG_NUM(0);
  357. r_ret = Math::linear2db(*p_args[0]);
  358. } break;
  359. case MATH_DB2LINEAR: {
  360. VALIDATE_ARG_COUNT(1);
  361. VALIDATE_ARG_NUM(0);
  362. r_ret = Math::db2linear(*p_args[0]);
  363. } break;
  364. case LOGIC_MAX: {
  365. VALIDATE_ARG_COUNT(2);
  366. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT) {
  367. int64_t a = *p_args[0];
  368. int64_t b = *p_args[1];
  369. r_ret = MAX(a, b);
  370. } else {
  371. VALIDATE_ARG_NUM(0);
  372. VALIDATE_ARG_NUM(1);
  373. real_t a = *p_args[0];
  374. real_t b = *p_args[1];
  375. r_ret = MAX(a, b);
  376. }
  377. } break;
  378. case LOGIC_MIN: {
  379. VALIDATE_ARG_COUNT(2);
  380. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT) {
  381. int64_t a = *p_args[0];
  382. int64_t b = *p_args[1];
  383. r_ret = MIN(a, b);
  384. } else {
  385. VALIDATE_ARG_NUM(0);
  386. VALIDATE_ARG_NUM(1);
  387. real_t a = *p_args[0];
  388. real_t b = *p_args[1];
  389. r_ret = MIN(a, b);
  390. }
  391. } break;
  392. case LOGIC_CLAMP: {
  393. VALIDATE_ARG_COUNT(3);
  394. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT && p_args[2]->get_type() == Variant::INT) {
  395. int64_t a = *p_args[0];
  396. int64_t b = *p_args[1];
  397. int64_t c = *p_args[2];
  398. r_ret = CLAMP(a, b, c);
  399. } else {
  400. VALIDATE_ARG_NUM(0);
  401. VALIDATE_ARG_NUM(1);
  402. VALIDATE_ARG_NUM(2);
  403. real_t a = *p_args[0];
  404. real_t b = *p_args[1];
  405. real_t c = *p_args[2];
  406. r_ret = CLAMP(a, b, c);
  407. }
  408. } break;
  409. case LOGIC_NEAREST_PO2: {
  410. VALIDATE_ARG_COUNT(1);
  411. VALIDATE_ARG_NUM(0);
  412. int64_t num = *p_args[0];
  413. r_ret = next_power_of_2(num);
  414. } break;
  415. case OBJ_WEAKREF: {
  416. VALIDATE_ARG_COUNT(1);
  417. if (p_args[0]->get_type() != Variant::OBJECT) {
  418. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  419. r_error.argument = 0;
  420. r_error.expected = Variant::OBJECT;
  421. r_ret = Variant();
  422. return;
  423. }
  424. if (p_args[0]->is_ref()) {
  425. REF r = *p_args[0];
  426. if (!r.is_valid()) {
  427. r_ret = Variant();
  428. return;
  429. }
  430. Ref<WeakRef> wref = memnew(WeakRef);
  431. wref->set_ref(r);
  432. r_ret = wref;
  433. } else {
  434. Object *obj = *p_args[0];
  435. if (!obj) {
  436. r_ret = Variant();
  437. return;
  438. }
  439. Ref<WeakRef> wref = memnew(WeakRef);
  440. wref->set_obj(obj);
  441. r_ret = wref;
  442. }
  443. } break;
  444. case FUNC_FUNCREF: {
  445. VALIDATE_ARG_COUNT(2);
  446. if (p_args[0]->get_type() != Variant::OBJECT) {
  447. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  448. r_error.argument = 0;
  449. r_error.expected = Variant::OBJECT;
  450. r_ret = Variant();
  451. return;
  452. }
  453. if (p_args[1]->get_type() != Variant::STRING && p_args[1]->get_type() != Variant::NODE_PATH) {
  454. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  455. r_error.argument = 1;
  456. r_error.expected = Variant::STRING;
  457. r_ret = Variant();
  458. return;
  459. }
  460. Ref<FuncRef> fr = memnew(FuncRef);
  461. fr->set_instance(*p_args[0]);
  462. fr->set_function(*p_args[1]);
  463. r_ret = fr;
  464. } break;
  465. case TYPE_CONVERT: {
  466. VALIDATE_ARG_COUNT(2);
  467. VALIDATE_ARG_NUM(1);
  468. int type = *p_args[1];
  469. if (type < 0 || type >= Variant::VARIANT_MAX) {
  470. r_ret = RTR("Invalid type argument to convert(), use TYPE_* constants.");
  471. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  472. r_error.argument = 0;
  473. r_error.expected = Variant::INT;
  474. return;
  475. } else {
  476. r_ret = Variant::construct(Variant::Type(type), p_args, 1, r_error);
  477. }
  478. } break;
  479. case TYPE_OF: {
  480. VALIDATE_ARG_COUNT(1);
  481. r_ret = p_args[0]->get_type();
  482. } break;
  483. case TYPE_EXISTS: {
  484. VALIDATE_ARG_COUNT(1);
  485. r_ret = ObjectTypeDB::type_exists(*p_args[0]);
  486. } break;
  487. case TEXT_STR: {
  488. String str;
  489. for (int i = 0; i < p_arg_count; i++) {
  490. String os = p_args[i]->operator String();
  491. if (i == 0)
  492. str = os;
  493. else
  494. str += os;
  495. }
  496. r_ret = str;
  497. } break;
  498. case TEXT_PRINT: {
  499. String str;
  500. for (int i = 0; i < p_arg_count; i++) {
  501. str += p_args[i]->operator String();
  502. }
  503. //str+="\n";
  504. print_line(str);
  505. r_ret = Variant();
  506. } break;
  507. case TEXT_PRINT_TABBED: {
  508. String str;
  509. for (int i = 0; i < p_arg_count; i++) {
  510. if (i)
  511. str += "\t";
  512. str += p_args[i]->operator String();
  513. }
  514. //str+="\n";
  515. print_line(str);
  516. r_ret = Variant();
  517. } break;
  518. case TEXT_PRINT_SPACED: {
  519. String str;
  520. for (int i = 0; i < p_arg_count; i++) {
  521. if (i)
  522. str += " ";
  523. str += p_args[i]->operator String();
  524. }
  525. //str+="\n";
  526. print_line(str);
  527. r_ret = Variant();
  528. } break;
  529. case TEXT_PRINTERR: {
  530. String str;
  531. for (int i = 0; i < p_arg_count; i++) {
  532. str += p_args[i]->operator String();
  533. }
  534. //str+="\n";
  535. OS::get_singleton()->printerr("%s\n", str.utf8().get_data());
  536. r_ret = Variant();
  537. } break;
  538. case TEXT_PRINTRAW: {
  539. String str;
  540. for (int i = 0; i < p_arg_count; i++) {
  541. str += p_args[i]->operator String();
  542. }
  543. //str+="\n";
  544. OS::get_singleton()->print("%s", str.utf8().get_data());
  545. r_ret = Variant();
  546. } break;
  547. case VAR_TO_STR: {
  548. VALIDATE_ARG_COUNT(1);
  549. String vars;
  550. VariantWriter::write_to_string(*p_args[0], vars);
  551. r_ret = vars;
  552. } break;
  553. case STR_TO_VAR: {
  554. VALIDATE_ARG_COUNT(1);
  555. if (p_args[0]->get_type() != Variant::STRING) {
  556. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  557. r_error.argument = 0;
  558. r_error.expected = Variant::STRING;
  559. r_ret = Variant();
  560. return;
  561. }
  562. VariantParser::StreamString ss;
  563. ss.s = *p_args[0];
  564. String errs;
  565. int line;
  566. Error err = VariantParser::parse(&ss, r_ret, errs, line);
  567. if (err != OK) {
  568. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  569. r_error.argument = 0;
  570. r_error.expected = Variant::STRING;
  571. r_ret = "Parse error at line " + itos(line) + ": " + errs;
  572. return;
  573. }
  574. } break;
  575. case VAR_TO_BYTES: {
  576. VALIDATE_ARG_COUNT(1);
  577. ByteArray barr;
  578. int len;
  579. Error err = encode_variant(*p_args[0], NULL, len);
  580. if (err) {
  581. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  582. r_error.argument = 0;
  583. r_error.expected = Variant::NIL;
  584. r_ret = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
  585. return;
  586. }
  587. barr.resize(len);
  588. {
  589. ByteArray::Write w = barr.write();
  590. encode_variant(*p_args[0], w.ptr(), len);
  591. }
  592. r_ret = barr;
  593. } break;
  594. case BYTES_TO_VAR: {
  595. VALIDATE_ARG_COUNT(1);
  596. if (p_args[0]->get_type() != Variant::RAW_ARRAY) {
  597. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  598. r_error.argument = 0;
  599. r_error.expected = Variant::RAW_ARRAY;
  600. r_ret = Variant();
  601. return;
  602. }
  603. ByteArray varr = *p_args[0];
  604. Variant ret;
  605. {
  606. ByteArray::Read r = varr.read();
  607. Error err = decode_variant(ret, r.ptr(), varr.size(), NULL);
  608. if (err != OK) {
  609. r_ret = RTR("Not enough bytes for decoding bytes, or invalid format.");
  610. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  611. r_error.argument = 0;
  612. r_error.expected = Variant::RAW_ARRAY;
  613. return;
  614. }
  615. }
  616. r_ret = ret;
  617. } break;
  618. case GEN_RANGE: {
  619. switch (p_arg_count) {
  620. case 0: {
  621. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  622. r_error.argument = 1;
  623. r_ret = Variant();
  624. } break;
  625. case 1: {
  626. VALIDATE_ARG_NUM(0);
  627. int count = *p_args[0];
  628. Array arr(true);
  629. if (count <= 0) {
  630. r_ret = arr;
  631. return;
  632. }
  633. Error err = arr.resize(count);
  634. if (err != OK) {
  635. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  636. r_ret = Variant();
  637. return;
  638. }
  639. for (int i = 0; i < count; i++) {
  640. arr[i] = i;
  641. }
  642. r_ret = arr;
  643. } break;
  644. case 2: {
  645. VALIDATE_ARG_NUM(0);
  646. VALIDATE_ARG_NUM(1);
  647. int from = *p_args[0];
  648. int to = *p_args[1];
  649. Array arr(true);
  650. if (from >= to) {
  651. r_ret = arr;
  652. return;
  653. }
  654. Error err = arr.resize(to - from);
  655. if (err != OK) {
  656. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  657. r_ret = Variant();
  658. return;
  659. }
  660. for (int i = from; i < to; i++)
  661. arr[i - from] = i;
  662. r_ret = arr;
  663. } break;
  664. case 3: {
  665. VALIDATE_ARG_NUM(0);
  666. VALIDATE_ARG_NUM(1);
  667. VALIDATE_ARG_NUM(2);
  668. int from = *p_args[0];
  669. int to = *p_args[1];
  670. int incr = *p_args[2];
  671. if (incr == 0) {
  672. r_ret = RTR("step argument is zero!");
  673. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  674. return;
  675. }
  676. Array arr(true);
  677. if (from >= to && incr > 0) {
  678. r_ret = arr;
  679. return;
  680. }
  681. if (from <= to && incr < 0) {
  682. r_ret = arr;
  683. return;
  684. }
  685. //calculate how many
  686. int count = 0;
  687. if (incr > 0) {
  688. count = ((to - from - 1) / incr) + 1;
  689. } else {
  690. count = ((from - to - 1) / -incr) + 1;
  691. }
  692. Error err = arr.resize(count);
  693. if (err != OK) {
  694. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  695. r_ret = Variant();
  696. return;
  697. }
  698. if (incr > 0) {
  699. int idx = 0;
  700. for (int i = from; i < to; i += incr) {
  701. arr[idx++] = i;
  702. }
  703. } else {
  704. int idx = 0;
  705. for (int i = from; i > to; i += incr) {
  706. arr[idx++] = i;
  707. }
  708. }
  709. r_ret = arr;
  710. } break;
  711. default: {
  712. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  713. r_error.argument = 3;
  714. r_ret = Variant();
  715. } break;
  716. }
  717. } break;
  718. case RESOURCE_LOAD: {
  719. VALIDATE_ARG_COUNT(1);
  720. if (p_args[0]->get_type() != Variant::STRING) {
  721. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  722. r_error.argument = 0;
  723. r_ret = Variant();
  724. } else {
  725. r_ret = ResourceLoader::load(*p_args[0]);
  726. }
  727. } break;
  728. case INST2DICT: {
  729. VALIDATE_ARG_COUNT(1);
  730. if (p_args[0]->get_type() == Variant::NIL) {
  731. r_ret = Variant();
  732. } else if (p_args[0]->get_type() != Variant::OBJECT) {
  733. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  734. r_error.argument = 0;
  735. r_ret = Variant();
  736. } else {
  737. Object *obj = *p_args[0];
  738. if (!obj) {
  739. r_ret = Variant();
  740. } else if (!obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton()) {
  741. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  742. r_error.argument = 0;
  743. r_error.expected = Variant::DICTIONARY;
  744. r_ret = RTR("Not a script with an instance");
  745. return;
  746. } else {
  747. GDInstance *ins = static_cast<GDInstance *>(obj->get_script_instance());
  748. Ref<GDScript> base = ins->get_script();
  749. if (base.is_null()) {
  750. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  751. r_error.argument = 0;
  752. r_error.expected = Variant::DICTIONARY;
  753. r_ret = RTR("Not based on a script");
  754. return;
  755. }
  756. GDScript *p = base.ptr();
  757. Vector<StringName> sname;
  758. while (p->_owner) {
  759. sname.push_back(p->name);
  760. p = p->_owner;
  761. }
  762. sname.invert();
  763. if (!p->path.is_resource_file()) {
  764. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  765. r_error.argument = 0;
  766. r_error.expected = Variant::DICTIONARY;
  767. r_ret = Variant();
  768. r_ret = RTR("Not based on a resource file");
  769. return;
  770. }
  771. NodePath cp(sname, Vector<StringName>(), false);
  772. Dictionary d(true);
  773. d["@subpath"] = cp;
  774. d["@path"] = p->path;
  775. p = base.ptr();
  776. while (p) {
  777. for (Set<StringName>::Element *E = p->members.front(); E; E = E->next()) {
  778. Variant value;
  779. if (ins->get(E->get(), value)) {
  780. String k = E->get();
  781. if (!d.has(k)) {
  782. d[k] = value;
  783. }
  784. }
  785. }
  786. p = p->_base;
  787. }
  788. r_ret = d;
  789. }
  790. }
  791. } break;
  792. case DICT2INST: {
  793. VALIDATE_ARG_COUNT(1);
  794. if (p_args[0]->get_type() != Variant::DICTIONARY) {
  795. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  796. r_error.argument = 0;
  797. r_error.expected = Variant::DICTIONARY;
  798. r_ret = Variant();
  799. return;
  800. }
  801. Dictionary d = *p_args[0];
  802. if (!d.has("@path")) {
  803. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  804. r_error.argument = 0;
  805. r_error.expected = Variant::OBJECT;
  806. r_ret = RTR("Invalid instance dictionary format (missing @path)");
  807. return;
  808. }
  809. Ref<Script> scr = ResourceLoader::load(d["@path"]);
  810. if (!scr.is_valid()) {
  811. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  812. r_error.argument = 0;
  813. r_error.expected = Variant::OBJECT;
  814. r_ret = RTR("Invalid instance dictionary format (can't load script at @path)");
  815. return;
  816. }
  817. Ref<GDScript> gdscr = scr;
  818. if (!gdscr.is_valid()) {
  819. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  820. r_error.argument = 0;
  821. r_error.expected = Variant::OBJECT;
  822. r_ret = Variant();
  823. r_ret = RTR("Invalid instance dictionary format (invalid script at @path)");
  824. return;
  825. }
  826. NodePath sub;
  827. if (d.has("@subpath")) {
  828. sub = d["@subpath"];
  829. }
  830. for (int i = 0; i < sub.get_name_count(); i++) {
  831. gdscr = gdscr->subclasses[sub.get_name(i)];
  832. if (!gdscr.is_valid()) {
  833. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  834. r_error.argument = 0;
  835. r_error.expected = Variant::OBJECT;
  836. r_ret = Variant();
  837. r_ret = RTR("Invalid instance dictionary (invalid subclasses)");
  838. return;
  839. }
  840. }
  841. r_ret = gdscr->_new(NULL, 0, r_error);
  842. GDInstance *ins = static_cast<GDInstance *>(static_cast<Object *>(r_ret)->get_script_instance());
  843. Ref<GDScript> gd_ref = ins->get_script();
  844. for (Map<StringName, GDScript::MemberInfo>::Element *E = gd_ref->member_indices.front(); E; E = E->next()) {
  845. if (d.has(E->key())) {
  846. ins->members[E->get().index] = d[E->key()];
  847. }
  848. }
  849. } break;
  850. case HASH: {
  851. VALIDATE_ARG_COUNT(1);
  852. r_ret = p_args[0]->hash();
  853. } break;
  854. case COLOR8: {
  855. if (p_arg_count < 3) {
  856. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  857. r_error.argument = 3;
  858. r_ret = Variant();
  859. return;
  860. }
  861. if (p_arg_count > 4) {
  862. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  863. r_error.argument = 4;
  864. r_ret = Variant();
  865. return;
  866. }
  867. VALIDATE_ARG_NUM(0);
  868. VALIDATE_ARG_NUM(1);
  869. VALIDATE_ARG_NUM(2);
  870. Color color((float)*p_args[0] / 255.0f, (float)*p_args[1] / 255.0f, (float)*p_args[2] / 255.0f);
  871. if (p_arg_count == 4) {
  872. VALIDATE_ARG_NUM(3);
  873. color.a = (float)*p_args[3] / 255.0f;
  874. }
  875. r_ret = color;
  876. } break;
  877. case COLORN: {
  878. if (p_arg_count < 1) {
  879. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  880. r_error.argument = 1;
  881. r_ret = Variant();
  882. return;
  883. }
  884. if (p_arg_count > 2) {
  885. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  886. r_error.argument = 2;
  887. r_ret = Variant();
  888. return;
  889. }
  890. if (p_args[0]->get_type() != Variant::STRING) {
  891. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  892. r_error.argument = 0;
  893. r_ret = Variant();
  894. } else {
  895. Color color = Color::named(*p_args[0]);
  896. if (p_arg_count == 2) {
  897. VALIDATE_ARG_NUM(1);
  898. color.a = *p_args[1];
  899. }
  900. r_ret = color;
  901. }
  902. } break;
  903. case PRINT_STACK: {
  904. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  905. for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
  906. 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) + "'");
  907. };
  908. } break;
  909. case INSTANCE_FROM_ID: {
  910. VALIDATE_ARG_COUNT(1);
  911. if (p_args[0]->get_type() != Variant::INT && p_args[0]->get_type() != Variant::REAL) {
  912. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  913. r_error.argument = 0;
  914. r_error.expected = Variant::INT;
  915. r_ret = Variant();
  916. break;
  917. }
  918. uint32_t id = *p_args[0];
  919. r_ret = ObjectDB::get_instance(id);
  920. } break;
  921. case FUNC_MAX: {
  922. ERR_FAIL();
  923. } break;
  924. }
  925. }
  926. bool GDFunctions::is_deterministic(Function p_func) {
  927. //man i couldn't have chosen a worse function name,
  928. //way too controversial..
  929. switch (p_func) {
  930. case MATH_SIN:
  931. case MATH_COS:
  932. case MATH_TAN:
  933. case MATH_SINH:
  934. case MATH_COSH:
  935. case MATH_TANH:
  936. case MATH_ASIN:
  937. case MATH_ACOS:
  938. case MATH_ATAN:
  939. case MATH_ATAN2:
  940. case MATH_SQRT:
  941. case MATH_FMOD:
  942. case MATH_FPOSMOD:
  943. case MATH_FLOOR:
  944. case MATH_CEIL:
  945. case MATH_ROUND:
  946. case MATH_ABS:
  947. case MATH_SIGN:
  948. case MATH_POW:
  949. case MATH_LOG:
  950. case MATH_EXP:
  951. case MATH_ISNAN:
  952. case MATH_ISINF:
  953. case MATH_EASE:
  954. case MATH_DECIMALS:
  955. case MATH_STEPIFY:
  956. case MATH_LERP:
  957. case MATH_DECTIME:
  958. case MATH_DEG2RAD:
  959. case MATH_RAD2DEG:
  960. case MATH_LINEAR2DB:
  961. case MATH_DB2LINEAR:
  962. case LOGIC_MAX:
  963. case LOGIC_MIN:
  964. case LOGIC_CLAMP:
  965. case LOGIC_NEAREST_PO2:
  966. case TYPE_CONVERT:
  967. case TYPE_OF:
  968. case TYPE_EXISTS:
  969. case TEXT_STR:
  970. case COLOR8:
  971. // enable for debug only, otherwise not desirable - case GEN_RANGE:
  972. return true;
  973. default:
  974. return false;
  975. }
  976. return false;
  977. }
  978. MethodInfo GDFunctions::get_info(Function p_func) {
  979. #ifdef TOOLS_ENABLED
  980. //using a switch, so the compiler generates a jumptable
  981. switch (p_func) {
  982. case MATH_SIN: {
  983. MethodInfo mi("sin", PropertyInfo(Variant::REAL, "s"));
  984. mi.return_val.type = Variant::REAL;
  985. return mi;
  986. } break;
  987. case MATH_COS: {
  988. MethodInfo mi("cos", PropertyInfo(Variant::REAL, "s"));
  989. mi.return_val.type = Variant::REAL;
  990. return mi;
  991. } break;
  992. case MATH_TAN: {
  993. MethodInfo mi("tan", PropertyInfo(Variant::REAL, "s"));
  994. mi.return_val.type = Variant::REAL;
  995. return mi;
  996. } break;
  997. case MATH_SINH: {
  998. MethodInfo mi("sinh", PropertyInfo(Variant::REAL, "s"));
  999. mi.return_val.type = Variant::REAL;
  1000. return mi;
  1001. } break;
  1002. case MATH_COSH: {
  1003. MethodInfo mi("cosh", PropertyInfo(Variant::REAL, "s"));
  1004. mi.return_val.type = Variant::REAL;
  1005. return mi;
  1006. } break;
  1007. case MATH_TANH: {
  1008. MethodInfo mi("tanh", PropertyInfo(Variant::REAL, "s"));
  1009. mi.return_val.type = Variant::REAL;
  1010. return mi;
  1011. } break;
  1012. case MATH_ASIN: {
  1013. MethodInfo mi("asin", PropertyInfo(Variant::REAL, "s"));
  1014. mi.return_val.type = Variant::REAL;
  1015. return mi;
  1016. } break;
  1017. case MATH_ACOS: {
  1018. MethodInfo mi("acos", PropertyInfo(Variant::REAL, "s"));
  1019. mi.return_val.type = Variant::REAL;
  1020. return mi;
  1021. } break;
  1022. case MATH_ATAN: {
  1023. MethodInfo mi("atan", PropertyInfo(Variant::REAL, "s"));
  1024. mi.return_val.type = Variant::REAL;
  1025. return mi;
  1026. } break;
  1027. case MATH_ATAN2: {
  1028. MethodInfo mi("atan2", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1029. mi.return_val.type = Variant::REAL;
  1030. return mi;
  1031. } break;
  1032. case MATH_SQRT: {
  1033. MethodInfo mi("sqrt", PropertyInfo(Variant::REAL, "s"));
  1034. mi.return_val.type = Variant::REAL;
  1035. return mi;
  1036. } break;
  1037. case MATH_FMOD: {
  1038. MethodInfo mi("fmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1039. mi.return_val.type = Variant::REAL;
  1040. return mi;
  1041. } break;
  1042. case MATH_FPOSMOD: {
  1043. MethodInfo mi("fposmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1044. mi.return_val.type = Variant::REAL;
  1045. return mi;
  1046. } break;
  1047. case MATH_FLOOR: {
  1048. MethodInfo mi("floor", PropertyInfo(Variant::REAL, "s"));
  1049. mi.return_val.type = Variant::REAL;
  1050. return mi;
  1051. } break;
  1052. case MATH_CEIL: {
  1053. MethodInfo mi("ceil", PropertyInfo(Variant::REAL, "s"));
  1054. mi.return_val.type = Variant::REAL;
  1055. return mi;
  1056. } break;
  1057. case MATH_ROUND: {
  1058. MethodInfo mi("round", PropertyInfo(Variant::REAL, "s"));
  1059. mi.return_val.type = Variant::REAL;
  1060. return mi;
  1061. } break;
  1062. case MATH_ABS: {
  1063. MethodInfo mi("abs", PropertyInfo(Variant::REAL, "s"));
  1064. mi.return_val.type = Variant::REAL;
  1065. return mi;
  1066. } break;
  1067. case MATH_SIGN: {
  1068. MethodInfo mi("sign", PropertyInfo(Variant::REAL, "s"));
  1069. mi.return_val.type = Variant::REAL;
  1070. return mi;
  1071. } break;
  1072. case MATH_POW: {
  1073. MethodInfo mi("pow", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
  1074. mi.return_val.type = Variant::REAL;
  1075. return mi;
  1076. } break;
  1077. case MATH_LOG: {
  1078. MethodInfo mi("log", PropertyInfo(Variant::REAL, "s"));
  1079. mi.return_val.type = Variant::REAL;
  1080. return mi;
  1081. } break;
  1082. case MATH_EXP: {
  1083. MethodInfo mi("exp", PropertyInfo(Variant::REAL, "s"));
  1084. mi.return_val.type = Variant::REAL;
  1085. return mi;
  1086. } break;
  1087. case MATH_ISNAN: {
  1088. MethodInfo mi("is_nan", PropertyInfo(Variant::REAL, "s"));
  1089. mi.return_val.type = Variant::REAL;
  1090. return mi;
  1091. } break;
  1092. case MATH_ISINF: {
  1093. MethodInfo mi("is_inf", PropertyInfo(Variant::REAL, "s"));
  1094. mi.return_val.type = Variant::REAL;
  1095. return mi;
  1096. } break;
  1097. case MATH_EASE: {
  1098. MethodInfo mi("ease", PropertyInfo(Variant::REAL, "s"), PropertyInfo(Variant::REAL, "curve"));
  1099. mi.return_val.type = Variant::REAL;
  1100. return mi;
  1101. } break;
  1102. case MATH_DECIMALS: {
  1103. MethodInfo mi("decimals", PropertyInfo(Variant::REAL, "step"));
  1104. mi.return_val.type = Variant::REAL;
  1105. return mi;
  1106. } break;
  1107. case MATH_STEPIFY: {
  1108. MethodInfo mi("stepify", PropertyInfo(Variant::REAL, "s"), PropertyInfo(Variant::REAL, "step"));
  1109. mi.return_val.type = Variant::REAL;
  1110. return mi;
  1111. } break;
  1112. case MATH_LERP: {
  1113. MethodInfo mi("lerp", PropertyInfo(Variant::REAL, "from"), PropertyInfo(Variant::REAL, "to"), PropertyInfo(Variant::REAL, "weight"));
  1114. mi.return_val.type = Variant::REAL;
  1115. return mi;
  1116. } break;
  1117. case MATH_DECTIME: {
  1118. MethodInfo mi("dectime", PropertyInfo(Variant::REAL, "value"), PropertyInfo(Variant::REAL, "amount"), PropertyInfo(Variant::REAL, "step"));
  1119. mi.return_val.type = Variant::REAL;
  1120. return mi;
  1121. } break;
  1122. case MATH_RANDOMIZE: {
  1123. MethodInfo mi("randomize");
  1124. mi.return_val.type = Variant::NIL;
  1125. return mi;
  1126. } break;
  1127. case MATH_RAND: {
  1128. MethodInfo mi("randi");
  1129. mi.return_val.type = Variant::INT;
  1130. return mi;
  1131. } break;
  1132. case MATH_RANDF: {
  1133. MethodInfo mi("randf");
  1134. mi.return_val.type = Variant::REAL;
  1135. return mi;
  1136. } break;
  1137. case MATH_RANDOM: {
  1138. MethodInfo mi("rand_range", PropertyInfo(Variant::REAL, "from"), PropertyInfo(Variant::REAL, "to"));
  1139. mi.return_val.type = Variant::REAL;
  1140. return mi;
  1141. } break;
  1142. case MATH_SEED: {
  1143. MethodInfo mi("seed", PropertyInfo(Variant::INT, "seed"));
  1144. mi.return_val.type = Variant::NIL;
  1145. return mi;
  1146. } break;
  1147. case MATH_RANDSEED: {
  1148. MethodInfo mi("rand_seed", PropertyInfo(Variant::INT, "seed"));
  1149. mi.return_val.type = Variant::ARRAY;
  1150. return mi;
  1151. } break;
  1152. case MATH_DEG2RAD: {
  1153. MethodInfo mi("deg2rad", PropertyInfo(Variant::REAL, "deg"));
  1154. mi.return_val.type = Variant::REAL;
  1155. return mi;
  1156. } break;
  1157. case MATH_RAD2DEG: {
  1158. MethodInfo mi("rad2deg", PropertyInfo(Variant::REAL, "rad"));
  1159. mi.return_val.type = Variant::REAL;
  1160. return mi;
  1161. } break;
  1162. case MATH_LINEAR2DB: {
  1163. MethodInfo mi("linear2db", PropertyInfo(Variant::REAL, "nrg"));
  1164. mi.return_val.type = Variant::REAL;
  1165. return mi;
  1166. } break;
  1167. case MATH_DB2LINEAR: {
  1168. MethodInfo mi("db2linear", PropertyInfo(Variant::REAL, "db"));
  1169. mi.return_val.type = Variant::REAL;
  1170. return mi;
  1171. } break;
  1172. case LOGIC_MAX: {
  1173. MethodInfo mi("max", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
  1174. mi.return_val.type = Variant::REAL;
  1175. return mi;
  1176. } break;
  1177. case LOGIC_MIN: {
  1178. MethodInfo mi("min", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
  1179. mi.return_val.type = Variant::REAL;
  1180. return mi;
  1181. } break;
  1182. case LOGIC_CLAMP: {
  1183. MethodInfo mi("clamp", PropertyInfo(Variant::REAL, "val"), PropertyInfo(Variant::REAL, "min"), PropertyInfo(Variant::REAL, "max"));
  1184. mi.return_val.type = Variant::REAL;
  1185. return mi;
  1186. } break;
  1187. case LOGIC_NEAREST_PO2: {
  1188. MethodInfo mi("nearest_po2", PropertyInfo(Variant::INT, "val"));
  1189. mi.return_val.type = Variant::INT;
  1190. return mi;
  1191. } break;
  1192. case OBJ_WEAKREF: {
  1193. MethodInfo mi("weakref", PropertyInfo(Variant::OBJECT, "obj"));
  1194. mi.return_val.type = Variant::OBJECT;
  1195. mi.return_val.name = "WeakRef";
  1196. return mi;
  1197. } break;
  1198. case FUNC_FUNCREF: {
  1199. MethodInfo mi("funcref", PropertyInfo(Variant::OBJECT, "instance"), PropertyInfo(Variant::STRING, "funcname"));
  1200. mi.return_val.type = Variant::OBJECT;
  1201. mi.return_val.name = "FuncRef";
  1202. return mi;
  1203. } break;
  1204. case TYPE_CONVERT: {
  1205. MethodInfo mi("convert", PropertyInfo(Variant::NIL, "what"), PropertyInfo(Variant::INT, "type"));
  1206. mi.return_val.type = Variant::OBJECT;
  1207. return mi;
  1208. } break;
  1209. case TYPE_OF: {
  1210. MethodInfo mi("typeof", PropertyInfo(Variant::NIL, "what"));
  1211. mi.return_val.type = Variant::INT;
  1212. return mi;
  1213. } break;
  1214. case TYPE_EXISTS: {
  1215. MethodInfo mi("type_exists", PropertyInfo(Variant::STRING, "type"));
  1216. mi.return_val.type = Variant::BOOL;
  1217. return mi;
  1218. } break;
  1219. case TEXT_STR: {
  1220. MethodInfo mi("str", PropertyInfo(Variant::NIL, "what"), PropertyInfo(Variant::NIL, "..."));
  1221. mi.return_val.type = Variant::STRING;
  1222. return mi;
  1223. } break;
  1224. case TEXT_PRINT: {
  1225. MethodInfo mi("print", PropertyInfo(Variant::NIL, "what"), PropertyInfo(Variant::NIL, "..."));
  1226. mi.return_val.type = Variant::NIL;
  1227. return mi;
  1228. } break;
  1229. case TEXT_PRINT_TABBED: {
  1230. MethodInfo mi("printt", PropertyInfo(Variant::NIL, "what"), PropertyInfo(Variant::NIL, "..."));
  1231. mi.return_val.type = Variant::NIL;
  1232. return mi;
  1233. } break;
  1234. case TEXT_PRINT_SPACED: {
  1235. MethodInfo mi("prints", PropertyInfo(Variant::NIL, "what"), PropertyInfo(Variant::NIL, "..."));
  1236. mi.return_val.type = Variant::NIL;
  1237. return mi;
  1238. } break;
  1239. case TEXT_PRINTERR: {
  1240. MethodInfo mi("printerr", PropertyInfo(Variant::NIL, "what"), PropertyInfo(Variant::NIL, "..."));
  1241. mi.return_val.type = Variant::NIL;
  1242. return mi;
  1243. } break;
  1244. case TEXT_PRINTRAW: {
  1245. MethodInfo mi("printraw", PropertyInfo(Variant::NIL, "what"), PropertyInfo(Variant::NIL, "..."));
  1246. mi.return_val.type = Variant::NIL;
  1247. return mi;
  1248. } break;
  1249. case VAR_TO_STR: {
  1250. MethodInfo mi("var2str", PropertyInfo(Variant::NIL, "var"));
  1251. mi.return_val.type = Variant::STRING;
  1252. return mi;
  1253. } break;
  1254. case STR_TO_VAR: {
  1255. MethodInfo mi("str2var:Variant", PropertyInfo(Variant::STRING, "string"));
  1256. mi.return_val.type = Variant::NIL;
  1257. return mi;
  1258. } break;
  1259. case VAR_TO_BYTES: {
  1260. MethodInfo mi("var2bytes", PropertyInfo(Variant::NIL, "var"));
  1261. mi.return_val.type = Variant::RAW_ARRAY;
  1262. return mi;
  1263. } break;
  1264. case BYTES_TO_VAR: {
  1265. MethodInfo mi("bytes2var:Variant", PropertyInfo(Variant::RAW_ARRAY, "bytes"));
  1266. mi.return_val.type = Variant::NIL;
  1267. return mi;
  1268. } break;
  1269. case GEN_RANGE: {
  1270. MethodInfo mi("range", PropertyInfo(Variant::NIL, "..."));
  1271. mi.return_val.type = Variant::ARRAY;
  1272. return mi;
  1273. } break;
  1274. case RESOURCE_LOAD: {
  1275. MethodInfo mi("load", PropertyInfo(Variant::STRING, "path"));
  1276. mi.return_val.type = Variant::OBJECT;
  1277. mi.return_val.name = "Resource";
  1278. return mi;
  1279. } break;
  1280. case INST2DICT: {
  1281. MethodInfo mi("inst2dict", PropertyInfo(Variant::OBJECT, "inst"));
  1282. mi.return_val.type = Variant::DICTIONARY;
  1283. return mi;
  1284. } break;
  1285. case DICT2INST: {
  1286. MethodInfo mi("dict2inst", PropertyInfo(Variant::DICTIONARY, "dict"));
  1287. mi.return_val.type = Variant::OBJECT;
  1288. return mi;
  1289. } break;
  1290. case HASH: {
  1291. MethodInfo mi("hash", PropertyInfo(Variant::NIL, "var:Variant"));
  1292. mi.return_val.type = Variant::INT;
  1293. return mi;
  1294. } break;
  1295. case COLOR8: {
  1296. MethodInfo mi("Color8", PropertyInfo(Variant::INT, "r8"), PropertyInfo(Variant::INT, "g8"), PropertyInfo(Variant::INT, "b8"), PropertyInfo(Variant::INT, "a8"));
  1297. mi.return_val.type = Variant::COLOR;
  1298. return mi;
  1299. } break;
  1300. case COLORN: {
  1301. MethodInfo mi("ColorN", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::REAL, "alpha"));
  1302. mi.return_val.type = Variant::COLOR;
  1303. return mi;
  1304. } break;
  1305. case PRINT_STACK: {
  1306. MethodInfo mi("print_stack");
  1307. mi.return_val.type = Variant::NIL;
  1308. return mi;
  1309. } break;
  1310. case INSTANCE_FROM_ID: {
  1311. MethodInfo mi("instance_from_id", PropertyInfo(Variant::INT, "instance_id"));
  1312. mi.return_val.type = Variant::OBJECT;
  1313. return mi;
  1314. } break;
  1315. case FUNC_MAX: {
  1316. ERR_FAIL_V(MethodInfo());
  1317. } break;
  1318. }
  1319. #endif
  1320. return MethodInfo();
  1321. }