MCPcopy
hub / github.com/4ian/GDevelop / applyVariableChange

Function applyVariableChange

newIDE/app/src/EditorFunctions/ApplyVariableChange.js:142–255  ·  view source on GitHub ↗
({
  variablePath,
  forcedVariableType,
  variablesContainer,
  value,
}: {|
  variablePath: string,
  forcedVariableType: string | null,
  variablesContainer: gd.VariablesContainer,
  value: string,
|})

Source from the content-addressed store, hash-verified

140};
141
142export const applyVariableChange = ({
143 variablePath,
144 forcedVariableType,
145 variablesContainer,
146 value,
147}: {|
148 variablePath: string,
149 forcedVariableType: string | null,
150 variablesContainer: gd.VariablesContainer,
151 value: string,
152|}): {
153 addedNewVariable: boolean,
154 variable: null | gdVariable,
155 variableType: string,
156} => {
157 const pathSegments = parseVariablePath(variablePath);
158
159 if (pathSegments.length === 0) {
160 throw new Error('Invalid variable path');
161 }
162
163 let addedNewVariable = false;
164 const firstSegment = pathSegments[0];
165
166 if (firstSegment.type !== 'property') {
167 throw new Error('Variable path must start with a property name');
168 }
169
170 const firstVariableName = firstSegment.value;
171 let variable = null;
172 if (!variablesContainer.has(firstVariableName)) {
173 variable = variablesContainer.insertNew(firstVariableName, 0);
174 addedNewVariable = true;
175 } else {
176 variable = variablesContainer.get(firstVariableName);
177 }
178
179 for (let i = 1; i < pathSegments.length; i++) {
180 const segment = pathSegments[i];
181
182 if (segment.type === 'property') {
183 // Navigate to structure property. Only cast if not already a Structure:
184 // castTo always clears children, even when the type already matches.
185 // $FlowFixMe[incompatible-use]
186 if (variable.getType() !== gd.Variable.Structure) {
187 // $FlowFixMe[incompatible-use]
188 variable.castTo('Structure');
189 }
190 // $FlowFixMe[incompatible-use]
191 if (!variable.hasChild(segment.value)) {
192 addedNewVariable = true;
193 }
194 // $FlowFixMe[incompatible-use]
195 variable = variable.getChild(segment.value);
196 } else if (segment.type === 'index') {
197 // Navigate to array element. Only cast if not already an Array:
198 // castTo always clears children, even when the type already matches.
199 const index = parseInt(segment.value, 10);

Callers 2

index.jsFile · 0.90

Calls 13

parseVariablePathFunction · 0.85
readOrInferVariableTypeFunction · 0.85
getMethod · 0.65
hasChildMethod · 0.65
hasMethod · 0.45
getTypeMethod · 0.45
castToMethod · 0.45
getChildMethod · 0.45
getChildrenCountMethod · 0.45
setStringMethod · 0.45

Tested by

no test coverage detected