[engine] Prevent assigning values to a variable called: null
Moderator: Forum Moderators
Forum rules
Before posting a new idea, you must read the following:
Before posting a new idea, you must read the following:
- Pentarctagon
- Project Manager
- Posts: 5533
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
[engine] Prevent assigning values to a variable called: null
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: [engine] Prevent assigning values to a variable called:
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.
- Pentarctagon
- Project Manager
- Posts: 5533
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: [engine] Prevent assigning values to a variable called:
Partially because actually assigning a value to
It's also not like there aren't other "very special exception" variable names already.
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: [engine] Prevent assigning values to a variable called:
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.
Re: [engine] Prevent assigning values to a variable called:
Check if exists seems like something that could be key for [variable].
- Pentarctagon
- Project Manager
- Posts: 5533
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: [engine] Prevent assigning values to a variable called:
I might be missing something, but I don't think that solves the problem. If I am checkinggfgtdf 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.
$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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: [engine] Prevent assigning values to a variable called:
well if you used lua you wouldn't nned to use null.Pentarctagon wrote:I might be missing something, but I don't think that solves the problem. If I am checkinggfgtdf 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.$null
to see if a variable exists or not, and them someone else's add-on setsnull
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.
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.
- Pentarctagon
- Project Manager
- Posts: 5533
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: [engine] Prevent assigning values to a variable called:
Ooh, right, my badgfgtdf wrote:well if you used lua you wouldn't nned to use null.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 setsnull
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: [engine] Prevent assigning values to a variable called:
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.Ravana wrote:Check if exists seems like something that could be key for [variable].
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
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
Re: [engine] Prevent assigning values to a variable called:
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
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."
- Pentarctagon
- Project Manager
- Posts: 5533
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: [engine] Prevent assigning values to a variable called:
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 variableSapient 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
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
take one down, patch it around
-2,147,483,648 little bugs in the code
- Celtic_Minstrel
- Developer
- Posts: 2166
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: [engine] Prevent assigning values to a variable called:
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