Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Core/GDCore/IDE/EventsFunctionTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions Core/GDCore/IDE/EventsFunctionTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,22 @@ 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 &parameters,
gd::ResourcesContainer &outputResourcesContainer);

static void PropertiesToResourcesContainer(
const PropertiesContainer &properties,
gd::ResourcesContainer &outputResourcesContainer);

private:
static void AddPropertiesToVariablesContainer(
const PropertiesContainer &properties,
gd::VariablesContainer &outputVariablesContainer);
};
} // namespace gd
10 changes: 4 additions & 6 deletions Core/GDCore/Project/VariablesContainersList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines 74 to 78
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared properties were cleared since the same variable container is used.


gd::EventsFunctionTools::ParametersToVariablesContainer(
Expand All @@ -85,7 +83,7 @@ VariablesContainersList VariablesContainersList::
parameterVariablesContainer);
variablesContainersList.Push(parameterVariablesContainer);

variablesContainersList.firstLocalVariableContainerIndex = 5;
variablesContainersList.firstLocalVariableContainerIndex = 4;
return variablesContainersList;
}

Expand Down
16 changes: 4 additions & 12 deletions GDJS/GDJS/Events/CodeGeneration/MetadataDeclarationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -1194,9 +1188,7 @@ void MetadataDeclarationHelper::DeclarePropertyInstructionAndExpression(
parameterOptions)
.SetFunctionName(setterName)
.SetGetter(getterName);
if (!isSharedProperty) {
propertyInstructionMetadata.SetHidden();
}
propertyInstructionMetadata.SetHidden();
}
}

Expand Down
127 changes: 127 additions & 0 deletions GDevelop.js/__tests__/GDJSBehaviorCodeGenerationIntegrationTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 4 additions & 8 deletions GDevelop.js/__tests__/MetadataDeclarationHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading