HereIsJones 20 Posted July 6, 2021 Noob question, what is the difference between this... _myTruck setVariable ["myTruck", 123]; ...and this? _myTruck setVehicleVarName "MyTruck"; "MyTruck" = _myTruck; Can both be used to do the same thing? If there's a decent writeup somewhere I'd love a pointer. Thanks! Share this post Link to post Share on other sites
sarogahtyp 1108 Posted July 6, 2021 Answer is no. But you should ask your real question and not posting nonsense pseudo code. Just say what u want to achieve and ask for solutions of the problems you discover. Share this post Link to post Share on other sites
HereIsJones 20 Posted July 6, 2021 I don't want code written for me, I'm trying to get a conceptual understanding. Don't hijack my thread. Does anyone know of a good writeup on this? Thanks! Share this post Link to post Share on other sites
sarogahtyp 1108 Posted July 6, 2021 Hijacking? What exactly is the problem you have? You are posting nonsense pseudo code. I helped you by telling you what kind of code you wrote and you call me a hijacker? Read the biki! Share this post Link to post Share on other sites
pierremgi 4886 Posted July 6, 2021 _myTruck setVehicleVarName "MyTruck"; // not enough to pass mytruck as the variable name of the truck. See link, read BIKI. missionNameSpace setVariable ["myTruck", _myTruck]; // mytruck, as global variable refers to _mytruck (the truck) (in SP here) "MyTruck" = _myTruck; // no sense. The string is not a variable 1 Share this post Link to post Share on other sites
HereIsJones 20 Posted July 6, 2021 Okay, I think I'm slowly understanding this. I found this old post (10 years old) and am hoping it's still correct. Is it (in simple terms) that setVariable creates variables associated with an object, and setVehicleVarName sets a variable that points to the object itself? Share this post Link to post Share on other sites
sarogahtyp 1108 Posted July 6, 2021 setVariable creates a variable which gets applied to a namespace. That namespace can be the missionnamespace, an object or a group or other nsmespaces like described in its biki entry. setVarName creates a name only. Its the string representation of an object. You can also create a variable of it as described in example 1 of its biki entry. Its essential to read those biki entries to understand it and also to try those examples maybe in debug console. 1 1 Share this post Link to post Share on other sites
pierremgi 4886 Posted July 6, 2021 I was writing the same answer. nameSpace. 1 Share this post Link to post Share on other sites
HereIsJones 20 Posted July 6, 2021 Yeah I think that's my problem, I don't fully understand nameSpace yet, so that's where I'll start. Thanks! Share this post Link to post Share on other sites
Twiznak 57 Posted July 6, 2021 7 hours ago, HereIsJones said: Noob question, what is the difference between this... _myTruck setVariable ["myTruck", 123]; ...and this? _myTruck setVehicleVarName "MyTruck"; "MyTruck" = _myTruck; Can both be used to do the same thing? If there's a decent writeup somewhere I'd love a pointer. Thanks! Hello! I can help you 😎 The difference between the two codes you have presented, is in the understanding of a variable, a script handle && the commands setVariable, publicVariable and missionNamespace . Lets use the examples provided to us by the Bohemia interactive wiki to fully explain the answer to the question you are trying to ask. We will create a truck with createVehicle , with a script handle of _myTruck, then define that script handle with a global scope variable . The purpose being, that your truck's global variable can be further acted upon with other scripts throughout the mission. I will then explain the syntax error in your examples. Example 3 from the createVehicle BIwiki: _myTruck = createVehicle ["B_Truck_01_mover_F", position player, [], 0, "FORM"]; myTRUCK = _myTruck; publicVariable "myTRUCK"; For the purposes of manipulating a created object in the short term, you should use publicVariable to redefine your local variable name (_myTruck) as the global variable name (myTruck). setVariable is more powerful of a command than publicVariable, as it is most often used in conjunction with missionNamespace. As such, you should use missionNamespace setVarible for persistent numerical based values. That can be referenced later, as true or false conditions for activating triggers, addActions and much more. Now for your examples. They return nil for your purposes because "myTruck" is the missionNamespace variable that is being defined as 123. However the local variable of _myTruck has no predefined variable of myTruck associated with it. If we used the following in the debug console to check the variable myTruck it would return "123". This is not what you need, to do what you want. missionNamespace getVariable "myTruck"; Please ask all the questions. We are happy to help here on the forums. We will give you more working examples of how to use missionNamespace setVariable to achieve more complex results, if your interested. Just let us know, Ciao! 2 1 Share this post Link to post Share on other sites
pierremgi 4886 Posted July 7, 2021 Well... @HereIsJones Global... public... needs a little bit more explanation. Based on Twiznak remarks: 1. If you stay on a local scope, you can work with a local variable. For example, you create a vehicle on server, then add a crew than add waypoints. No need to make the variable global (known for any scope/script/sqf.. in the mission) , and furthermore no need to make it public (known by all clients/server, roughly) The notion of scope is important here. See Twiznak's global scope variable link. Spoiler The common mistake is the if () then {_myLocalVarHere = something}; then attempting to use _myLocalVarHere further in script. This variable is not defined outside the condition scope if not defined before! 2. If you need to re-use a variable in a different script/sqf, in single player, or for specific local scripts, so make it global (or pass it as parameter as shown below in summary). Note: You can save line of code without writing a local variable then a global one for the same thing. Just myTruck = createVehicle.... and you don't need to public them for nuts. You must make the difference between: - the variable myTruck which allows you to work on it (here in global, so in all scripts of the mission, as far as the variable is existing of course) - the created object by createVehicle. This command is Effect Global (the vehicle is on all PCs), no matter the variable handle is local, global or public. - the name of the object like you could write in editor, by setVehicleVarName. Summary: 1 - _myTruck = createVehicle... // on a (server or local) script does the trick. Create also crew, add waypoints... even pass that as parameter: _myTruck execVM "atruck.sqf"; // the best solution, most of the time, like spawning AI units/vehicles on server. Coding with local variables is faster than with global ones, if multiple occurrences. 2 - myTruck = createVehicle... // you can work with myTruck everywhere you need it on PC (no need to pass it as parameter, just: execVM "aTruck.sqf" for example). If you are sure this variable is to be known by client scripts, public it by publicVariable (you can avoid that most of the time!) 3 - add a name, specific to the object/vehicle like in editor: _myTruck setVehicleVarName "truck1"; in single player. This command is Effect Local (so the name is known only on PC which run the code). In MP, remoteExec it: [_mytruck,"truck1"] remoteExec ["setVehicleVarName",0,_truck]; but you need also to set the variable truck1 as reference for the object _mytruck. So, in SP: missionNameSpace setVariable ["truck1",_myTruck]; or: truck1 = _myTruck; in MP, simple as: missionNameSpace setVariable ["truck1",_myTruck,TRUE]; // I'd rather choose this way (habit) or: truck1 = _myTruck; publicVariable "truck1"; 2 2 Share this post Link to post Share on other sites
HereIsJones 20 Posted July 7, 2021 Ok I'm getting this now. I'm building a kind of "quest-based" system for a map I'm working on, and it looks like the mission namespace might be a good place to organize and store "quest" states. I just figured out how to implement "function libraries" 🙂 so I can figure this out. I'll test it today. I have one last simpleminded question, which is at the heart of why I'm confused about this (apologies if it was explained above, I'm dense) - why would you use setVariable in missionNamespace instead of just setting a global variable? What is the benefit of it? It seems like it's about localization. I'm guessing using namespace keeps things more organized as well. Thanks! Share this post Link to post Share on other sites
sarogahtyp 1108 Posted July 7, 2021 There are no big differences. The situations where I use setVariable in missions namespace are when I want to propagate the variable to all clients or if i want to dynamicaly set the name of variables. Because setVariable takes a string as argument you are able to first build the wanted string dynamicaly and then create a variable of it using setVariable. But its much more common to use other namespaces like objects to store states or properties on them. You could store the owner player, the creation time, the buying price or whatever u want on a car object for example. 1 1 Share this post Link to post Share on other sites