Skip to content

Commit

Permalink
GH-807 Support named lookup of signals, used by await keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Sep 14, 2024
1 parent 704cfba commit 2fc01a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/script/instances/script_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,33 @@ bool OScriptInstance::set(const StringName& p_name, const Variant& p_value, Prop

bool OScriptInstance::get(const StringName& p_name, Variant& p_value, PropertyError* r_err)
{
// First check if we have a member variable
const String variable_name = _get_variable_name_from_path(p_name);

OScriptVirtualMachine::Variable* variable = _vm.get_variable(variable_name);
if (!variable || !variable->exported)
if (_vm.has_variable(variable_name))
{
OScriptVirtualMachine::Variable* variable = _vm.get_variable(variable_name);
if (!variable || !variable->exported)
{
if (r_err)
*r_err = PROP_NOT_FOUND;
return false;
}

if (r_err)
*r_err = PROP_NOT_FOUND;
*r_err = PROP_OK;

return false;
p_value = variable->value;
return true;
}

if (r_err)
*r_err = PROP_OK;

p_value = variable->value;
// Next check signals - for named access, i.e. "await obj.signal"
if (_vm.has_signal(p_name))
{
p_value = _vm.get_signal(p_name);
return true;
}

return true;
return false;
}

GDExtensionPropertyInfo* OScriptInstance::get_property_list(uint32_t* r_count)
Expand Down
16 changes: 16 additions & 0 deletions src/script/vm/script_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,11 @@ bool OScriptVirtualMachine::register_variable(const Ref<OScriptVariable>& p_vari
return true;
}

bool OScriptVirtualMachine::has_variable(const StringName& p_name) const
{
return _variables.has(p_name);
}

OScriptVirtualMachine::Variable* OScriptVirtualMachine::get_variable(const StringName& p_name) const
{
const HashMap<StringName, Variable>::ConstIterator E = _variables.find(p_name);
Expand Down Expand Up @@ -976,6 +981,17 @@ bool OScriptVirtualMachine::set_variable(const StringName& p_name, const Variant
return true;
}

bool OScriptVirtualMachine::has_signal(const StringName& p_name) const
{
return _script->has_script_signal(p_name);
}

Variant OScriptVirtualMachine::get_signal(const StringName& p_name)
{
ERR_FAIL_COND_V_MSG(!_script->has_script_signal(p_name), Variant(), "No signal with name '" + p_name + "' found.");
return Signal(get_owner(), p_name);
}

bool OScriptVirtualMachine::register_function(const Ref<OScriptFunction>& p_function)
{
ERR_FAIL_COND_V_MSG(!p_function.is_valid(), false, "Cannot register function that is invalid.");
Expand Down
5 changes: 5 additions & 0 deletions src/script/vm/script_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class OScriptVirtualMachine
/// @return true if the variable was registered successfully, false otherwise
bool register_variable(const Ref<OScriptVariable>& p_variable);

bool has_variable(const StringName& p_name) const;

/// Gets the variable by name
/// @param p_name the variable name
/// @return the variable if found, otherwise returns null
Expand All @@ -224,6 +226,9 @@ class OScriptVirtualMachine
/// @return true if the value was set, false otherwise
bool set_variable(const StringName& p_name, const Variant& p_value);

bool has_signal(const StringName& p_name) const;
Variant get_signal(const StringName& p_name);

/// Register a function
/// @param p_function the function to be registered
/// @return true if the function was registered successfully, false othrewise
Expand Down

0 comments on commit 2fc01a4

Please sign in to comment.