JDog 11 Posted July 27, 2009 I'm trying to get the damage a character receives and save it as a local variable but whatever I do I can't get it to change. Might be a scalar vs. integer conflict? Here's what I'm workin with: _damage = 0; player addeventhandler ["dammaged", { _damage = (_this select 2) } ]; hint format ["%1", _damage]; It always returns "0" in the hint. If I use a hint to check right from the EH, it returns the proper damage value, like so : caller addeventhandler ["dammaged", {(_this select 0) setdamage 0;hint format ["%1", (_this select 2)]; } ]; Just to be clear I don't want it printed as a hint, just using that for debugging purposes. I want to be able to save the damage into _damage so I can toy with it. Any suggestions? Share this post Link to post Share on other sites
f2k sel 145 Posted July 27, 2009 I had this problem last week you need to replace the _damage variable with damaged no underscore as I believe it will only output using a global variable and the word damage is a command. Share this post Link to post Share on other sites
JDog 11 Posted July 27, 2009 Well in terms of "damage" being a command, "_damage" still works fine if you use that word as a local variable, I have before. But yes thanks for the input, I'll try using a global variable instead when I get the chance. Share this post Link to post Share on other sites
f2k sel 145 Posted July 27, 2009 Yea that's what I was trying to say, after you remove the underscore it becomes a command and will need changing. But you got it anyway. Share this post Link to post Share on other sites
Taurus 20 Posted July 27, 2009 Hello. Think of the event handler as what it is. A handler for an event, or event listener.(you get my point) if you want to play around with it either make a global variable as mentioned, however you should avoid calling it damage as that's an command, it may conflict. or, execVM/spawn/call a script/script/function each time the event is handled(triggered) player addeventhandler ["dammaged", { [_this select 2] execVM "myScript.sqf"} ]; or player addeventhandler ["dammaged", { nil = [_this select 2] spawn myScript} ]; You'd need to compile the script first tho' For example, in init.sqf myScript = compile preProcessFile "myScript.sqf"; myScript.sqf private ["_dmg"]; _dmg = _this select 0; hint format ["Dmg=%1",_dmg]; //do rest of stuff below Share this post Link to post Share on other sites
JDog 11 Posted July 27, 2009 Thanks Taurus. However in this case I will try and see if global variables work first. I'm actually just trying to make some system in the game from scratch so I can get a little used to scripting in ArmA2, the eventHandlers, and making my own functions. Kinda like a "make this to get comfortable with it" kinda deal :) Anyway, reason is cuz I don't wanna make another file haha. This thing already consists of 5 files, two which are only a few lines but whose commands only execute if they're called and passed values. I'm actually already using a few global variables in this anyway because I encountered a problem using a while loop and couldnt get it figured out "properly", somethin like this didn't work: _value1 = _this select 0; while{_value1 == "on"}do{ //do stuff in here }; if(_value1 == "off")then{ //do other stuff }; If it was executing the "on" loop... and I had another file pass "off" to this script... it would react and do whatever is in the "off" loop, however it would keep on doing whatever is in the "on" loop also, it was just locked repeating inside that loop since it never left to see if _value1 was updated to "off". I guess it would run a separate instance of the script whenever it was called again. I suppose I could have corrected that by terminating the script and re-executing it, but the script holds and keeps track of an important local (at the time) variable that changes often, and resetting the script would reset the variable. Setting everything to global variables fixed it for me... since I couldn't figure out if there was a "get"-type command to place inside the while loop to see if _value1 got changed or not. Share this post Link to post Share on other sites
JDog 11 Posted July 28, 2009 hm here's an easy one for ya... does "exit;" work in SQF? cuz it sure isnt working for me. worked around, jw what I should use since that doesnt exit the script. ---------- Post added at 10:15 PM ---------- Previous post was at 09:54 PM ---------- ok another one, sorry... hopefully this is simple too. I can't get the longer version of addAction to work. The following will not execute or add an action. It just skips over this statement. _newAction = _object addAction ["Deactivate", "scripts\deactivate.sqf", "", 1, false, true, "", true]; ---------- Post added at 10:23 PM ---------- Previous post was at 10:15 PM ---------- nevermind, seems as long as I leave out the last argument it works. too bad, i would've liked to use that one in the future, as it's the condition as to whether or not the action is shown. there's a workaround for that too, tho. ---------- Post added at 10:54 PM ---------- Previous post was at 10:23 PM ---------- Ok I know I'm posting here a CRAZY amount, but there are TWO things stopping me from finishing my script I'm writing. One I'm not worrying about til the end, cuz I can't even check to see if it works until I get this one done. Current issue: I can't get out of a while loop, any suggestions for this? I intentionally want the loop to continue for the life of the object, as it'll be holding some local variables that need to be kept track of even when it isn't doing anything. while{alive _object}do{ if(_condition == "on")then{ //this stuff works }; while{_condition == "on"}do{ if(_power > 0)then{ nul=[_power*100] execVM "scripts\subscript.sqf"; [b] //here is my problem. i need "_condition" to be //updated but it gets updated from an Action //which runs a seperate script. I need the other script //to somehow change a value in here, or have this //check the value in that script[/b] }; if(_power == 0)then{ //this stuff works and has a statement to change //"_condition" so it exits the loop when needed }; }; [b]//could place here[/b] }; So again, the issue is I need this script to be able to know when "_condition" turns to false in another script. It's local in both, so I need to transfer it somehow into this one. So is there a "get" type command in SQF for retrieving a variable's value in another script/function? Or even possible to define a function later in this script and call it from outside this script? (so I don't have to use an external file and the variable i want gets updated within). Sorry, I'm sure that's all confusing, but it makes sense if you try real hard :) PS. I can't use a global variable because then it wouldn't be compatible with more than 1 person. Share this post Link to post Share on other sites
Murklor 10 Posted July 28, 2009 If it was executing the "on" loop... and I had another file pass "off" to this script... it would react and do whatever is in the "off" loop, however it would keep on doing whatever is in the "on" loop also, it was just locked repeating inside that loop since it never left to see if _value1 was updated to "off". I guess it would run a separate instance of the script whenever it was called again. Of course its another instance... Every time you run a script its a new instance. So that's expected behaviour. When you run it as off, it get stuck in the second loop. When you run it as on, it will get stuck in the first loop. You cant change the value if its stuck in the first loop, unless you intend to make the on loop you ran get stuck in the off loop and have 2 off loops running. Etc and so on. A sidenote to a question in the post above: As I discovered lately, exit is sqs only. exitWith {} works for sqf, but I think it only breaks the current loop (ie using it in deep wont quit the script). As for the variable question, have you tried using a global variable? If you want it to be visible on all clients, use publicvariable. Just set it to something in the second action and check it in the first action. You probably want a sleep in there btw, unless you want to get a gazillion instances of subscript... Looks to me like it should be a function instead of a executed script. Share this post Link to post Share on other sites
JDog 11 Posted July 28, 2009 Thanks for the input Murklor. The script doesn't re-execute or anything, like it doesn't start copies of itself unless I try to execute it again issuing an "off" instead of "on". I already made a working version of this script using global variables... but then it's incompatible with more than 1 unit, since each unit running will have their values change differently and at different times in the script. If I can't "fetch" a variable from one script into another, is it possible to define a variable that's only accessible from files in a certain folder or filenames you dictate? Like... global only to these script files themselves and not the game? All I'm trying to do is get it to work for more than 1 entity. Share this post Link to post Share on other sites
Big Dawg KS 5 Posted July 28, 2009 I can't get the longer version of addAction to work. The following will not execute or add an action. It just skips over this statement._newAction = _object addAction ["Deactivate", "scripts\deactivate.sqf", "", 1, false, true, "", true]; ---------- Post added at 10:23 PM ---------- Previous post was at 10:15 PM ---------- nevermind, seems as long as I leave out the last argument it works. too bad, i would've liked to use that one in the future, as it's the condition as to whether or not the action is shown. there's a workaround for that too, tho. The condition must be in string format (with ""s around it). ---------- Post added at 12:23 PM ---------- Previous post was at 12:21 PM ---------- I think you've got the wrong idea about the purpose of using local variables. If you want to save a state of say an object, use setVariable/getVariable instead. Share this post Link to post Share on other sites
JDog 11 Posted July 28, 2009 I tried using quotes Big Dawg, no go. I don't need that part though, it's fine. Also... I'm using local variables in the script so that each person who is running the script has their own version and isn't sharing variable values. Is that incorrect? And wouldn't setVariable be the same as just using = to assign a value? getVariable I assume can also only get from within the file? Share this post Link to post Share on other sites
Big Dawg KS 5 Posted July 28, 2009 You can use setVariable to add any variable to a unit/vehicle/object. If you're OO minded, think of them as member variables. Share this post Link to post Share on other sites
JDog 11 Posted July 28, 2009 Dear lord... Big Dawg I think you just saved me from certain giving-up-ness. Never knew objects had their own space reserved for variables you can set and get! This is quite possibly the best thing that's ever happened to me. I'm about to cry. I've seen the light, and it's so wondrous. Now I gotta rewrite a bit of the code for it to be nicer! Share this post Link to post Share on other sites
Big Dawg KS 5 Posted July 28, 2009 Dear lord... Big Dawg I think you just saved me from certain giving-up-ness. Never knew objects had their own space reserved for variables you can set and get! This is quite possibly the best thing that's ever happened to me. I'm about to cry. I've seen the light, and it's so wondrous.Now I gotta rewrite a bit of the code for it to be nicer! You should have been there when it was first introduced in ArmA. I think many tears were shed. Share this post Link to post Share on other sites
JDog 11 Posted July 28, 2009 Oh my god, you can even use it with addAction as the value!? HOW SEXY IS THAT! I'm gonna have so much fun with this. Share this post Link to post Share on other sites
JDog 11 Posted July 28, 2009 Well, that made a lot of stuff easier. However now I did something I apparently can't reverse and don't know what it is. And stupid ArmA Edit for some reason made ctrl+S start overwriting the ones in my backup folder instead of the ones i was working on. It's cool though I wanna start it from scratch again later and add more to it. The thing that was giving me trouble at the end... and the last lesson learned... execVM is not my friend. Thanks everyone who helped :) Share this post Link to post Share on other sites