How do I use OnPropertyChanged()

0 favourites
  • 9 posts
  • I'd like to use the OnPropertyChanged() function in instance.js to then clamp the properties values in the editor - how can I do this? Thanks.

  • Just call SetPropertyValue inside that method if the value is invalid.

  • Great, thanks Ashley .

  • Ashley - maybe I am misunderstanding this. If I enter the following:

    OnPropertyChanged(id, value)
    {
    	this.SetPropertyValue(id, 2);
    }

    Then nothing changes in the editor. In this example I was expecting the value to be clamped to "2". What am I missing? Thanks.

  • Did you check the console? That will be crashing because the method does not exist there. It's on IObjectInstance, which you normally access from your IInstanceBase-derived instance class with this._inst.

  • Ashley - yes, of course, there's nothing in the console. Neither of the following do anything when called inside OnPropertyChanged():

    this.SetPropertyValue(id, 2);
    
    this._inst.SetPropertyValue(id, 2);
    

    If I add an undeclared variable inside the OnPropertyChanged(){} then that doesn't highlight an error in the console either - it's as if OnPropertyChanged is not being called. This is a behavior rather than a plugin - could that make a difference (no reason to think so, I expect)? The undeclared variable with no console error makes me suspect that this might be an editor bug, but I could be missing something hugely obvious...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm sure it works - several official features use it. E.g. try setting the text object's font size to -1 and it clamps it to 0.1. I can't tell why it's not working for you, I guess I'd need to see real running code.

  • I was wondering about this too and tried it, I can get it to work with a plugin, but not with a behavior (it looks the the OnPropertyChanged function is not called when changing the property of the behaior on an instance.

    Here's the code from the top-level instance.js (using the SDK behavior example to test from here: construct.net/en/make-games/manuals/addon-sdk )

    "use strict";
    
    {
    	const BEHAVIOR_CLASS = SDK.Behaviors.MyCompany_MyBehavior;
    	
    	BEHAVIOR_CLASS.Instance = class MyCustomBehaviorInstance extends SDK.IBehaviorInstanceBase
    	{
    		constructor(sdkBehType, behInst)
    		{
    			super(sdkBehType, behInst);
    		}
    		
    		Release()
    		{
    		}
    		
    		OnCreate()
    		{
    		}
    		
    		OnPropertyChanged(id, value)
    		{
    			console.log("***INFO*** id:"+id+" value:"+value);
    			debugger
    		}
    		
    		LoadC2Property(name, valueString)
    		{
    			return false;		// not handled
    		}
    	};
    }
    
  • Thanks for confirming this Mikal

    Bug report logged here.

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