Page MenuHomeFeedback Tracker

[Feature Request] Optional bool flag for getVariable to set the default value
New, NormalPublic

Description

Is it possible to have an optional bool flag for getVariable to set the variable automatically if it doesn't exist?

_obj getVariable ["var", default, setIfDoesntExist]

It would be very useful when working with array references, since you don't have to use setVariable anymore:

_arr = _obj getVariable ["my_array", [0, 0], true];
_arr set [1, _newValue];

Details

Severity
Feature
Resolution
Open
Reproducibility
N/A
Operating System
Windows 10 x64
Category
General

Event Timeline

Leopard20 created this task.Feb 8 2023, 6:55 PM
MDCCLXXVI added a subscriber: MDCCLXXVI.EditedFeb 11 2023, 8:26 AM

a variable counterpart for "getOrDefaultCall" would be fantastic

_vehicle = createVehicle ['B_MRAP_01_F',[0,0,0]];

_displayname = _vehicle getVariableCall ['displayname',{(getText ((configOf _vehicle) >> 'simulation'))}];

systemchat str _displayname  // "Hunter"
systemchat str (_vehicle getvariable 'displayname')	// 	"Hunter"
SaMatra added a subscriber: SaMatra.Jan 4 2024, 7:18 AM

Here is a suggestion on how getVariableCall can be incorporated into existing command with backwards capability and also address original ticket idea:

getVariable ["var", {123}] // Return {123} code if "var" is not found
getVariable ["var", {123}, false] // Call {123} if "var" is not found and just return it
getVariable ["var", {123}, true] // Call {123} if "var" is not found, write into "var" and return it

Perhaps it can also work with non-code 2nd argument and true 3rd argument. You'll need code returning code if you'll want to default set the code though.

POLPOX added a subscriber: POLPOX.Jan 4 2024, 7:39 AM
mrzorn added a subscriber: mrzorn.Mar 29 2024, 4:02 PM
mrzorn added a comment.EditedMar 30 2024, 12:21 PM

additional from T180211

when getVariable would recieve the additional pramater of setDefault, it might be worthwhile adding a 4th parameter that is equal to
setVariable's "public" parameter.

setVariable:

varspace setVariable [name, value, public]

public - (Optional, default false) can be one of:

Boolean - if set to true, the variable is broadcast globally and is persistent (JIP compatible) GEGlobal
Number - the variable is only set on the client with the given Machine network ID. If the number is negative, the variable is set on every client except for the one with the given ID.
Array of Numbers - array of Machine network IDs

This parameter is only available if the varspace parameter is a Namespace, Object or Group. Furthermore, only the following Data Types can be broadcast: [...]