[engine] Prevent assigning values to a variable called: null

Brainstorm ideas of possible additions to the game. Read this before posting!

Moderator: Forum Moderators

Forum rules
Before posting a new idea, you must read the following:
Post Reply
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

[engine] Prevent assigning values to a variable called: null

Post by Pentarctagon »

The title pretty much says it all: I think it makes sense to prevent any value from ever being assigned to a variable called null. This would also allow a bit more sensible answer to be provided when the question "How do I check if a variable exists?" and its variants are asked over in the WML Workshop.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: [engine] Prevent assigning values to a variable called:

Post by zookeeper »

Maybe I'm being dense, but how would that actually help in answering that question? I mean, instead of telling someone that you just use a made-up variable name that doesn't exist, you'd need to tell them to use "null" because that one can't exist because there's a very special exception in the engine for it and you really need to remember to not try to use it in a normal manner.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: [engine] Prevent assigning values to a variable called:

Post by Pentarctagon »

Partially because actually assigning a value to null doesn't make a whole lot of sense anyways; and partially because there's no way to know for certain what variables are and aren't being used, since add-ons from multiple people can be used at the same time, so even if I know I don't ever assign something to a particular variable name in my code, code in someone else's era, scenario, modification, etc might. Obviously that's a more general potential problem, but in this case at least it seems like it's a relatively common thing to be done and there's a straight forward way to allow it to be done consistently.

It's also not like there aren't other "very special exception" variable names already.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: [engine] Prevent assigning values to a variable called:

Post by gfgtdf »

in 1.13 you can define custom conditionalwml tags, so you shodul be able to create a variable_exists condition that compares the lua value to nil.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [engine] Prevent assigning values to a variable called:

Post by Ravana »

Check if exists seems like something that could be key for [variable].
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: [engine] Prevent assigning values to a variable called:

Post by Pentarctagon »

gfgtdf wrote:in 1.13 you can define custom conditionalwml tags, so you shodul be able to create a variable_exists condition that compares the lua value to nil.
I might be missing something, but I don't think that solves the problem. If I am checking $null to see if a variable exists or not, and them someone else's add-on sets null to 0 or "" or -999 or some other value, my code will no longer work regardless of if I check the value with lua or WML.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: [engine] Prevent assigning values to a variable called:

Post by gfgtdf »

Pentarctagon wrote:
gfgtdf wrote:in 1.13 you can define custom conditionalwml tags, so you shodul be able to create a variable_exists condition that compares the lua value to nil.
I might be missing something, but I don't think that solves the problem. If I am checking $null to see if a variable exists or not, and them someone else's add-on sets null to 0 or "" or -999 or some other value, my code will no longer work regardless of if I check the value with lua or WML.
well if you used lua you wouldn't nned to use null.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: [engine] Prevent assigning values to a variable called:

Post by Pentarctagon »

gfgtdf wrote:
Pentarctagon wrote:I might be missing something, but I don't think that solves the problem. If I am checking $null to see if a variable exists or not, and them someone else's add-on sets null to 0 or "" or -999 or some other value, my code will no longer work regardless of if I check the value with lua or WML.
well if you used lua you wouldn't nned to use null.
Ooh, right, my bad :oops:
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: [engine] Prevent assigning values to a variable called:

Post by WhiteWolf »

Ravana wrote:Check if exists seems like something that could be key for [variable].
I'd find that really useful. The usecase in my case is when a new feature based on a variabe is introduced, that is being handled by f.e. a menu item, say, introduced in the first scenario.
Then, a default behaviour should be provided for older saves of later scenarios. But, it must be made sure that the default behaviour doesn't override the player-selected newer cases.
For example:

Code: Select all

# scenario ten
# for newer players, new_feature can be selected between default.png and option2.png (etc...) with a menu item defined in scenario one
# for older saves, nothing would be shown, when $new_feature is used
# To prove compatibility:

[event]
name=preload

[if]
[variable]
name = new_feature
if_exists = no
[/variable]
[then]
{VARIABLE new_feature default.png}
[/then]
[/if]
[/event]
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: [engine] Prevent assigning values to a variable called:

Post by Sapient »

If I were going to create such a variable, I would call it $empty and have it always evaluate to empty string.

In WML, there is no concept of a null variable, nor do I think it should be introduced. An empty variable and an unused variable are intended to be equivalent in WML.

For this same reason, I would rather see [variable] empty=yes instead of [variable] exists=no
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: [engine] Prevent assigning values to a variable called:

Post by Pentarctagon »

Sapient wrote:If I were going to create such a variable, I would call it $empty and have it always evaluate to empty string.

In WML, there is no concept of a null variable, nor do I think it should be introduced. An empty variable and an unused variable are intended to be equivalent in WML.

For this same reason, I would rather see [variable] empty=yes instead of [variable] exists=no
That works too :) . I'm less concerned about the exact details, as trying to get a consistent way to check if a variable has had a value assigned to it or not with WML. gfgtdf's idea works as well, and is what I'll do in my own code if this idea doesn't go anywhere, but I still think having a variable null or empty would be more obvious for people less familiar with WML/lua.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: [engine] Prevent assigning values to a variable called:

Post by Celtic_Minstrel »

Since the [set_variable] tag is now implemented in Lua in 1.13, it'd be easy to extend it (even in your addon!) to add the ability to test if the variable exists. For example, if you want "only_if_not_exists=yes" which does nothing when the variable already exists, you could do something like this:

Code: Select all

local old_set_variable = wesnoth.wml_actions.set_variable
function wesnoth.wml_actions.set_variable(cfg)
    if cfg.only_if_not_exists and (not wesnoth.get_variable(cfg.name) or wesnoth.get_variable(cfg.name) == '') then
        return
    end
    old_set_variable(cfg)
end
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply