Jump to content
Sign in to follow this  
gnurf

Objects to strings, used by call function

Recommended Posts

I´ll try to illustrate my problem with two examples

example 1:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_tmp = "doStop ((units _CPgroup) select 0)"

player sideChat _tmp

call _tmp

Here sideChat generates a message string that looks like this

"doStop ((units _CPgroup) select 0)"

and call _tmp works like a charm.

Example 2:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_tmp = (units _CPgroup) select 0

_tmp = format["doStop (%1)",_tmp]

player sideChat _tmp

call _tmp

Here sideChat generates a message string that looks like this

"doStop (CIVL Foxtrot Black:1)"

but call _tmp generates this error

Quote[/b] ]'doStop (CIVL #Foxtrot Black:1)': Error unknown operator Foxtrot

Why can´t i just use doStop ((units _CPgroup) select 0)? Becouse in the real code ((units _CPgroup) select 0) is acctually a temporary variable and the finished string is to be saved in an array to be used much later when the temporary no longer exists.

obviously the problem is that CIVL Foxtrot Black:1 is not considerd an object after (units _CPgroup) select 0

has passed through the format function. so how does one convert CIVL Foxtrot Black:1 back into an object?

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">"doStop ( 'unknown command' (Civi Foxtrot Black:1) )"

Can it be done?

Share this post


Link to post
Share on other sites

how about

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_tmp = (units _CPgroup) select 0

doStop _tmp

Quote[/b] ]so how does one convert CIVL Foxtrot Black:1 back into an object?
One does not, unless you want to run a brute force comparison on all units to match the string.

EDIT: If you really want to use call then

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_tmp = (units _CPgroup) select 0

_command="doStop _tmp"

call _command

Share this post


Link to post
Share on other sites

Nope.

the resulting _tmp string is stored in an array

and the contents of that array gets executed by a call function later on. So everything in the array must first be converted into strings and thats the problem.

I cant save the temporary argument, in these examples converted to (units _CPgroup) select 0, becouse this is actually the result of a function and which might not return the same result when executed next time. There for i must store the result from (units _CPgroup) select 0 and later convert it back to an object.

...does that explain it better?

Quote[/b] ]One does not

Thats just.....great...

Ah, well guess i will have to save the temporary argument for the unit somewhere.

Share this post


Link to post
Share on other sites

What you are doing seems rather strange, before you were simply storing a command in a string. Then why not simply script the command when you need it?

Otherwise, you can store even that command in a string:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_tmp = {(units _CPgroup) select 0}

_tmp = format["doStop %1",_tmp]

player sideChat _tmp

call _tmp

I have to warn you that _CPgroup may be some other group or even non existant by the time you do the call _tmp somewhere else.

PS: Whenever you store any command in a string you are in effect forming a function. Once your commands get larger than "one instruction" I suggest you create functions and use preProcess on them.

Share this post


Link to post
Share on other sites
Quote[/b] ]Whenever you store any command in a string you are in effect forming a function

and thats exactly what i want smile_o.gif

Quote[/b] ]What you are doing seems rather strange

duh! biggrin_o.gif

Quote[/b] ]before you were simply storing a command in a string

...I was trying to illustrate the problem i had in adding the object directly into the string. I guess i failed both in illustrating the problem and adding the object. biggrin_o.gif

Its not that strange really and i´ll try to explain what im doing

(every one not really intrested should stop reading NOW!)

_CPgroup or _Current_People_Group is a group out of an array called _people consisting of groups which get processed one by one in huge loop

_CPgroup = _people select _i

_i is then also used on these arrays to get information about the current group that the script stored previousluy

_PActionBriefInfo       select _i  ==>_CPActionBriefInfo

_PActionEvent          select _i  ==>_CPActionEvent

_PActionCondition

_PActionFinishedCondition

_PActionOnFinishedCondition

_PActionObject

_PActionFinishedConditionTimer

_PActionConditionTimer

_CPActionEvent for example is an array contining strings of commands to be executed.

the last index in _CPActionEvent corresponds to the last index of every other _CP array (except _CPgroup which as i just said is a group and not an array)

to execute the string in (_CPActionEvent select 3) i need to use the call function: call (_CPActionEvent select 3)

But also for call (_CPActionEvent select 3) to be executed call _CPActionCondition needs to return true

? call (_CPActionCondition select 3) : call (_CPActionEvent select 3)

Lets say unit1 is waiting to borde a bus. then (_CPActionEvent select 3) should be a string that looks somthing like this "unit1 assignAsCargo bus1" but that should only be executed if

? call "(_bus1_status == waiting) and (_bus1_station == _unit1_station)" returns true

? call (_CPActionCondition select ((count _CPActionCondition)-1)): call (_CPActionEvent select ((count _CPActionCondition)-1))

Basiclly im building a waypoint system with things to do in certain sequences that is to be used with a random event generator

the problem i had was that if a unit was found in the driver seat of a bus i without previously having been there,

for example maybe he was moved there by the moveInDriver command in a trigger, then all the other units in his group should be assigned as cargo. The problem was that when the command to move the other units into the cargo spots i had to know who was the driver to prevent him from changing seats. Also he might for some reason (a trigger again) no longer be the driver of the bus there for i needed to know who is supposed to be the driver and not who pressently is.

The sollution i found (even if i don´t like it) was to add the supposed driver to the bus allready stored in _CPActionObject and also change _CPActionObject´s format from [object] to [[object,driver]].

(everyone not asleep by now should please be quiet and not wake the others)

biggrin_o.gif

Share this post


Link to post
Share on other sites

Well then create an array of relevant units in a global variable.

MyStorageArray for example, and add your specific men to it (when needed)

MyStorageArray = MyStorageArray +[_theunit];

in your string you save

format ["MyStorageArray select %1",count MyStorageArray -1]

Then anywhere in your scripts you can continue to use that string to reference the unit. Don't add the unit if it is already in the array, just grab it's index in that case. And never remove any units. That will do it.

Share this post


Link to post
Share on other sites

Nah i think i´ll keep it as it is. If i stored it in a global array i would have to clean that out once in a while not to mention keeping track of what index number corresponds to what in the array

damnit you edited again wink_o.gif ... ok sure if you dont let the script clean up after it self and just keep adding to that array that would work. But thats not a good solution in the long run

Thanks for the help and suggestions though

smile_o.gif

Share this post


Link to post
Share on other sites

Fine, then the other method is to use metavariables. Pick a metavariable prefix like MyDude and then keep appending numbers to it like MyDude1 MyDude2 MyDude3... MyDude2321312

and you assign your specific units in those variables. In your string you save that name. When you are done with it you can delete the metavariable by doing MyDude% = nil. (whatever the syntax)

Share this post


Link to post
Share on other sites

If you are worried about overwriting existing metavariables you can use a function like funDefined

or funGetType

which I can provide to you in the number system library from CoC.

Share this post


Link to post
Share on other sites

you mean somthing like this?

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

call  "MyDude" + format["%1",random 999] + "= driver(bus1)"

I figured on using call for this since that enables you to construct the name anyway you want, but maybe you know a better way? The problem i had when i tested this a few weeks back was that i couldn´t get around the span of the call function which made everything created in it a local variabel and void when called from outside (as you said, one is actually calling a function).

Never got around to try and create a global though. I just figured it to be impossible and worked around the problem.

But if you know a better way please tell  me smile_o.gif

About testing wether or not a varibel is set. Wouldn´t this simple thing work?

Quote[/b] ]? format["%1",anyvariabelname] == "scalar bool array string 0fxcffffef" :

Since "scalar bool array string 0fxcffffef" is returned by any undefined variabel. Sure dont look pretty though

Share this post


Link to post
Share on other sites

To form a metavariable you can write a function like this

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private["_i","_v"];

_i=0;

while"fomat[{myvar%1},_i]call funDefined"do{_i=_i+1;};

_v=format["myvar%1",_i];

call format["%1=objnull",_v];

_v

that will return a metaviariable name you can use filled with objNull so that it's not overwritten before you use it.

So if that was preProcesed as getVar:

_myVar=call getVar;

call format ["%1=(whatever unit)",_myVar];

myCommandString = format ["bla bla %1",_myVar];

Using the "scalar bool" is a method which is capable of crashing OFP. This is proven, but even without proof most scripters should realize you should stay away from comparing to a scalar address like this. tounge_o.gif

Share this post


Link to post
Share on other sites

Nice function and except for the funDefined fuction call the principle is the same as the one i wrote. So i might acctually be on the right track. Cool!

Quote[/b] ]

but even without proof most scripters should realize you should stay away from comparing to a scalar address like this.

Hey, I admited that it wasn´t pretty... now i feel realy hurt *sniff* sad_o.gif

biggrin_o.gif

Share this post


Link to post
Share on other sites

LOL,

well you can run with your defined method or a similar one if you areabsolutely sure you will never run the check on any large arrays or strings. smile_o.gif

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  

×