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

Function getVariableAtPath

newIDE/app/src/EditorFunctions/ApplyVariableChange.js:335–381  ·  view source on GitHub ↗
({
  variablePath,
  variablesContainer,
}: {|
  variablePath: string,
  variablesContainer: gd.VariablesContainer,
|})

Source from the content-addressed store, hash-verified

333};
334
335export const getVariableAtPath = ({
336 variablePath,
337 variablesContainer,
338}: {|
339 variablePath: string,
340 variablesContainer: gd.VariablesContainer,
341|}): gdVariable | null => {
342 const pathSegments = parseVariablePath(variablePath);
343
344 if (pathSegments.length === 0) {
345 throw new Error('Invalid variable path');
346 }
347
348 const firstSegment = pathSegments[0];
349 if (firstSegment.type !== 'property') {
350 throw new Error('Variable path must start with a property name');
351 }
352
353 if (!variablesContainer.has(firstSegment.value)) {
354 return null;
355 }
356 let variable = variablesContainer.get(firstSegment.value);
357
358 for (let i = 1; i < pathSegments.length; i++) {
359 const segment = pathSegments[i];
360 if (segment.type === 'property') {
361 if (
362 variable.getType() !== gd.Variable.Structure ||
363 !variable.hasChild(segment.value)
364 ) {
365 return null;
366 }
367 variable = variable.getChild(segment.value);
368 } else {
369 const index = parseInt(segment.value, 10);
370 if (
371 variable.getType() !== gd.Variable.Array ||
372 index >= variable.getChildrenCount()
373 ) {
374 return null;
375 }
376 variable = variable.getAtIndex(index);
377 }
378 }
379
380 return variable;
381};

Callers 1

index.jsFile · 0.90

Calls 7

parseVariablePathFunction · 0.85
getMethod · 0.65
hasChildMethod · 0.65
hasMethod · 0.45
getTypeMethod · 0.45
getChildMethod · 0.45
getChildrenCountMethod · 0.45

Tested by

no test coverage detected