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

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

Moderators: Forum Moderators, Developers

Forum rules
Before posting a new idea, you must read the following:

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

Postby Pentarctagon » May 20th, 2017, 6:30 pm

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
Pentarctagon
Forum Administrator
 
Posts: 2948
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

Postby zookeeper » May 20th, 2017, 7:01 pm

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
zookeeper
WML Wizard
 
Posts: 9610
Joined: September 11th, 2004, 10:40 pm
Location: Finland

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

Postby Pentarctagon » May 20th, 2017, 7:13 pm

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
User avatar
Pentarctagon
Forum Administrator
 
Posts: 2948
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

Postby gfgtdf » May 20th, 2017, 7:14 pm

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.
gfgtdf
Developer
 
Posts: 910
Joined: February 10th, 2013, 2:25 pm

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

Postby Ravana » May 20th, 2017, 7:19 pm

Check if exists seems like something that could be key for [variable].
User avatar
Ravana
Moderator
 
Posts: 1459
Joined: January 29th, 2012, 12:49 am
Location: Estonia

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

Postby Pentarctagon » May 20th, 2017, 8:07 pm

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
User avatar
Pentarctagon
Forum Administrator
 
Posts: 2948
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

Postby gfgtdf » May 20th, 2017, 8:48 pm

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.
gfgtdf
Developer
 
Posts: 910
Joined: February 10th, 2013, 2:25 pm

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

Postby Pentarctagon » May 20th, 2017, 9:06 pm

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
Pentarctagon
Forum Administrator
 
Posts: 2948
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

Postby WhiteWolf » May 20th, 2017, 9:22 pm

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]
Author of the Underness Series, consisting of V parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
Standalone works: The Ravagers - now published!
User avatar
WhiteWolf
Forum Regular
 
Posts: 330
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

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

Postby Sapient » May 21st, 2017, 4:51 am

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
Sapient
Developer
 
Posts: 4363
Joined: November 26th, 2005, 7:41 am

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

Postby Pentarctagon » May 21st, 2017, 6:58 am

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
Pentarctagon
Forum Administrator
 
Posts: 2948
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

Postby Celtic_Minstrel » May 28th, 2017, 6:39 am

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.
Maintainer of Steelhive.
User avatar
Celtic_Minstrel
Developer
 
Posts: 769
Joined: August 3rd, 2012, 11:26 pm


Return to Ideas

Who is online

Users browsing this forum: No registered users and 4 guests