1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571 | /**************************************************************************/
/* visual_script_yield_nodes.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "visual_script_yield_nodes.h"
#include "core/os/os.h"
#include "scene/main/node.h"
#include "scene/main/scene_tree.h"
#include "visual_script_nodes.h"
//////////////////////////////////////////
////////////////YIELD///////////
//////////////////////////////////////////
int VisualScriptYield::get_output_sequence_port_count() const {
return 1;
}
bool VisualScriptYield::has_input_sequence_port() const {
return true;
}
int VisualScriptYield::get_input_value_port_count() const {
return 0;
}
int VisualScriptYield::get_output_value_port_count() const {
return 0;
}
String VisualScriptYield::get_output_sequence_port_text(int p_port) const {
return String();
}
PropertyInfo VisualScriptYield::get_input_value_port_info(int p_idx) const {
return PropertyInfo();
}
PropertyInfo VisualScriptYield::get_output_value_port_info(int p_idx) const {
return PropertyInfo();
}
String VisualScriptYield::get_caption() const {
return yield_mode == YIELD_RETURN ? RTR("Yield") : RTR("Wait");
}
String VisualScriptYield::get_text() const {
switch (yield_mode) {
case YIELD_RETURN:
return "";
break;
case YIELD_FRAME:
return RTR("Next Frame");
break;
case YIELD_PHYSICS_FRAME:
return RTR("Next Physics Frame");
break;
case YIELD_WAIT:
return vformat(RTR("%s sec(s)"), rtos(wait_time));
break;
}
return String();
}
class VisualScriptNodeInstanceYield : public VisualScriptNodeInstance {
public:
VisualScriptYield::YieldMode mode;
float wait_time;
virtual int get_working_memory_size() const { return 1; } //yield needs at least 1<--- Function in derived class
//virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
//virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {<--- Function in derived class
if (p_start_mode == START_MODE_RESUME_YIELD) {
return 0; //resuming yield
} else {
//yield
SceneTree *tree = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
if (!tree) {
r_error_str = "Main Loop is not SceneTree";
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
return 0;
}
Ref<VisualScriptFunctionState> state;
state.instance();
int ret = STEP_YIELD_BIT;
switch (mode) {
case VisualScriptYield::YIELD_RETURN:
ret = STEP_EXIT_FUNCTION_BIT;
break; //return the yield
case VisualScriptYield::YIELD_FRAME:
state->connect_to_signal(tree, "idle_frame", Array());
break;
case VisualScriptYield::YIELD_PHYSICS_FRAME:
state->connect_to_signal(tree, "physics_frame", Array());
break;
case VisualScriptYield::YIELD_WAIT:
state->connect_to_signal(tree->create_timer(wait_time).ptr(), "timeout", Array());
break;
}
*p_working_mem = state;
return ret;
}
}
};
VisualScriptNodeInstance *VisualScriptYield::instance(VisualScriptInstance *p_instance) {
VisualScriptNodeInstanceYield *instance = memnew(VisualScriptNodeInstanceYield);
//instance->instance=p_instance;
instance->mode = yield_mode;
instance->wait_time = wait_time;
return instance;
}
void VisualScriptYield::set_yield_mode(YieldMode p_mode) {
if (yield_mode == p_mode) {
return;
}
yield_mode = p_mode;
ports_changed_notify();
_change_notify();
}
VisualScriptYield::YieldMode VisualScriptYield::get_yield_mode() {
return yield_mode;
}
void VisualScriptYield::set_wait_time(float p_time) {
if (wait_time == p_time) {
return;
}
wait_time = p_time;
ports_changed_notify();
}
float VisualScriptYield::get_wait_time() {
return wait_time;
}
void VisualScriptYield::_validate_property(PropertyInfo &property) const {
if (property.name == "wait_time") {
if (yield_mode != YIELD_WAIT) {
property.usage = 0;
}
}
}
void VisualScriptYield::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_yield_mode", "mode"), &VisualScriptYield::set_yield_mode);
ClassDB::bind_method(D_METHOD("get_yield_mode"), &VisualScriptYield::get_yield_mode);
ClassDB::bind_method(D_METHOD("set_wait_time", "sec"), &VisualScriptYield::set_wait_time);
ClassDB::bind_method(D_METHOD("get_wait_time"), &VisualScriptYield::get_wait_time);
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Frame,Physics Frame,Time", PROPERTY_USAGE_NOEDITOR), "set_yield_mode", "get_yield_mode");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "wait_time"), "set_wait_time", "get_wait_time");
BIND_ENUM_CONSTANT(YIELD_FRAME);
BIND_ENUM_CONSTANT(YIELD_PHYSICS_FRAME);
BIND_ENUM_CONSTANT(YIELD_WAIT);
}
VisualScriptYield::VisualScriptYield() {
yield_mode = YIELD_FRAME;
wait_time = 1;
}
template <VisualScriptYield::YieldMode MODE>
static Ref<VisualScriptNode> create_yield_node(const String &p_name) {
Ref<VisualScriptYield> node;
node.instance();
node->set_yield_mode(MODE);
return node;
}
///////////////////////////////////////////////////
////////////////YIELD SIGNAL//////////////////////
//////////////////////////////////////////////////
int VisualScriptYieldSignal::get_output_sequence_port_count() const {
return 1;
}
bool VisualScriptYieldSignal::has_input_sequence_port() const {
return true;
}
Node *VisualScriptYieldSignal::_get_base_node() const {
#ifdef TOOLS_ENABLED
Ref<Script> script = get_visual_script();
if (!script.is_valid()) {
return nullptr;
}
MainLoop *main_loop = OS::get_singleton()->get_main_loop();
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
if (!scene_tree) {
return nullptr;
}
Node *edited_scene = scene_tree->get_edited_scene_root();
if (!edited_scene) {
return nullptr;
}
Node *script_node = NSVisualScript::_find_script_node(edited_scene, edited_scene, script);
if (!script_node) {
return nullptr;
}
if (!script_node->has_node(base_path)) {
return nullptr;
}
Node *path_to = script_node->get_node(base_path);
return path_to;
#else
return NULL;
#endif
}
StringName VisualScriptYieldSignal::_get_base_type() const {
if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
return get_visual_script()->get_instance_base_type();
} else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
Node *path = _get_base_node();<--- Assignment 'path=_get_base_node()', assigned value is 0
if (path) {<--- Condition 'path' is always false
return path->get_class();
}
}
return base_type;
}
int VisualScriptYieldSignal::get_input_value_port_count() const {
if (call_mode == CALL_MODE_INSTANCE) {
return 1;
} else {
return 0;
}
}
int VisualScriptYieldSignal::get_output_value_port_count() const {
MethodInfo sr;
if (!ClassDB::get_signal(_get_base_type(), signal, &sr)) {
return 0;
}
return sr.arguments.size();
}
String VisualScriptYieldSignal::get_output_sequence_port_text(int p_port) const {
return String();
}
PropertyInfo VisualScriptYieldSignal::get_input_value_port_info(int p_idx) const {
if (call_mode == CALL_MODE_INSTANCE) {
return PropertyInfo(Variant::OBJECT, "instance");
} else {
return PropertyInfo();
}
}
PropertyInfo VisualScriptYieldSignal::get_output_value_port_info(int p_idx) const {
MethodInfo sr;
if (!ClassDB::get_signal(_get_base_type(), signal, &sr)) {
return PropertyInfo(); //no signal
}
ERR_FAIL_INDEX_V(p_idx, sr.arguments.size(), PropertyInfo());
return sr.arguments[p_idx];
}
String VisualScriptYieldSignal::get_caption() const {
switch (call_mode) {
case CALL_MODE_SELF: {
return RTR("WaitSignal");
} break;
case CALL_MODE_NODE_PATH: {
return RTR("WaitNodeSignal");
} break;
case CALL_MODE_INSTANCE: {
return RTR("WaitInstanceSignal");
} break;
}
return String();
}
String VisualScriptYieldSignal::get_text() const {
if (call_mode == CALL_MODE_SELF) {
return " " + String(signal) + "()";
} else {
return " " + _get_base_type() + "." + String(signal) + "()";
}
}
void VisualScriptYieldSignal::set_base_type(const StringName &p_type) {
if (base_type == p_type) {
return;
}
base_type = p_type;
_change_notify();
ports_changed_notify();
}
StringName VisualScriptYieldSignal::get_base_type() const {
return base_type;
}
void VisualScriptYieldSignal::set_signal(const StringName &p_type) {
if (signal == p_type) {
return;
}
signal = p_type;
_change_notify();
ports_changed_notify();
}
StringName VisualScriptYieldSignal::get_signal() const {
return signal;
}
void VisualScriptYieldSignal::set_base_path(const NodePath &p_type) {
if (base_path == p_type) {
return;
}
base_path = p_type;
_change_notify();
ports_changed_notify();
}
NodePath VisualScriptYieldSignal::get_base_path() const {
return base_path;
}
void VisualScriptYieldSignal::set_call_mode(CallMode p_mode) {
if (call_mode == p_mode) {
return;
}
call_mode = p_mode;
_change_notify();
ports_changed_notify();
}
VisualScriptYieldSignal::CallMode VisualScriptYieldSignal::get_call_mode() const {
return call_mode;
}
void VisualScriptYieldSignal::_validate_property(PropertyInfo &property) const {
if (property.name == "base_type") {
if (call_mode != CALL_MODE_INSTANCE) {
property.usage = PROPERTY_USAGE_NOEDITOR;
}
}
if (property.name == "node_path") {
if (call_mode != CALL_MODE_NODE_PATH) {
property.usage = 0;
} else {
Node *bnode = _get_base_node();<--- Assignment 'bnode=_get_base_node()', assigned value is 0
if (bnode) {<--- Condition 'bnode' is always false
property.hint_string = bnode->get_path(); //convert to loong string
}
}
}
if (property.name == "signal") {
property.hint = PROPERTY_HINT_ENUM;
List<MethodInfo> methods;
ClassDB::get_signal_list(_get_base_type(), &methods);
List<String> mstring;
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
if (E->get().name.begins_with("_")) {
continue;
}
mstring.push_back(E->get().name.get_slice(":", 0));
}
mstring.sort();
String ml;
for (List<String>::Element *E = mstring.front(); E; E = E->next()) {
if (ml != String()) {
ml += ",";
}
ml += E->get();
}
property.hint_string = ml;
}
}
void VisualScriptYieldSignal::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptYieldSignal::set_base_type);
ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptYieldSignal::get_base_type);
ClassDB::bind_method(D_METHOD("set_signal", "signal"), &VisualScriptYieldSignal::set_signal);
ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptYieldSignal::get_signal);
ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptYieldSignal::set_call_mode);
ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptYieldSignal::get_call_mode);
ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptYieldSignal::set_base_path);
ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptYieldSignal::get_base_path);
String bt;
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
if (i > 0) {
bt += ",";
}
bt += Variant::get_type_name(Variant::Type(i));
}
ADD_PROPERTY(PropertyInfo(Variant::INT, "call_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance"), "set_call_mode", "get_call_mode");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
BIND_ENUM_CONSTANT(CALL_MODE_SELF);
BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
}
class VisualScriptNodeInstanceYieldSignal : public VisualScriptNodeInstance {
public:
VisualScriptYieldSignal::CallMode call_mode;
NodePath node_path;
int output_args;
StringName signal;
VisualScriptYieldSignal *node;<--- Shadowed declaration
VisualScriptInstance *instance;
virtual int get_working_memory_size() const { return 1; }<--- Function in derived class
//virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
//virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {<--- Function in derived class
if (p_start_mode == START_MODE_RESUME_YIELD) {
return 0; //resuming yield
} else {
//yield
Object *object = nullptr;
switch (call_mode) {
case VisualScriptYieldSignal::CALL_MODE_SELF: {
object = instance->get_owner_ptr();
} break;
case VisualScriptYieldSignal::CALL_MODE_NODE_PATH: {
Node *node = Object::cast_to<Node>(instance->get_owner_ptr());<--- Shadow variable
if (!node) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
r_error_str = "Base object is not a Node!";
return 0;
}
Node *another = node->get_node(node_path);
if (!another) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
r_error_str = "Path does not lead Node!";
return 0;
}
object = another;
} break;
case VisualScriptYieldSignal::CALL_MODE_INSTANCE: {
object = *p_inputs[0];
if (!object) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
r_error_str = "Supplied instance input is null.";
return 0;
}
} break;
}
Ref<VisualScriptFunctionState> state;
state.instance();
state->connect_to_signal(object, signal, Array());
*p_working_mem = state;
return STEP_YIELD_BIT;
}
}
};
VisualScriptNodeInstance *VisualScriptYieldSignal::instance(VisualScriptInstance *p_instance) {
VisualScriptNodeInstanceYieldSignal *instance = memnew(VisualScriptNodeInstanceYieldSignal);
instance->node = this;
instance->instance = p_instance;
instance->signal = signal;
instance->call_mode = call_mode;
instance->node_path = base_path;
instance->output_args = get_output_value_port_count();
return instance;
}
VisualScriptYieldSignal::VisualScriptYieldSignal() {
call_mode = CALL_MODE_SELF;
base_type = "Object";<--- Variable 'base_type' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'base_type' a value by passing the value to the constructor in the initialization list.
}
template <VisualScriptYieldSignal::CallMode cmode>
static Ref<VisualScriptNode> create_yield_signal_node(const String &p_name) {
Ref<VisualScriptYieldSignal> node;
node.instance();
node->set_call_mode(cmode);
return node;
}
void register_visual_script_yield_nodes() {
VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_frame", create_yield_node<VisualScriptYield::YIELD_FRAME>);
VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_physics_frame", create_yield_node<VisualScriptYield::YIELD_PHYSICS_FRAME>);
VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_time", create_yield_node<VisualScriptYield::YIELD_WAIT>);
VisualScriptLanguage::singleton->add_register_func("functions/yield", create_yield_node<VisualScriptYield::YIELD_RETURN>);
VisualScriptLanguage::singleton->add_register_func("functions/yield_signal", create_node_generic<VisualScriptYieldSignal>);
}
|