Jump to content
Sign in to follow this  
kyfohatl

What's wrong with this script???

Recommended Posts

I've written the very simple script bellow, which runs in the init.sqs. It's supposed to allow the player to switch to the unit (any infantry) closest to where he clicked on the map:

_nearUnit = nearestObject "Unit"

onMapSingleClick "selectPlayer _nearunit"

Why doesn't it work? (yeah, I know... I'm a noob) Maybe its because the object class "unit" doesn't make sense or its too broad?

Edited by kyfohatl

Share this post


Link to post
Share on other sites

A lot to be honest. :) Why don't you just make all your units playable and use TeamSwitch (T key)? Do keep in mind that teamswitch games are stupid when it comes to tasks, notes, and eventhandlers.

Share this post


Link to post
Share on other sites

I knew there were many things wrong with it...:o

Anyway, I have to use this because the units are DAC generated (dynamic AI creator), so I can't set them to playable before the mission, as they are created at the start of each mission (but I'm sure you knew that :)).

Could you perhaps help me improve this retarded script?

Thanks for the quick reply by the way

---------- Post added at 07:11 AM ---------- Previous post was at 06:47 AM ----------

Ok, made some improvements on the script, but it still desn't work:

_nearunit = nearestObject [_pos, soldierWB]

onMapSingleClick "selectPlayer _nearunit"

Anyone know why?

Share this post


Link to post
Share on other sites

Beacuse _nearunit does not exist in the scope of the onMapSingleClick event. Either use a global variable:

KYF_Nearunit = nearestObject [_pos, 'soldierWB']

And

onMapSingleClick "selectPlayer KYF_Nearunit"

Or do it all in one line:

onMapSingleClick "selectPlayer (nearestObject [_pos,'soldierWB'])"

But onMapSingleClick can be over written if something else uses it. Eventually you should look to use:

displayAddEventHandler

Share this post


Link to post
Share on other sites

Adapted from a teleport script, and sadly in SQS but I'm too sleepy to convert to SQF:

switched=true
titletext ["Click on the map to select a unit","plain down"]
onMapSingleClick "swUnit = nearestObject [_pos, 'soldierWB']; selectPlayer swUnit;switched=false"
@!switched
onMapSingleClick ""
titletext ["","plain down"]

Run it from a Radio trigger I guess? :)

Or what UNN said :)

Share this post


Link to post
Share on other sites

@UNN: Yes...IT WORKED! Thankyou very much. I know the method I'm using is not the best option. I will definitely learn how to use the EventHandler. I'm just using this method becuase I'm a noob and really can't script well, so I stuck with the very basic stuff untill I learn some more.

@Kylania: That should too. Thanks alot. With my current scripting skills, I can undrestand about 80% of it. I'm looking at a scripting tutorial right now to improve, and hopefully fully understand it.

Share this post


Link to post
Share on other sites

damn, it doesn't work well. When I tested it, it worked (on a small test mission), but as soon as I tried the script on my actual mission (which is rather large), it wouldn't work and I tried changing alot of things, but it still didn't work. Actually it worked for one guy (one of the patroling guards), so I could only switch to him, but no one else (strange, isn't it?). Does anyone know why?

I think it may be because, as UNN said onMapSingleClick is unreliable, so I went off to learn about event Handlers as UNN suggested. I did alot of searching, both in the wiki and in the forums, but I still don't understand how to use them (the wiki confused the hell out of me). My understanding so far, is that an event handler basically takes an enet, say the click of a mouse (MouseButtonClick) and "tags" it to a function that does something after the "event" has taken place. Is that right? If so, how do I "tag" the MouseButtonClick event to a function (function: has something in the lines of selectPlayer (nearestObject (<position & unit class>))?

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×