Home   Help Search Login Register  

Author Topic: call format question  (Read 1553 times)

0 Members and 1 Guest are viewing this topic.

Offline Darren

  • Members
  • *
call format question
« on: 04 Jul 2006, 21:33:40 »
I feel silly asking this as I have searched and found many examples of
call format which have either helped me in understanding it or confused me completely.
I also feel bad as it is used so widely and I cannot quite grasp it :-(

Based on reading 3 versions of the command ref
(command reference 1.91 by HAMMY, CMDref104 (unofficial) and CMDref (official))
and tuts written by snyper, mcguba and a few others
-- ** Thanks to all who spent the time and trouble to create all of these really cool docs/tuts/references!! **--

...I got lost...

call {x=1} - will make x = 1 at this point. (right?)
x = [1,2] call {(_this select 0)+(_this select 1)} - will have x = 3 (right?)

now
_NewString = format ["%1", _MyString] - will make _NewString that same as _MyString (right?)
or
_MyString = format ["%1", _MyString] - will leave _MyString just as it is. (right?)

what happens when
_MyString = call format ["%1",_MyString] -
I would believe it is the same thing as you are telling it to "execute command" (or call) format ["%1",_MyString]
Does it change the variable type to something else (an object maybe)?

I have seen many examples in snippets where they
_TheUnit = call format ["%1", _TheUnit]
I see this as _TheUnit becomes the string value of whatever _TheUnit was - but I know this is not the case.
if I had to hint format ["%1",_TheUnit] after the call format it does not know what to do.

There are some pretty wild uses of call that I won't even go there yet until I get some basics...maybe the rest will fall in place.

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: call format question
« Reply #1 on: 06 Jul 2006, 14:54:53 »
I feel silly ...

Don't feel silly, as this a question for the advanced forum topic. I will try to answer your questions as best I can, but I am no expert even though I have used the call command a lot. Hopefully any mistakes I make will be corrected.

Quote
call {x=1} - will make x = 1 at this point. (right?)
Yes

Quote
x = [1,2] call {(_this select 0)+(_this select 1)} - will have x = 3 (right?)
This is interesting. I had not seen it used like this before, but the answer is Yes.

Quote
1)   _NewString = format ["%1", _MyString] - will make _NewString that same as _MyString (right?)
or
2)   _MyString = format ["%1", _MyString] - will leave _MyString just as it is. (right?)
what happens when
3)   _MyString = call format ["%1",_MyString] -
I would believe it is the same thing as you are telling it to "execute command" (or call) format ["%1",_MyString]
Does it change the variable type to something else (an object maybe)?

The most common use for the call format idiom is to allow the use of dynamically created variables.(In Ofp-speak they are called metavariables, and may also be invoked using forEach and count. Check out the last section of Dinger's variable tute in the Ed Depot. It predates the call command so uses forEach and count command work-arounds  to do the same thing as call now does.)  Dynamically created variables are variables whose names are created while the mission is running.  The variable name can be stored in another variable as a string.  The call command executes a code string i.e. a code snippet enclosed in either double quotes or curly braces.  The format command is used to create the code string to pass to the call command. Back to your examples above.

1) Not sure what this will do, but I do not think it will do the same thing as _NewString = _MyString.  My guess is that _NewString will have the same string as _MyString, but double quotes will enclose it.  But the best way is just to test this using the editor which I cannot do atm as I am at work :).
2) Same as above. I think the new string will be enclosed in quotes.
3) This statement is the same as _MyString = _MyString.

I am trying to think of a clear example...Okay. Let's say you have a bunch of units named u1,u2,u3,u4,...,uN,...,u100. You also have a set of variables a1,a2,a3,a4,...,aN,...,a100, that store some data about each unit respectively. You have a script you want to operate on any given  unit uN that uses the value of aN to determine a course of action.. You could write an example script like this one:
Code: [Select]
_N = _this select 0
_unit = call format["u%1",_N]
_a = call format["a%1",_N]
if (_a == 1) then {_unit doWatch tank1} else {_unit doWatch tank2}
exit
The script is called by:
Code: [Select]
[_anumber] exec "examplescript.sqs"
The call format lets you pass _anumber to the script and then access the values of uN and aN. This is not a very useful example but I hope it illustrates something. Of course you can put a longer code strings in a format statement.

The best way to proceed is to post any code snippets that you do not understand, and I will try to decipher them for you and maybe someone can correct my mistakes...  ::)
« Last Edit: 06 Jul 2006, 15:17:31 by Mr.Peanut »
urp!

Offline Darren

  • Members
  • *
Re: call format question
« Reply #2 on: 10 Jul 2006, 21:48:24 »
Thanks Mr. Peanut for the explination ;)

I'm still not 100% but you have me a little closer...

Before I post something else, let me first look at Digner's tut and see what I can find there.

Lastly,  the calibur of scripts I write are not yet worth mentioning, therefore I'm trying to understand other snippets or scripts I have DL'd over time.

I do not want to get into trouble by posting other people's stuff and questioning it.
Some of the stuff I have got I cannot even remember where I got it from, as one tends to surf many places just to get bits and bobs. (or is it bits and bytes)

Let me see if I get further and hopefully post positive findings.

Thanks again for your time and trouble.

Offline Darren

  • Members
  • *
I think I got it...
« Reply #3 on: 10 Jul 2006, 23:31:24 »
 :)

;if I use player (which should be me) or [this] in the init field of my player as in
;[player] exec "thisscript.sqs" or [this] exec "thisscript.sqs"
;then it is unable to do the getpos command for _TheUnit.
;_Pos1 and _Pos2 give scalar bool array errors on the hint display

;if I use the name assigned to the player in the editor and pass it as a string as follows
;["MyPlayersEditorName"] exec "thisscript.sqs"
;then it is necessary to use call format to identify the unit as an abject


_TheUnit = _this select 0

_TheUnit = call format ["%1",_TheUnit]

_Pos1 = getpos _TheUnit select 0
_Pos2 = getpos _TheUnit select 1
 
hint format ["Pos1 = %1 Pos2 = %2", _Pos1,_Pos2]


; this means to me that the call command calls the object with the name you formatted as string
; if I'm correct then the following would work too... given that you pass it a string and not the object
; ["MyPlayersEditorName"] exec "thisscript.sqs"

;wait before the next hint as this is all one script

~5

_TheUnit = _this select 0

;Note the change
_TheUnit = call _TheUnit

_Pos1 = getpos _TheUnit select 0
_Pos2 = getpos _TheUnit select 1
 
hint format ["Pos1 = %1 Pos2 = %2", _Pos1,_Pos2]

;GREAT!! This worked too!!!

;in conclusion...
;the format command after the call is merely there so that,
;if using multiple variables you want this script to understand
;then you can format them specifically (Mr.Peanut - as per your unit examples: _unit = call format["u%1",_N]
;else just use the call command to ask OFP nicely to pass you the object assigned to that string
;(or rather has that string for a name)

;That is why in the first attempt,
;it failed due to the fact that there is no object with the string name of player or this (note no quotes)
;or when I use the call format on player or this it gives me a much longer string
;including player's side, teamname:number (ActualProfileName)
;and this is not the string assigned specifically as the name

;what I don't understand in your example is what it would do to the action variables
;as per the example: _a = call format["a%1",_N]

;have I just touched the "call" surface?

Offline Darren

  • Members
  • *
Re: call format question
« Reply #4 on: 10 Jul 2006, 23:54:30 »
 ???

Allow me to attemp to answer my own question...

;what I don't understand in your example is what it would do to the action variables
;as per the example: _a = call format["a%1",_N]


Lets say I have a bunch of anythings defined in my script as local variables or even as globals...

Eg1 - Strings
a1 = "Text1"
a2 = "Text2"
a3 = "Text3"


Eg2 - Values
a1 = 1
a2 = 2
a3 = 3


Eg3 - objects
a1 = Waypoint1
a2 = Waypoint2
a3 = Waypoint3


and the script contained
_a = call format ["a%1",_n]

then passing the _n as a value to the script would result in:

Eg1
if n was passed as 1 then
_a = "Text1"
if n was passed as 2 then
_a = "Text2"
if n was passed as 3 then
_a = "Text3"

ie. _a would end up as a string

Eg2
if n was passed as 1 then
_a = 1
if n was passed as 2 then
_a = 2
if n was passed as 3 then
_a = 3

ie. _a would end up as a value

Eg3
if n was passed as 1 then
_a = Waypoint1
if n was passed as 2 then
_a = Waypoint1
if n was passed as 3 then
_a = Waypoint1

ie. _a would end up as an object

How close am I?

Merc

  • Guest
Re: call format question
« Reply #5 on: 11 Jul 2006, 00:04:13 »
I would like to thank Darren and Mr.Peanut for helping me solve one of my own problems. This topic really helped.

And I don't know if this will help you but it's basically what I used for my script:
Code: [Select]
global = player setdamage 1
call format ["%1", global]
call format activated whatever I set as the global variable.

Hope that helped!  ;D

Offline Darren

  • Members
  • *
Yup... I really think I got it!!
« Reply #6 on: 11 Jul 2006, 01:37:06 »
 8)

Ok... somtimes I can kick my own arse!  ;D

Why as questions when I can test my own answers...

What follows is a little test script I wrote with pretty descriptive comments to try and shed more light on my own question and most importantly test my sanity:
 ::)

;this script is to attempt to understand call format command
;pass this script a number between 1 and 5 and it will return the relevant results
;Use: [<MyValue>] exec "thisscript.sqs" in any init field
;Where: <MyValue> is replaced with a number, 1 to 5

;get the number you entered

_n = _this select 0

;these are the 3 different variable types I used
;Strings

_a1 = "Text1"
_a2 = "Text2"
;Values
_a3 = 3
_a4 = 4
;An Object (I chose player coz I'm sure anyone trying to run this would have one  ;))
_a5 = player

;Check that you passed the correct value 1 to 5, if not, goto #WrongVal
?(_n<1 || _n>5) : goto "WrongVal"

;Hee Hee
hint "I am about to enter unknown scripting territory!"
~3

;turn _MyVar into one of the types you have defined above by using call format.
;ie if number passed to script is 1, then firstly
;format the string to be _a1 and secondly
;call will resolve _a1 as whatever type you defined it and the assign it to (in this case "Text1")
;same applies for 2 to 5!
;baring in mind that it keeps original variable type
;_a1 and _a2 remain strings
;_a3 and _a4 remain values
;_a5 remains an object

 
;Abracadabra
_MyVar = call format ["_a%1",_n]

;Now I handle the different types to see if the above were true
;if you used 1 or 2 as the values passed to this script
;then you should be able to just diplay the strings assigned to _a1 and _a2 respectively,
;no further formatting needed

? (_n >= 1 && _n <= 2) : hint _MyVar
;if you used 3 or 4 as the values passed to this script
;then some simple maths should prove whether or not it worked

? (_n >= 3 && _n <= 4) : hint format ["%1 + 1 = %2",_MyVar,_MyVar+1]
;if you used 5 as the passed value then player object was selected and
;you should see a hint stating your player's current pos

? _n == 5 : hint format ["player pos is %1,%2",getpos _MyVar select 0,getpos _MyVar select 1]

;I've had enough fun... let's go!
goto "GetOut"

#WrongVal
;MAN, 1 to 5 ... 1 TO 5 ...  ;)
hint format ["You entered the incorrect value to call this script. Only between 1 and 5, you entered %1",_n]

#GetOut
;my first mistake was not waiting here before the last hint
;and I ended up overwriting my test hints
;hee hee, I thought the damn thing did not work... serves me right!
  :-[
~3
Hint "End of Call Script."


SPECIAL TX TO MR. PEANUT FOR POINTING ME IN THE RIGHT DIRECTION AND MAKING MY GREY MATTER WORK FOR A CHANGE!! 
Merc, tx for you contribution and it's only a pleasure ;)


Merc, your example is one I may have forgotten to think of and therefore attempt to mention
Call is also used (as per the command refs) to execute given code is string format. (only now do I see what they meant) :o
It's worth mentioning yours specifically as I see it by saying:


global = player setdamage 1
- specifically sets global variable to a command, suicide (harsh man!  ;))
now we have set up a command, one needs to call it as a string... and how else do we get it as a string but by using format...
call format ["%1", global]
- in this case is as good as typing player setdamage 1

Just to be silly, (not to dis you at all Merc, but for the purpose of as many explinations in one place as possible for others who may find what is on these posts useful/confusing) I would like to take you example and say it is the same as saying

global = "player setdamage 1"
call global
-in this way global is a string already and therefore no format required in the call statement

one last go at it for now...
_TheCommand = setdamage
_TheUnitOrObject = player
_TheDamageValue = 1
call format ["%1 %2 %3",_TheUnitOrObject, _TheCommand, _TheDamageValue]
- and now you have one home made suicidal calling card!






« Last Edit: 11 Jul 2006, 01:45:36 by Darren »

Merc

  • Guest
Re: call format question
« Reply #7 on: 11 Jul 2006, 02:57:12 »
While suicide is a great topic, I only used it as an example (wonder why that was my first thought  ;D). I needed call format in a script that took something that was already in a global variable and execute it (from an edit box in a dialog to a script).

Here's the finished product if your interested: http://www.ofpec.com/forum/index.php?topic=27641.0

The dialog and script allow you to execute any command you input, like killing yourself  ;) or killing any other named object or setpos yourself anywhere.

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: call format question
« Reply #8 on: 11 Jul 2006, 21:22:37 »
Code: [Select]
global = player setdamage 1
call format ["%1", global]

Now that is just plain weird. OFP can create a pointer to a command line?  Maybe some computer scientist out there could explain exactly in technical terms (objects and addresses) what the scripting engine is doing in this instance? What is the difference between the snippet above and the following snippet?
Code: [Select]
global = "player setdamage 1"
call global
They do the same thing, but I think they are doing it in completely different ways.(The first creates a pointer to a command? And the second one???)

A new tute on the call command would be very useful, if a real expert could write one up.  I mostly use call format to create dynamic variables and manipulate markers, and am not qualified to write it.  Perhaps a thread where people could post their own example uses of call?

@Darren, glad to see you have got the answers to your questions.  Now I just have to reread your _theUnit example and figure it out for myself! ;)
« Last Edit: 11 Jul 2006, 21:31:27 by Mr.Peanut »
urp!

Offline Darren

  • Members
  • *
Re: call format question
« Reply #9 on: 13 Jul 2006, 00:38:59 »
 :) Merc, Like what you did with the sandbox dialog!  I've always wanted to type/test script commands while in a game, but you gave me the tool to do it!  I first thought it was just for typing text, but it was cool when I used it to "soldiereb" createunit [getpos player,mygrp] !!  :)

 :-\
Mr.Peanut...
From what I can see there is no difference between the 2.
As the call command requires you to pass it something as a string... in both cases you do just that!

In the first example global is set to a command right,
but when you want to execute it using call it has to be in string format (pardon the pun)
so you use the format, which just turns whatever global contained into a string.
global used to be player setdamage 1 and after format became "player setdamage 1"

In the second case, I attempted to save time (and maybe some processing - but I'm not too sure of this)
by already creating the command as a string.
This means that call can just call the string and effectively execute the same command in the same format.
from the start global was "player setdamage 1"

WRT my _theunit example...
In my initial question (the one that started this thread), I just took it from a script, not paying attention to the fact that at the top of the script...
_theunit select 0
...was what caused my confusion.  This is due to the fact that it is important to understand what type of variable the variable is prior to you using the call or call format command

Hence, in my sample script I wrote to figure it all out...I came to the conclusion that if an object is passed to the script as one of the parameters, then there is no need to call format it nor call it, especially if you are going to use it as they type is was given

For this example only, I use getpos.
Getpos requires an object in order to work
I pass it in this example as the object
So I do not need to call anything as I have what I wanted

eg [player] exec "thisscript.sqs"
_MyUnit = this select 0
_MyPos = getpos _MyUnit

However, if I passed the editor name to the same script,
I would have to change it to use call to resolve the text name associated to the object I actually wanted in the first place
eg ["PlayerNameAsItIsInTheEditor"] exec "thisscript.sqs"

_MyUnit = this select 0

;at this point _MyUnit is just text but what I need to be able to getpos is the object, so I

_MyUnit = call format ["%1"_MyUnit]
***or I could have said***
_MyUnit = call _MyUnit

;1st _MyUnit - which took _MyUnit and made sure it was text and
;then called the associated object assigned to the text. ie. the acutal player who's name was "PlayerNameAsItIsInTheEditor"

;when I realised what it was doing, I also figured that if call looked for text and the parameter passed was already text
;then why format text into text and you could just use 2nd _MyUnit. It did the same thing!

_MyPos = getpos _MyUnit



Offline Darren

  • Members
  • *
Re: call format question
« Reply #10 on: 13 Jul 2006, 01:15:23 »
 ;D ;D ;D ;D ;D ;D ;D ;D

MAN!!!  This stuff just gets better by the second!!!

Check what I got right...

;Put this in a script and pass it an object's name as it is in the editor as text
;eg ["objecteditorname"] exec "thisfreak.sqs"

hint format["Player X Pos = %1",getpos call format["%1",[Testing123,_this select 0] call {_this select 1}] select 0]

 ??? ??? ??? ???

Scary part is...  :o  I can understand it  ;D

Strange to think it's the same dumbass that started this thread!  :-[