Plugin SDK 2 Loop

1 favourites
  • 7 posts
From the Asset Store
Easily integrate Telegram Mini Apps SDK into your Construct 3 projects for building interactive apps and games.
  • Is it possible to create loops in my Plugin SDK 2? In the old SDK 1, loops work as expected and perform their function successfully. However, in SDK 2, I can't seem to create loops that execute for each element or a specific number of times. Below is an example script I made in `conditions.js` (I created this script after extensively searching through the Construct 3 documentation, and this script only progressed to `GetSolModificators()`, but with an error due to `GetCurrentEvent()` returning null, even though I followed the documentation):

    	TestLoop(Count) {
    		const runtime = this.runtime;
    		const eventSheetManager = new C3.EventSheetManager(runtime);
    		const eventStack = eventSheetManager.GetEventStack();
    		const oldFrame = eventStack.GetCurrentStackFrame();
    		const currentEvent = oldFrame.GetCurrentEvent();
    		const solModifiers = currentEvent.GetSolModifiers();
     	const newFrame = eventStack.Push(currentEvent);
    
    		for (let i = 0; i < Count; i++) {
    			// ... optionally update state here ...
    
    			// Push a copy of the current SOL
    			eventSheetManager.PushCopySol(solModifiers);
    
    			// Retrigger the current event, running a single loop iteration
    			eventSheet.Retrigger(oldFrame, newFrame);
    
    			// Pop the current SOL
    			eventSheetManager.PopSol(solModifiers);
    		}
    			
    		// Pop the event stack frame
    		eventStack.Pop();
    		// Return false since event already executed
    		return false;
    	}
    

    The `aces.json` is as follows:

    {

    "highlight": false,

    "id": "TestLoop",

    "isLooping": true,

    "isAsync": false,

    "isInvertible": false,

    "params": [ { "id": "Count", "type": "number" } ],

    "scriptName": "TestLoop"

    }

    After creating a new project, adding this loop and actions to it on layout start or button press, an error appears in the console:

    If anyone can help me fix the error so I can finally create loops in Plugin SDK 2, I would be very grateful!

  • It's not yet supported in the Addon SDK v2 - it's on the list to get implemented soon though. I think this is also a good case to redesign the API so you don't have to write so much code to make a looping condition.

  • This is now supported in r401 with the new method createLoopingConditionContext(). You can use it like this (sample code for a 'TestLoop' condition that repeats a number of times):

    TestLoop(count)
    {
    	const loopCtx = this.runtime.sdk.createLoopingConditionContext();
    
    	for (let i = 0; i < count; ++i)
    	{
    		loopCtx.retrigger();
    
    		if (loopCtx.isStopped)
    			break;
    	}
    
    	loopCtx.release();
    }
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you for adding loops to Construct 3! Now, I can create various loops with delays between iterations. However, when waiting for a condition, the loop executes immediately without waiting exactly once, but it still remembers itself and executes a second time when the condition is met.

    async TestWaitingVariable() {
    	const loopCtx = this.runtime.sdk.createLoopingConditionContext();
    	
    	while (this._variable !== true) {
    		if (!loopCtx.isStopped) {
    			await new Promise(resolve => setTimeout(resolve, 100));
    		} else {
    			break;
    		}
    	}
    	
    	if (!loopCtx.isStopped)
    		loopCtx.retrigger();
    	loopCtx.release();
    }
    

    In this example, I want to wait until the variable this._variable equals true, and if it is true, then execute the code. However, when I run the current loop, all actions execute immediately without waiting exactly once (when this._variable = false), and when I change the variable this._variable to true in another action, all actions execute once more or multiple times (depending on the number of loops previously called). How can I make it wait until the variable equals true and then execute the actions in the current loop block in Construct 3?

    Thank you for your help!

  • Construct doesn't support asyncronous loop condition methods. You'll have to find another way to achieve this.

  • This is now supported in r401 with the new method createLoopingConditionContext(). You can use it like this (sample code for a 'TestLoop' condition that repeats a number of times):

    > TestLoop(count)
    {
    	const loopCtx = this.runtime.sdk.createLoopingConditionContext();
    
    	for (let i = 0; i < count; ++i)
    	{
    		loopCtx.retrigger();
    
    		if (loopCtx.isStopped)
    			break;
    	}
    
    	loopCtx.release();
    }
    

    Hi, Ashley!

    The property sdk appears to be undefined on TypeScript types.

    createLoopingConditionContext is -indeed- defined on a ISDKUtils type which is never referenced or used in any other type-def file.

    FYI, I just generated the type-def files using version r403.

    Is this a bug?

    Thank you.

  • You're right, it looked like the definition for runtime.sdk was missing. I've added that for the next beta.

    In future you can file any such similar issues to the issue tracker.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)