diff --git a/Core/GDCore/IDE/EventsFunctionTools.cpp b/Core/GDCore/IDE/EventsFunctionTools.cpp index 479a51d84a1b..58acace5ded4 100644 --- a/Core/GDCore/IDE/EventsFunctionTools.cpp +++ b/Core/GDCore/IDE/EventsFunctionTools.cpp @@ -140,14 +140,34 @@ void EventsFunctionTools::ParametersToVariablesContainer( void EventsFunctionTools::PropertiesToVariablesContainer( const PropertiesContainer &properties, gd::VariablesContainer &outputVariablesContainer) { + outputVariablesContainer.Clear(); + AddPropertiesToVariablesContainer( + properties, + outputVariablesContainer); +} + +void EventsFunctionTools::PropertiesToVariablesContainer( + const PropertiesContainer &properties, + const PropertiesContainer &sharedProperties, + gd::VariablesContainer &outputVariablesContainer) { + outputVariablesContainer.Clear(); + AddPropertiesToVariablesContainer( + properties, + outputVariablesContainer); + AddPropertiesToVariablesContainer( + sharedProperties, + outputVariablesContainer); +} + +void EventsFunctionTools::AddPropertiesToVariablesContainer( + const PropertiesContainer &properties, + gd::VariablesContainer &outputVariablesContainer) { if (outputVariablesContainer.GetSourceType() != gd::VariablesContainer::SourceType::Properties) { throw std::logic_error("Tried to generate a variables container from " "properties with the wrong source type."); } - outputVariablesContainer.Clear(); - gd::String lastObjectName; for (std::size_t i = 0; i < properties.GetCount(); ++i) { const auto &property = properties.Get(i); if (property.GetName().empty()) diff --git a/Core/GDCore/IDE/EventsFunctionTools.h b/Core/GDCore/IDE/EventsFunctionTools.h index 52b5c9bd8b5f..3f0bcd59d4eb 100644 --- a/Core/GDCore/IDE/EventsFunctionTools.h +++ b/Core/GDCore/IDE/EventsFunctionTools.h @@ -81,6 +81,11 @@ class GD_CORE_API EventsFunctionTools { const PropertiesContainer &properties, gd::VariablesContainer &outputVariablesContainer); + static void PropertiesToVariablesContainer( + const PropertiesContainer &properties, + const PropertiesContainer &sharedProperties, + gd::VariablesContainer &outputVariablesContainer); + static void ParametersToResourcesContainer( const ParameterMetadataContainer ¶meters, gd::ResourcesContainer &outputResourcesContainer); @@ -88,5 +93,10 @@ class GD_CORE_API EventsFunctionTools { static void PropertiesToResourcesContainer( const PropertiesContainer &properties, gd::ResourcesContainer &outputResourcesContainer); + +private: + static void AddPropertiesToVariablesContainer( + const PropertiesContainer &properties, + gd::VariablesContainer &outputVariablesContainer); }; } // namespace gd diff --git a/Core/GDCore/Project/VariablesContainersList.cpp b/Core/GDCore/Project/VariablesContainersList.cpp index bbe4b9bf7d15..9c955b594265 100644 --- a/Core/GDCore/Project/VariablesContainersList.cpp +++ b/Core/GDCore/Project/VariablesContainersList.cpp @@ -72,11 +72,9 @@ VariablesContainersList VariablesContainersList:: variablesContainersList.Push(extension.GetSceneVariables()); gd::EventsFunctionTools::PropertiesToVariablesContainer( - eventsBasedBehavior.GetSharedPropertyDescriptors(), propertyVariablesContainer); - variablesContainersList.Push(propertyVariablesContainer); - - gd::EventsFunctionTools::PropertiesToVariablesContainer( - eventsBasedBehavior.GetPropertyDescriptors(), propertyVariablesContainer); + eventsBasedBehavior.GetPropertyDescriptors(), + eventsBasedBehavior.GetSharedPropertyDescriptors(), + propertyVariablesContainer); variablesContainersList.Push(propertyVariablesContainer); gd::EventsFunctionTools::ParametersToVariablesContainer( @@ -85,7 +83,7 @@ VariablesContainersList VariablesContainersList:: parameterVariablesContainer); variablesContainersList.Push(parameterVariablesContainer); - variablesContainersList.firstLocalVariableContainerIndex = 5; + variablesContainersList.firstLocalVariableContainerIndex = 4; return variablesContainersList; } diff --git a/GDJS/GDJS/Events/CodeGeneration/MetadataDeclarationHelper.cpp b/GDJS/GDJS/Events/CodeGeneration/MetadataDeclarationHelper.cpp index 82220d0f214a..48c2b5f2794f 100644 --- a/GDJS/GDJS/Events/CodeGeneration/MetadataDeclarationHelper.cpp +++ b/GDJS/GDJS/Events/CodeGeneration/MetadataDeclarationHelper.cpp @@ -1132,9 +1132,7 @@ void MetadataDeclarationHelper::DeclarePropertyInstructionAndExpression( iconURL, iconURL); addObjectAndBehaviorParameters(conditionMetadata); conditionMetadata.SetFunctionName(getterName); - if (!isSharedProperty) { - conditionMetadata.SetHidden(); - } + conditionMetadata.SetHidden(); auto &setterActionMetadata = entityMetadata.AddScopedAction( actionName, propertyLabel, @@ -1152,9 +1150,7 @@ void MetadataDeclarationHelper::DeclarePropertyInstructionAndExpression( setterActionMetadata .AddParameter("yesorno", _("New value to set"), "", false) .SetFunctionName(setterName); - if (!isSharedProperty) { - setterActionMetadata.SetHidden(); - } + setterActionMetadata.SetHidden(); auto &toggleActionMetadata = entityMetadata.AddScopedAction( toggleActionName, _("Toggle") + " " + propertyLabel, @@ -1168,9 +1164,7 @@ void MetadataDeclarationHelper::DeclarePropertyInstructionAndExpression( iconURL, iconURL); addObjectAndBehaviorParameters(toggleActionMetadata); toggleActionMetadata.SetFunctionName(toggleFunctionName); - if (!isSharedProperty) { - toggleActionMetadata.SetHidden(); - } + toggleActionMetadata.SetHidden(); } else { auto typeExtraInfo = GetStringifiedExtraInfo(property); auto parameterOptions = gd::ParameterOptions::MakeNewOptions(); @@ -1194,9 +1188,7 @@ void MetadataDeclarationHelper::DeclarePropertyInstructionAndExpression( parameterOptions) .SetFunctionName(setterName) .SetGetter(getterName); - if (!isSharedProperty) { - propertyInstructionMetadata.SetHidden(); - } + propertyInstructionMetadata.SetHidden(); } } diff --git a/GDevelop.js/__tests__/GDJSBehaviorCodeGenerationIntegrationTests.js b/GDevelop.js/__tests__/GDJSBehaviorCodeGenerationIntegrationTests.js index 4ff6eef70edf..daab357e563d 100644 --- a/GDevelop.js/__tests__/GDJSBehaviorCodeGenerationIntegrationTests.js +++ b/GDevelop.js/__tests__/GDJSBehaviorCodeGenerationIntegrationTests.js @@ -325,6 +325,59 @@ describe('libGD.js - GDJS Behavior Code Generation integration tests', function expect(behavior._getMyIdentifier()).toBe(456); }); + it('Can use a shared property in a variable action', () => { + const project = new gd.ProjectHelper.createNewGDJSProject(); + const eventsFunctionsExtension = project.insertNewEventsFunctionsExtension( + 'MyExtension', + 0 + ); + const eventsBasedBehavior = eventsFunctionsExtension + .getEventsBasedBehaviors() + .insertNew('MyBehavior', 0); + + eventsBasedBehavior + .getSharedPropertyDescriptors() + .insertNew('MySharedProperty', 0) + .setValue('123') + .setType('Number'); + + const eventsSerializerElement = gd.Serializer.fromJSObject([ + { + type: 'BuiltinCommonInstructions::Standard', + conditions: [], + actions: [ + { + type: { value: 'SetNumberVariable' }, + parameters: ['MySharedProperty', '=', '456'], + }, + ], + }, + ]); + eventsBasedBehavior + .getEventsFunctions() + .insertNewEventsFunction('MyFunction', 0) + .getEvents() + .unserializeFrom(project, eventsSerializerElement); + gd.WholeProjectRefactorer.ensureBehaviorEventsFunctionsProperParameters( + eventsFunctionsExtension, + eventsBasedBehavior + ); + + const { runtimeScene, behavior } = generatedBehavior( + gd, + project, + eventsFunctionsExtension, + eventsBasedBehavior, + { logCode: false } + ); + + // Check the default value is set. + expect(behavior._sharedData._getMySharedProperty()).toBe(123); + + behavior.MyFunction(); + expect(behavior._sharedData._getMySharedProperty()).toBe(456); + }); + it('Can use a property in a variable condition', () => { const project = new gd.ProjectHelper.createNewGDJSProject(); const scene = project.insertNewLayout('MyScene', 0); @@ -479,6 +532,80 @@ describe('libGD.js - GDJS Behavior Code Generation integration tests', function ).toBe(456); }); + it('Can use a shared property in a variable condition', () => { + const project = new gd.ProjectHelper.createNewGDJSProject(); + const scene = project.insertNewLayout('MyScene', 0); + const eventsFunctionsExtension = project.insertNewEventsFunctionsExtension( + 'MyExtension', + 0 + ); + const eventsBasedBehavior = eventsFunctionsExtension + .getEventsBasedBehaviors() + .insertNew('MyBehavior', 0); + + eventsBasedBehavior + .getSharedPropertyDescriptors() + .insertNew('MySharedProperty', 0) + .setValue('123') + .setType('Number'); + + eventsFunctionsExtension + .getSceneVariables() + .insertNew('MyVariable', 0) + .setValue(0); + + const eventsSerializerElement = gd.Serializer.fromJSObject([ + { + type: 'BuiltinCommonInstructions::Standard', + conditions: [ + { + type: { value: 'NumberVariable' }, + parameters: ['MySharedProperty', '=', '123'], + }, + ], + actions: [ + { + type: { value: 'SetNumberVariable' }, + parameters: ['MyVariable', '=', '456'], + }, + ], + }, + ]); + eventsBasedBehavior + .getEventsFunctions() + .insertNewEventsFunction('MyFunction', 0) + .getEvents() + .unserializeFrom(project, eventsSerializerElement); + gd.WholeProjectRefactorer.ensureBehaviorEventsFunctionsProperParameters( + eventsFunctionsExtension, + eventsBasedBehavior + ); + + const { runtimeScene, behavior } = generatedBehavior( + gd, + project, + eventsFunctionsExtension, + eventsBasedBehavior, + { logCode: false } + ); + + // Check the default value is set. + expect( + runtimeScene + .getVariablesForExtension('MyExtension') + .get('MyVariable') + .getAsNumber() + ).toBe(0); + + behavior.MyFunction(); + expect( + runtimeScene + .getVariablesForExtension('MyExtension') + .get('MyVariable') + .getAsNumber() + ).toBe(456); + }); + it('Can use a parameter in a variable condition', () => { const project = new gd.ProjectHelper.createNewGDJSProject(); const eventsFunctionsExtension = project.insertNewEventsFunctionsExtension( diff --git a/GDevelop.js/__tests__/MetadataDeclarationHelper.js b/GDevelop.js/__tests__/MetadataDeclarationHelper.js index 0daeea381f35..bad00b4cf468 100644 --- a/GDevelop.js/__tests__/MetadataDeclarationHelper.js +++ b/GDevelop.js/__tests__/MetadataDeclarationHelper.js @@ -976,8 +976,7 @@ describe('MetadataDeclarationHelper', () => { expect(action.getSentence()).toBe( 'Change the property value for Value of _PARAM0_: _PARAM2_ _PARAM3_' ); - // Shared properties can't be used in variable instructions yet. - expect(action.isHidden()).toBe(false); + expect(action.isHidden()).toBe(true); expect(action.isPrivate()).toBe(true); expect(action.getParametersCount()).toBe(4); @@ -1001,8 +1000,7 @@ describe('MetadataDeclarationHelper', () => { expect(condition.getSentence()).toBe( 'The property value for Value of _PARAM0_ _PARAM2_ _PARAM3_' ); - // Shared properties can't be used in variable instructions yet. - expect(condition.isHidden()).toBe(false); + expect(condition.isHidden()).toBe(true); expect(condition.isPrivate()).toBe(true); expect(condition.getParametersCount()).toBe(4); @@ -1077,8 +1075,7 @@ describe('MetadataDeclarationHelper', () => { expect(action.getSentence()).toBe( 'Set property value for Value of _PARAM0_ to _PARAM2_' ); - // Shared properties can't be used in variable instructions yet. - expect(action.isHidden()).toBe(false); + expect(action.isHidden()).toBe(true); expect(action.isPrivate()).toBe(true); expect(action.getParametersCount()).toBe(3); @@ -1100,8 +1097,7 @@ describe('MetadataDeclarationHelper', () => { expect(condition.getSentence()).toBe( 'Property Value of _PARAM0_ is true' ); - // Shared properties can't be used in variable instructions yet. - expect(condition.isHidden()).toBe(false); + expect(condition.isHidden()).toBe(true); expect(condition.isPrivate()).toBe(true); expect(condition.getParametersCount()).toBe(2);