Jump to content
ZaellixA

Convert string to variable

Recommended Posts

Hey all, back here with a question. I am looking for a way to get access to some variables which have the same name with an increasing numerical suffix.

 

I tried to do something like this but to no avail

 

// Trying to get access to objects named (in the editor) obj0, obj1, obj2, obj3, etc.

private _maxIdx = 4; // I get this from somewhere else but I define it here for completeness

// Increase an index
for[{private _i = _0}, {_i < _maxIdx}, {_i = _i + 1}] do {
  // Create the name of the variable
  private _var = "obj" + (str _i); // Parse the name of the variable as a string
  _var = call (compile _var); // Get the object variable

  // Get the position of the object
  _secPos set[_i, getPosATL _var]; // Get the position of the object placed in the sector

  // Delete the object placed at the sector objective
  deleteVehicle _var;
};

This doesn't seem to work. The only evidence I have (haven't done the testing myself unfortunately) is that the objects (obj0, obj1, obj2, etc.) are not deleted in the editor.

 

Any ideas or insights are most welcome :).

Share this post


Link to post
Share on other sites
Quote

private _var = call compile format["%1", "obj" + str _i];

 

Edited by RCA3
Bad code
  • Like 2

Share this post


Link to post
Share on other sites
21 minutes ago, RCA3 said:

private _var = call compile format["%1", "obj" + str(_i)];

 

Hhhmmm... why should this work though? What I am doing is actually pretty much the same in two steps, 1) _var = "obj" + str _i, and then call compile _var.

 

Am I missing something here?

 

As a note, I haven't tested your solution yet, so I can't say whether it works or not. Thanks though :).

Share this post


Link to post
Share on other sites
// Increase an index
for[{private _i = _0}, {_i < _maxIdx}, {_i = _i + 1}] do {
                                      
  // Create the name of the variable
  private _var = "obj" + (str _i); // Parse the name of the variable as a string
                                      
  _var = missionNamespace getVariable [_var , objNull]; // *NEW* Get the object variable

  // Get the position of the object
  _secPos set[_i, getPosATL _var]; // Get the position of the object placed in the sector

  // Delete the object placed at the sector objective
  deleteVehicle _var;
};

 

  • Like 2

Share this post


Link to post
Share on other sites
Just now, Harzach said:

// Increase an index
for[{private _i = _0}, {_i < _maxIdx}, {_i = _i + 1}] do {
                                      
  // Create the name of the variable
  private _var = "obj" + (str _i); // Parse the name of the variable as a string
                                      
  _var = missionNamespace getVariable [_var , objNull]; // Get the object variable

  // Get the position of the object
  _secPos set[_i, getPosATL _var]; // Get the position of the object placed in the sector

  // Delete the object placed at the sector objective
  deleteVehicle _var;
};

 

Aha...!!! missionNamespace... of course!!!

 

Haha, to be honest, I didn't know that all the objects are saved in the mission namespace as variables. Thanks a lot, I'll give it a try and post back with results.

 

Really appreciated!

Share this post


Link to post
Share on other sites
1 hour ago, RCA3 said:

private _var = call compile format["%1", "obj" + str(_i)];

 

Well, it seems that this is working (I am having my bro do the testing and let me know :D). Not sure why this works and the one I had does not. If you have any idea, please shed some light...

Share this post


Link to post
Share on other sites

@Harzach, what Killzone Kid is talking about in there is unrelated. You'd need an expert on that matter: "Secure Callback", but in here we're not sending data over the network, we're just converting a string (compiling code) locally. The danger there as far as I can explain is the client (potential hacker) defining he's own variable locally and then publicVariable'ing it with malicious code into our call compile'd function, executing it through an addPublicVariableEventHandler.

 

@ZaellixA

Here's another way 😁:

Quote

private _var = call compile ("obj" + str _i);

 

I guess yours is not working because you're using the same variable for defining the compiled code as the variable you're compiling 😁:

Quote

private _var = "obj" + (str _i); // Parse the name of the variable as a string
_var = call (compile _var); // Get the object variable

private _var = "obj" + (str _i) ; // Parse the name of the variable as a string
private _obj  = call (compile _var); // Get the object variable

Cheers.

Edited by RCA3
Bad code. Bad explanation.
  • Like 3

Share this post


Link to post
Share on other sites

Hey, thanks for the answer @RCA3. I too think that the problem with format is only related to KK's custom callback system.

 

I have done some variable reuse/reassignment (with different types) in the past with no problems. This shouldn't cause any issues as far as I understand the way it works. You may be right though and I will have another look into it.

 

Nevertheless, thank you all for the contributions :).

Share this post


Link to post
Share on other sites
16 hours ago, RCA3 said:

private _var = call compile format["%1", "obj" + str _i];

 

please don't recommend that to people I see that garbage too often. Just use getVariable. compile has bad performance and was once even a factor in the famous 3FPS bug, please only use it if there is no alternative.

  • Like 3

Share this post


Link to post
Share on other sites
2 hours ago, RCA3 said:

Well... i'm sorry then @ZaellixA, @Harzach, @Dedmen and the community. I clearly don't have a clue what i'm talking about.

Cheers.

Hey, no worries mate. You are trying to help here, nothing bad happened here. In the end, we all came out of this just a wee bit wiser 8).

 

9 hours ago, Dedmen said:

please don't recommend that to people I see that garbage too often. Just use getVariable. compile has bad performance and was once even a factor in the famous 3FPS bug, please only use it if there is no alternative.

Thanks Dedmen for the info, really appreciated.

  • Like 2

Share this post


Link to post
Share on other sites
3 hours ago, RCA3 said:

Well... i'm sorry then @ZaellixA, @Harzach, @Dedmen and the community. I clearly don't have a clue what i'm talking about.

Cheers.

 

Like @ZaellixA said, it's cool. You're a helpful and engaging member of the community.

  • Like 2

Share this post


Link to post
Share on other sites

Hey all, finally found out what the "problem" was... Nothing! The person that was performing the tests didn't create the objects in the editor correctly and this is why they were not deleted...

 

Thank you all for your contributions.

 

Just for the sake of completeness, I tried all ways proposed here and they all had the same results. Haven't performed any efficiency testing, so I can't say which one is faster, but as proposed here, the use of compile is discouraged (thanks Dedmen and Harzach).

  • Haha 3

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

×