OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: .pablo. on 24 Jul 2003, 19:28:23

Title: variables (tough)
Post by: .pablo. on 24 Jul 2003, 19:28:23
original post:
Quote
_type createunit [getpos _trig, _group, "soldier1 = this"]

i want to be able to send several different ai men through this line, but the init string won't let me put local variables in it

so how do i make a local variable work in a string, or is there something i can do something that will have the same effect?

(basically i want to replace soldier1 with _soldier)

i fixed this problem by making an array of strings, for example:
_soldinitlist = ["soldier1 = this", "soldier2 = this", etc]
then i did _soldinit = _soldinitlist select _soldnumber
then i just put _soldinit in for the where the init string would go

but another problem came up, which is detailed below!!
Title: Re:createunit (tough)
Post by: .pablo. on 24 Jul 2003, 22:54:57
edit:
taken out because it was wrong
Title: Re:createunit (tough)
Post by: KTottE on 25 Jul 2003, 08:33:07
Quote
cant use no private varies  

From LCD in my thread reg. this command.
I'm sorry mate, you'll have to do a separate line for each dude.
Title: Re:createunit (tough)
Post by: .pablo. on 25 Jul 2003, 08:47:15
k i think i know exactly what the problem is now, it has nothing to do with createunit

(i just updated my original posts with what i've learned)

basically if i have a line like:

Quote
soldiers = [s1, s2, s3]

then what i'm getting is actually:

Quote
soldiers = [what s1 stands for, what s2 stands for, etc]

for example:

Quote
what i write: soldiers = [s1, s2]
what i get: soldiers = [Alpha Black 1, Alpha Kilo 1]
what i want: _sold = s1
what i get: _sold = Alpha Black 1

so when i deletevehicle _sold and createunit another unit which is assigned to s1/s2/etc, _sold is still equal to the deleted unit instead of s1 and the new unit, so when i do:

Quote
[_sold] exec "script.sqs
i'm getting:
Quote
[Alpha Black 1] exec "script.sqs"
instead of:
Quote
[s1] exec "script.sqs"


how can i get around this??
Title: Re:createunit (tough)
Post by: KTottE on 25 Jul 2003, 08:53:52
Hm, that's a toughy :-\
I don't know if you can change the way OFP sends variables, by reference or by value. A toughy indeed...
Title: Re:variables (tough)
Post by: .pablo. on 25 Jul 2003, 08:58:41
just updated the topic with a more suitable name

i was hoping there would be a way to take a string like "s1" and then somehow take off the quotes and make it equal to the variable that it is named after.

but i can't think of any way to do it...
Title: Re:variables (tough)
Post by: .pablo. on 25 Jul 2003, 09:07:10
OMG i figured out how to do it

toadlife posted this function in KTottE's thread:
http://www.ofpec.com/editors/funcref.php?letter=C#CreateUnit2

i looked at the function and stole this line of code:
_return = (units _group) select ((count units _group) -1);
i just changed _return for _sold and i was good to go!!!!


PWNAGE

edit:
and god damn that took a lot of time to figure out!!

edit2:
does anyone know of any bugs i could get from using the group method of finding the id? (like if i called the script several times at the same time or something)

and why is it ((count units _group) -1) instead of just (count units _group)?

edit3:
omg this script pwns so much, it uses like 1/4 the amount of code and now can be used with any ai units, not just predetermined ones...god damn this kicks ass...
Title: Re:variables (tough)
Post by: toadlife on 25 Jul 2003, 09:20:37
Use the call command.   ;)

Code: [Select]
soldiers = ["s1", "s2"]
(call soldiers select 0) exec "script.sqs"
(call soldiers select 1) exec "script.sqs"

In the above example the call command retrives the string and executes it as if it were code. So...

(call soldiers select 0) exec "script.sqs"

Is executed as....

s1 exec "script.sqs"

If you want to call your strings and ahve them executed AS strings you simply do this...

(call {soldiers select 0}) exec "script.sqs"

and it will be executed as...

"s1" exec "script.sqs"

:cheers:
Title: Re:variables (tough)
Post by: .pablo. on 25 Jul 2003, 09:28:17
what if i have more than one parameter?
Title: Re:variables (tough)
Post by: toadlife on 25 Jul 2003, 09:29:16
LOL. Looks like I posted my answer a second too late!
Title: Re:variables (tough)
Post by: toadlife on 25 Jul 2003, 09:30:39
what if i have more than one parameter?

More than one parameter for what?
Title: Re:variables (tough)
Post by: .pablo. on 25 Jul 2003, 09:33:26
Quote
soldiers = ["s1", "s2"]
(call soldiers select 0) exec "script.sqs"
(call soldiers select 1) exec "script.sqs"

i kept the example simple when i was explaining it, script.sqs actually takes 2 arguments, so it looks like this:

Quote
[_sold, _man] exec "script.sqs"

i don't know anything about the call command...(1.85 added a lot of commands, still learning some of them)
Title: Re:variables (tough)
Post by: toadlife on 25 Jul 2003, 09:44:26
More examples of using the call command...

Code: [Select]
soldiers = ["s1","s2","s3"]
men = ["c1',"c2","c3"]
[call soldiers select 0,call men select 0] exec "script.sqs"

Code: [Select]
soldiers = ["s1","s2","s3"]
men = ["c1',"c2","c3"]
_sold = call soldiers select 0
_man = call men select 0
[_sold,_man] exec "script.sqs"

My universal weapons respawn script uses the call command..it looks something like this:

Code: [Select]
_name = _this select 0
_unit = call _name

#respawnloop
@!alive _unit
@alive call _name
_unit = call _name
removeallweapons _unit
.....
.....
.....
.....
goto "respawnloop"
Title: Re:variables (tough)
Post by: .pablo. on 25 Jul 2003, 09:57:34
Quote
soldiers = ["s1","s2","s3"]
men = ["c1',"c2","c3"]
_sold = call soldiers select 0
_man = call men select 0
[_sold,_man] exec "script.sqs"

i was thinking you had to start up the script while using the call command, this gives me a better idea of what the call command actually does (though im still confused)

Quote
soldiers = ["s1", "s2"]
(call soldiers select 0) exec "script.sqs"
(call soldiers select 1) exec "script.sqs"
 

In the above example the call command retrives the string and executes it as if it were code. So...

(call soldiers select 0) exec "script.sqs"

Is executed as....

s1 exec "script.sqs"

If you want to call your strings and ahve them executed AS strings you simply do this...

(call {soldiers select 0}) exec "script.sqs"

and it will be executed as...

"s1" exec "script.sqs"

why do the {} change it from no quotes to quotes?


edit:
and what does it mean to execute a string as if it were code?  does that mean it just gets rid of the quotes?

edit2:
ok pwnage i just got the call command to work in the script too
Title: Re:variables (tough)
Post by: toadlife on 25 Jul 2003, 10:08:12
Code: [Select]
why do the {} change it from no quotes to quotes?
because the little curly brackets are interpereted by the game engine as quotes. You could instead use double quotes in place of the curly brackets ie....

call ""_variable"" exec "script.sqs"

instead of...

call {variable} exec "script.sqs"


.....but the double quotes are messy.


Curly brackets are cleaner looking. BIS mentions the curly brackets and recommend using them in the official command reference.


Before you get even more confused, double quotes are needed when you are putting 'quotes around quotes'.

Here is an example of a line of code where you would need to use double quotes AND/OR curly brackets...

"""_x setunitpos "up""" foreach units group1
{_x setunitpos "up"} foreach units group1
{_x setunitpos {up}} foreach unit group1


Note that all three lines above are interpereted by the game the same and they all do the exact same thing.

I prefer using only brackets. It make the code look cleaner and MUCH easier to read.

Title: Re:variables (tough)
Post by: toadlife on 25 Jul 2003, 10:17:35
and what does it mean to execute a string as if it were code?  does that mean it just gets rid of the quotes?
Quote

Yep, it does get rid of the quotes.

Lets say you have this global variable...

mycodestring = "player setdamage 1"

If in  a script, you put..

Code: [Select]
call mycodestring

it would be EXACTLY the same as putting this in the script...

Code: [Select]
player setdamage 1

The call command simple takes a string and "executes" the contents of the string as if you had typed the string in the script.

Title: Re:variables (tough)
Post by: .pablo. on 25 Jul 2003, 10:18:18
nevermind, questions answered  :)

is my imagination or has format been around longer than call?  kind of weird considering call is just the opposite of format (in my understanding of it).

new problem:
i put this in a guy's init:
Quote
(call (soldiernames select 0) = this);

it says unknown operator and points at the =, what gives?
is call restricted to local variables or something?
Title: Re:variables (tough)
Post by: toadlife on 25 Jul 2003, 23:12:11
It's possible the call command will not work in a units init feild. Why not just name the soldier?


Format has been around since v1.0. It outputs a string as opposed to the call command which outputs the contents of a string.


Edit:call and format can be used together to create dynamically named variables, which has led to alot more posibilities for those nuts who code COC. I won't get into that though. I havn't had my Coffee yet this morning.

Title: Re:variables (tough)
Post by: .pablo. on 26 Jul 2003, 00:16:41
Quote
It's possible the call command will not work in a units init feild. Why not just name the soldier?

there was a reason, i just don't remember it right now...
Title: Re:variables (tough)
Post by: LCD on 26 Jul 2003, 00:26:02
@ pablo

u forgot 2 add da

::)

in da endof ur sentence ;)

so it shud b like dat

there was a reason, i just don't remember it right now...  ::)

LCD OUT
Title: Re:variables (tough)
Post by: .pablo. on 26 Jul 2003, 00:36:37
oh no!  :o

lol
Title: Re:variables (tough)
Post by: LCD on 26 Jul 2003, 01:02:56






(http://www.spielerboard.net/images/smilies/lool.gif)







LCD OUT