Skip to content

Commit

Permalink
GH-444 Correctly identify variant arguments for functions/signals
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Jul 10, 2024
1 parent 7ef6caa commit 6096533
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/common/property_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This file is part of the Godot Orchestrator project.
//
// Copyright (c) 2023-present Vahera Studios LLC and its contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "common/property_utils.h"

namespace PropertyUtils
{
bool is_nil_no_variant(const PropertyInfo& p_property)
{
return is_nil(p_property) && !(p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
}
}
37 changes: 37 additions & 0 deletions src/common/property_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is part of the Godot Orchestrator project.
//
// Copyright (c) 2023-present Vahera Studios LLC and its contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef ORCHESTRATOR_PROPERTY_UTILS_H
#define ORCHESTRATOR_PROPERTY_UTILS_H

#include <godot_cpp/core/property_info.hpp>

namespace PropertyUtils
{
using namespace godot;

/// Checks whether the property type is <code>NIL</code>
/// @param p_property the property to check
/// @return true if the property is NIL, false otherwise
_FORCE_INLINE_ bool is_nil(const PropertyInfo& p_property) { return p_property.type == Variant::NIL; }

/// Checks whether the property type is <code>NIL</code> but the variant flag is not set.
/// @param p_property the property to check
/// @return true if the property is <code>NIL</code> but has no variant flag set
bool is_nil_no_variant(const PropertyInfo& p_property);
}

#endif // ORCHESTRATOR_PROPERTY_UTILS_H
18 changes: 16 additions & 2 deletions src/script/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "common/dictionary_utils.h"
#include "common/method_utils.h"
#include "common/property_utils.h"
#include "nodes/functions/function_result.h"
#include "script/script.h"

Expand Down Expand Up @@ -72,6 +73,10 @@ bool OScriptFunction::_set(const StringName &p_name, const Variant &p_value)
{
if (argument.usage == 7)
argument.usage = PROPERTY_USAGE_DEFAULT;

// If "Any" (Variant::NIL) set the usage flag correctly
if (PropertyUtils::is_nil_no_variant(argument))
argument.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
}

// Cleanup return value usage flags that were constructed incorrectly due to godot-cpp bug
Expand Down Expand Up @@ -201,8 +206,9 @@ bool OScriptFunction::resize_argument_list(size_t p_new_size)
_method.arguments.resize(p_new_size);
for (size_t i = current_size; i < p_new_size; i++)
{
_method.arguments[i].name = "arg" + itos(i + 1);
_method.arguments[i].name = "arg" + (i + 1);
_method.arguments[i].type = Variant::NIL;
_method.arguments[i].usage = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT;
}
result = true;
}
Expand All @@ -223,7 +229,15 @@ void OScriptFunction::set_argument_type(size_t p_index, Variant::Type p_type)
{
if (_method.arguments.size() > p_index && _user_defined)
{
_method.arguments[p_index].type = p_type;
PropertyInfo& pi = _method.arguments[p_index];
pi.type = p_type;

// Function arguments set as "Any" type imply variant, using Variant::NIL
if (PropertyUtils::is_nil(pi))
pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
else
pi.usage &= ~PROPERTY_USAGE_NIL_IS_VARIANT;

emit_changed();
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/script/signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "script/signals.h"

#include "common/dictionary_utils.h"
#include "common/property_utils.h"
#include "common/variant_utils.h"
#include "script/script.h"

Expand Down Expand Up @@ -156,11 +157,11 @@ bool OScriptSignal::resize_argument_list(size_t p_new_size)
_method.arguments.resize(p_new_size);
for (size_t i = current_size; i < p_new_size; i++)
{
_method.arguments[i].name = "arg" + itos(i + 1);
_method.arguments[i].name = "arg" + (i + 1);
_method.arguments[i].type = Variant::NIL;

// Cleanup the argument usage flags that were constructed incorrectly due to godot-cpp bug
_method.arguments[i].usage = PROPERTY_USAGE_DEFAULT;
_method.arguments[i].usage = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT;
}
result = true;
}
Expand All @@ -180,7 +181,15 @@ void OScriptSignal::set_argument_type(size_t p_index, Variant::Type p_type)
{
if (_method.arguments.size() > p_index)
{
_method.arguments[p_index].type = p_type;
PropertyInfo& pi = _method.arguments[p_index];
pi.type = p_type;

// Indicate that "Any" (Variant::NIL) indicates Variant types
if (PropertyUtils::is_nil_no_variant(pi))
pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
else
pi.usage &= ~PROPERTY_USAGE_NIL_IS_VARIANT;

emit_changed();
}
}
Expand Down

0 comments on commit 6096533

Please sign in to comment.