Skip to content

Commit

Permalink
Check invalid node name
Browse files Browse the repository at this point in the history
  • Loading branch information
volzhs committed May 2, 2018
1 parent a096df5 commit 6758b6c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
7 changes: 4 additions & 3 deletions editor/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,11 +644,12 @@ void SceneTreeEditor::_renamed() {
ERR_FAIL_COND(!n);

String new_name = which->get_text(0);
if (new_name.find(".") != -1 || new_name.find("/") != -1) {
if (!Node::_validate_node_name(new_name)) {

error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n \".\", \"/\"");
error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + Node::invalid_character);
error->popup_centered_minsize();
new_name = n->get_name();

which->set_text(0, new_name);
}

if (new_name == n->get_name())
Expand Down
16 changes: 15 additions & 1 deletion scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,23 @@ void Node::_set_name_nocheck(const StringName &p_name) {
data.name = p_name;
}

String Node::invalid_character = ". : @ / \"";

bool Node::_validate_node_name(String &p_name) {
String name = p_name;
Vector<String> chars = Node::invalid_character.split(" ");
for (int i = 0; i < chars.size(); i++) {
name = name.replace(chars[i], "");
}
bool is_valid = name == p_name;
p_name = name;
return is_valid;
}

void Node::set_name(const String &p_name) {

String name = p_name.replace(":", "").replace("/", "").replace("@", "");
String name = p_name;
_validate_node_name(name);

ERR_FAIL_COND(name == "");
data.name = name;
Expand Down
6 changes: 6 additions & 0 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ class Node : public Object {

void _set_tree(SceneTree *p_tree);

#ifdef TOOLS_ENABLED
friend class SceneTreeEditor;
#endif
static String invalid_character;
static bool _validate_node_name(String &p_name);

protected:
void _block() { data.blocked++; }
void _unblock() { data.blocked--; }
Expand Down

0 comments on commit 6758b6c

Please sign in to comment.