Jump to content
🛡️FORUMS ARE IN READ-ONLY MODE Read more... ×
Sign in to follow this  
BomboBombom

setVariable,getVariable problems

Recommended Posts

I've been trying to use setVariable and getVariable to save variables on units, I've not figured out how to return the values or how to turn them into arrays. I checked someones script out and adapted the script according to see if it would work. But even with changes row 6 has errors.

The first time I had it set differently but it returned nothing at all.

func_TrackDie =
{
  private ["_die","_kil"];
  _die = vehicle (_this select 0);
  _kil = name (_this select 1);
  _dies = _dies setVariable["PN_Fired",_kil,"PN_Died",_die];
  _text = text format ["%1 VS %2 - %3 - %4",_kil,_die,_dies getVariable "PN_Fired",_dies getVariable "PN_Died"];
  _text = text format ["%1 - %2",_text,_dies getVariable ["PN_Fired","PN_Died"] select 0];
  _text = text format ["%1 - %2",_text,_dies getVariable ["PN_Fired","PN_Died"] select 1];
  hint _text;
};

I've executed this function on a tractor. Need arrays on units to get gun and what ammo or w/e I need to store on them. Can't make it work

~ cheers.

Share this post


Link to post
Share on other sites

Hi... checked these out yet?... Tells you exactly how to use them.

http://community.bistudio.com/wiki/setVariable

http://community.bistudio.com/wiki/getVariable

Surely you can see on that page for setvariable that your syntax is wrong!

_dies = _dies setVariable["PN_Fired",_kil,"PN_Died",_die];

versus...

_myTruck setVariable ["myVariable", 123, true]; 

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

I have absolutely no idea about what you're trying to do here, but all that syntax looks extremely suspicious to me.

Have you checked the wiki?

setVariable's syntax is this:

objectName setVariable [name, value, (public)];

Thus this line doesn't make any sense to me:

_dies = _dies setVariable["PN_Fired",_kil,"PN_Died",_die];

And what's with the three successive _text = stuff?

Obviously only the last one is gonna be displayed?

Could you explain what your variables refer to?

Edit: Goddamn, twirly.

Share this post


Link to post
Share on other sites
....And what's with the three successive _text = stuff?

Obviously only the last one is gonna be displayed?

@Blackmamb :) Same exact thoughts.... but you added that bit about the text. The whole chunk of code is screwy!

Share this post


Link to post
Share on other sites

I'm trying to learn still and have been been trying to get this to work for a long time.

I always check wiki for help. Doesn't explain enough imo.

1. The client kept telling me that I needed to add a ] to the code. New errors came along eventually and I kept poking until I got no errors.

2. The wiki gives too little info for me to be able to use them. Doesn't give enough examples to explain how to use it in different ways.

Example for get "_aVariable = _myTruck getVariable "myVariable"" and set "_myTruck setVariable ["myVariable", 123, true];" are basic use examples.

The three _text variables are there to build a line to print at the end. Do not know why \n isn't working.

For the code :

 func_TrackDie =
{
  private ["_die","_kil"];
  _die = vehicle (_this select 0);
  _kil = name (_this select 1);
  _dies = _dies setVariable["PN_Fired",_kil,"PN_Died",_die];
  _text = text format ["%1 VS %2 - %3 - %4",_kil,_die,_dies getVariable "PN_Fired",_dies getVariable "PN_Died"];
  _text = text format ["%1 - %2",_text,_dies getVariable ["PN_Fired","PN_Died"] select 0];
  _text = text format ["%1 - %2",_text,_dies getVariable ["PN_Fired","PN_Died"] select 1];
  hint _text;
};

The script is called when the tractor is killed. It is supposed to print the killer, victim and then the array variable.

Now to explain each line;

_die = this select 0; (works instead of the vehicle one, it was a simple experiment to see if I got different info)

_kil = name (this select 1); returns the name of the unit killing the tractor.

_dies = _dies setVariable["PN_Fired",_kil,"PN_Died",_die]; sets PN_Fired to _kil and PN_Died sets the dieing tractor. As you explained it seems I've done it wrong. I assume it needs to be "PN_Fired",_kil,true,"PN_Died",_die,true instead.

the _text lines are for building a line of text, supposed to have line breaks in it for each line though. I was testing retrieving my variable and trying various methods and tried to get various variables out.

hint _text; prints the _text value once it is made.

I want to create an array rather than split values.

Cheers~~

Share this post


Link to post
Share on other sites

Yes mate.... but nothing is right there!

What is "_dies"???

and...

If you do something like this:-

_text = "one";

_text = "two";

_text = "three";

At the end of it _text is equal to "three"... the lines before don't come into it. You have assigned a new value to _text each time!

Following the example in the Biki...

_myTruck setVariable ["myVariable", 123, true];

_myTruck is a vehicle (an object). So you set the variable "myVariable" on the object to be equal to the number 123, The true/false part is whether or not you want the variable to be set on all machines or only locally (this machine).

After you set it... you can retrieve it like this...

_aVariable = _myTruck getVariable "myVariable"; 

Now _aVariable is equal to the number 123

If you want to set the variable to be an array.... go right ahead. Just make the array first... it will be less confusing.

_myArray = [_item1,_item2,_item3];
_myTruck setVariable ["myVariable", _myArray , true];

To retrieve it....

_anArray = _myTruck getVariable "myVariable";

_anArray will then be [_item1,_item2,_item3].... just as we stored it!

Hope that helps a bit.

Share this post


Link to post
Share on other sites

Alright. Not sure i got everything here, so i'll ask a few more questions and try and correct some stuff.

I'll have to agree with you on one point, sometimes (and especially at the beginning of the learning curve) the wiki isn't just enough. Only way to understand stuff is to experiment a lot, read a lot and try and deconstruct scripts made by others.

setVariable is basically used to apply a variable (one, actually) to a specific object. First thing, then, you cannot add two variables at the same time, which you're trying to do.

_myTruck = _myTruck setVariable ["variable1", value1, "variable2", value2] is what you do.

_myTruck setVariable ["variable1", value1];
_myTruck setVariable ["variable2", value2];

is what should be done.

The workaround is to do:

_myTruck setVariable ["Variable", [value1, value2]];

There I assigned only one variable to _myTruck. But that variable, named Variable, contains two values. Note that the expression does NOT contain any equal operator.

Now to the purpose of this. I don't pretend to know much about variables and coding and general, being rather new to this, but the goal here is to store values in a particular space (the one of the _myTruck object) so that we can easily retrieve them from anywhere.

For example, let's say I have ten trucks, for each truck i'll randomly set a variable that can take 0 or 1 as a value.

Then, later in another script, i'll retrieve that variable for each truck and remove the fuel from the trucks with a value of 1 (that is totally dumb, but that's an example of the use of setVariable/getVariable). The advantage being i don't have to create 10 diffrently name variables.

In your attempt, you try to set a variable to an undefined object (_dies) that you will retrieve on the next line. There is no point and that's impossible. Your code can be summarized this way:

hint format ["%1 killed %2", _this select 1, _this select 2]; 

Which is gonna have the exact same result.

I hope i didn't come out wrong and aggressive, and/or totally missed the point.

Edit: Twirly, i'm gonna hit you in the face with a rubber stick.

Btw, his text stuff does re-use the precedent one, so he actually adds something each time, and the last string depends on the first. I realized that afterwards. Though i guess most of it doesn't return much.

Edited by BlackMamb

Share this post


Link to post
Share on other sites

Edit: Twirly, i'm gonna hit you in the face with a rubber stick.

Lol!.. Between the two of us he'll get it sorted fuh sure.

Share this post


Link to post
Share on other sites

Sorry running an op, haven't been able to reply. Want to say thanks and I'll read this through and reply asap. :-)

Ok done.

@twirly: Thank you too! ~reading that one too~ Excuse my not seeing that post. :s

@BlackMamb: I've got a script for the finding who killed who, I was moding it to test the get and setVariable functions. The reason why I needed to learn the setVariable and getVariable is because I wanted to try and make a script that detects who killed who using a handgrenade and also to prevent it from logging a pistol in case of long range snipers switch weapon before their bullets hit the target. Thus I want to set "fired" value on the person shooting or throwing and then for the grenade try and determine that it was the grenade by the damage done and determining the sniper shot by the fired value.

I've got no clue how I will make all of this work yet, but I'm working on it as well as I can and all the help has been of great use. :-)

Original code for when players shoot each other is something like bellow.

func_Killed =
{
  private ["_die","_kil"];
  if (!isPlayer (_this select 0)) exitWith{};
//   _die = name (_this select 0);
//   _kil = name (_this select 1);
  hint format ["%1 killed %2",name (_this select 1),name (_this select 0)];
  _this removeMPEventHandler ["Killed",0];
};

- The above is just an example, the removempeventhandler is an attempt to optimize, not sure if the index is always 0. the "//"-lines are what I previously used in the code.

- I use a script for event "Fired" that saves gun and ammo used (ammo for throwing weapons). Not yet worked out how everything works so got some time till I can script all I want.

Cheers~

Edited by BomboBombom
Just and update

Share this post


Link to post
Share on other sites
Sign in to follow this  

×