• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

 Make uninitialized variables undefined.

GMWolf

aka fel666
This would make things much easier.
Rather than having to write
Code:
var a = undefined;
var b = undefined;
//etc
You could just do
Code:
var a,b,c,d,e;
Many algorithms assume you start with a a value of undefined, so this would be helpful.
 

Jobo

Member
GMC Elder
You don't want default value assignment on every variable you declare. There's also no use in this (that I can find??), and having algorithms that require undefined parameters makes no sense??? What would it do, if(argument0 != undefined) do_crash()? I don't see how this provides any sort of benefit to anyone.
 

GMWolf

aka fel666
You don't want default value assignment on every variable you declare. There's also no use in this (that I can find??), and having algorithms that require undefined parameters makes no sense??? What would it do, if(argument0 != undefined) do_crash()? I don't see how this provides any sort of benefit to anyone.
I remember there being quite a few algorithms that need variables initialized as null (undefined) from my algorithm lectures. Will go find them if you like.
Its not to do a 'do_crash()' but to act as sentinel values.

I also think its aproblem right now that you cannot check if a value has been initialized or not. Every other language I know off uses NULL, or 0 for integers. (aside from C. but C isnt a super friendly language)
 
Last edited:

Jobo

Member
GMC Elder
The only times you use var in GML is when you're in a local scope, and that means you're gonna use that variable so you don't want an intermediary value you're just gonna overwrite. Assign it to -1 if you want to check if you've overwritten it later, like when finding an index in an array.

I also think its aproblem right now that you cannot check if a value has been initialized or not.
Like I said, when you write the code you know if you initialized it or not. It's not a runtime thing.
 

GMWolf

aka fel666
Well, not always. And -1 is not optimal when the solution to an algorithm can be -1.

The only possible solutions then are a null value or virtual initialization.
I doubt most GM users even know what the later is, so undefined seems most sensible.
 

GMWolf

aka fel666
Then make it -2 :)
*can be any integer, +ve or -ve*

To be fair, its pretty trivial to initialize them as undefined, so its not needed.
But its seems strange to have both undefined and uninitialized. Would be nice to start unifying.
Much like we have undefined and noone, when the behaviour of each could be gathered under undefined.
 

Mike

nobody important
GMC Elder
The benefit of having to force you to write to a variable is that you don't start using the ones you've not setup yet by mistake. In the long run, doing initialisation properly will save you time and loads of bug hunting.
 
M

Max Terminus

Guest
I have found myself in a similar situation:

I need to perform an activity once, for instance, a procedurally generated sprite that would remain constant once generated. There are some functions I need to use that can only occur in a draw event, instead of the create event where I would normally conduct activities which need to occur once. Because of these restrictions, artificial or not, the procedure to be run once must appear in every draw event.

I understand that I can create a variable in the create event that can be tested in the draw event and set in the script I need to run once. However, if "undefined" truly meant not (yet) defined, then the script that must be called every time, but execute once can be self-contained, with initial code:

if !is_undefined( need_this_very_specific_operation) exit;
need_this_very_specific_operation =false;

A simple, elegant, self-contained solution, available when is_undefined() doesn't require the item being tested to have been already defined. To have to "define" something as "undefined" is mind twisting, and defeats the ability to keep a function self-contained.
 
Last edited by a moderator:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
variable_instance_exists allows to check if an instance' variable exists and would seem like it covers your case entirely.

As per original sample, if you want something shorter, make a macros for it
Code:
#macro null undefined
(semantically, common language' null is exactly same as undefined in GML)

My 50 cents, but having undefined variables not error out is a horrible idea - both JS and Lua suffer heavily from this.
 

GMWolf

aka fel666
My 50 cents, but having undefined variables not error out is a horrible idea - both JS and Lua suffer heavily from this.
undefined does error out - at least, for anything that cannot accept undefined.
Much like NULL in other languags.

What im saying is that we have some pretty useless redundancy: undefined and uninitialized. Why have both? (I cant think of another language that does this, they all use null, 0, or whatever is in memory for languages like C).
 
A

Ampersand

Guest
You don't want default value assignment on every variable you declare. There's also no use in this (that I can find??), and having algorithms that require undefined parameters makes no sense??? What would it do, if(argument0 != undefined) do_crash()? I don't see how this provides any sort of benefit to anyone.
Actually this would be really nice. It's the same way global variables are currently handled.
 
M

Max Terminus

Guest
Thanks, I have been using variable_instance_exists() to good effect. Part of the difficulty is the paucity of inter-connections in the manual and partly the Orwellian definition of is_undefined(). That a variable must first be "defined" as "undefined" in order to pass is_undefined() requires "double-think" as in the book 1984.

I began programming professionally over 40 years ago, and I can assure you that "undefined" has always meant "has not been defined in this scope." If someone wants to change the meaning, and allow alternate ways to achieve the desired result, at least recognize the very strong likely-hood of misunderstanding surrounding this change in traditional definitions and make enough cross-references in the manual to allow a programmer to find what he needs to use. The various implementations of the concepts of exists, null, uninitialized, undefined, and defined as undefined need to be correlated.

Note that at the time of this post, the reference page in the manual on variable_instance_exists() and variable_global_exists() both define undefined in the traditional sense of never having been defined, instead of having been defined as undefined, as used elsewhere in the manual, sadly with no cross-references between them. The confusion on this implementation's interpretation of these concepts is valid, as the manual itself is inconsistent.
 
Top