Instance Variables and Extensions

I have an extension that needs to determine if a particular object takes up a square space.
I pass a shape to the extension, and in the extension I have the following condition:

Variable squareShape of Shape = 1

what happens if the object passed doesn’t have an instance variable named squareShape?

Will this cause issues when I export to HTML?


In GD if a variable does not exist, GD will go ahead and initialise it for you with the value 0 or empty string “” to avoid any runtime errors. So the condition you mention most likely going to return false. If you were checking if the value is 0 then it would return true I guess.

Everything should work the same as in the preview except in case of mobile specific features.

1 Like

Thanks for the information ddabrahim. Just wanted to make sure, because some languages handle variables differently.

Yes, In most languages and frameworks if you try to use a variable before it is being defined or initialized normally the game crash with a runtime error “variable is undefined”.
In order to avoid crash, GD initialize any missing variables during runtime with the value 0 or empty string which helps to avoid any crash. It is usually fine if you assign value to the variable but it can introduce some bugs when you miss type a variable name somewhere and you don’t notice it and you don’t assign the value, then when you compare the value or use it in expression you have a bug.

In order to avoid this crash and bugs what most modern code editors do is let you know and highlight it if a variable is defined but never used (yellow) or used but not defined, not initialized (red). Instead of trying to fix it during runtime, you get a notification in the IDE so you can fix it.

Maybe something like this could be useful in the event editor of GD to make sure people are aware what variables and objects they use and avoid bugs.