editor_run_script.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "editor_run_script.h"
  2. #include "editor_node.h"
  3. void EditorScript::add_root_node(Node *p_node) {
  4. if (!editor) {
  5. EditorNode::add_io_error("EditorScript::add_root_node : Write your logic in the _run() method.");
  6. return;
  7. }
  8. if (editor->get_edited_scene()) {
  9. EditorNode::add_io_error("EditorScript::add_root_node : There is an edited scene already.");
  10. return;
  11. }
  12. editor->set_edited_scene(p_node);
  13. }
  14. Node *EditorScript::get_scene() {
  15. if (!editor) {
  16. EditorNode::add_io_error("EditorScript::get_scene : Write your logic in the _run() method.");
  17. return NULL;
  18. }
  19. return editor->get_edited_scene();
  20. }
  21. void EditorScript::_run() {
  22. Ref<Script> s = get_script();
  23. ERR_FAIL_COND(!s.is_valid());
  24. if (!get_script_instance()) {
  25. EditorNode::add_io_error("Couldn't instance script:\n "+s->get_path()+"\nDid you forget the 'tool' keyword?");
  26. return;
  27. }
  28. Variant::CallError ce;
  29. ce.error=Variant::CallError::CALL_OK;
  30. get_script_instance()->call("_run",NULL,0,ce);
  31. if (ce.error!=Variant::CallError::CALL_OK) {
  32. EditorNode::add_io_error("Couldn't run script:\n "+s->get_path()+"\nDid you forget the '_run' method?");
  33. }
  34. }
  35. void EditorScript::set_editor(EditorNode *p_editor) {
  36. editor=p_editor;
  37. }
  38. void EditorScript::_bind_methods() {
  39. ObjectTypeDB::bind_method(_MD("add_root_node","node"),&EditorScript::add_root_node);
  40. ObjectTypeDB::bind_method(_MD("get_scene"),&EditorScript::get_scene);
  41. BIND_VMETHOD( MethodInfo("_run") );
  42. }
  43. EditorScript::EditorScript() {
  44. editor=NULL;
  45. }