Home   Help Search Login Register  

Author Topic: variables (tough)  (Read 3161 times)

0 Members and 1 Guest are viewing this topic.

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
variables (tough)
« 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!!
« Last Edit: 25 Jul 2003, 08:56:36 by pablo_m123 »

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:createunit (tough)
« Reply #1 on: 24 Jul 2003, 22:54:57 »
edit:
taken out because it was wrong
« Last Edit: 25 Jul 2003, 08:48:31 by pablo_m123 »

Offline KTottE

  • Former Staff
  • ****
Re:createunit (tough)
« Reply #2 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.
"Life is not a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming 'WOW What a Ride!'"

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:createunit (tough)
« Reply #3 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??
« Last Edit: 25 Jul 2003, 08:53:17 by pablo_m123 »

Offline KTottE

  • Former Staff
  • ****
Re:createunit (tough)
« Reply #4 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...
"Life is not a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming 'WOW What a Ride!'"

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:variables (tough)
« Reply #5 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...
« Last Edit: 25 Jul 2003, 09:00:09 by pablo_m123 »

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:variables (tough)
« Reply #6 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...
« Last Edit: 25 Jul 2003, 09:20:09 by pablo_m123 »

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:variables (tough)
« Reply #7 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:
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:variables (tough)
« Reply #8 on: 25 Jul 2003, 09:28:17 »
what if i have more than one parameter?

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:variables (tough)
« Reply #9 on: 25 Jul 2003, 09:29:16 »
LOL. Looks like I posted my answer a second too late!
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:variables (tough)
« Reply #10 on: 25 Jul 2003, 09:30:39 »
what if i have more than one parameter?

More than one parameter for what?
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:variables (tough)
« Reply #11 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)
« Last Edit: 25 Jul 2003, 09:35:23 by pablo_m123 »

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:variables (tough)
« Reply #12 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"
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:variables (tough)
« Reply #13 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
« Last Edit: 25 Jul 2003, 10:13:44 by pablo_m123 »

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:variables (tough)
« Reply #14 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.

"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.