Mudbill
Muderator
Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation:
179
|
String reference within constant name (Advanced)
Alright so this might be a little difficult to do. I'll try to explain what the situation is, but you might have a hard time understanding it because it heavily relies on many aspects. This is more targeted towards C++/AngelScript itself rather than Amnesia alone, but it should work in Amnesia as well.
Basically:
How can I reference a string variable within the name of a boolean constant?
I have a situation where it would really help me out if I was able to input the string name of a current caller (for instance using asEntity to reference the current entity) into the name of a pre-defined boolean variable.
Let me attempt an example of something I know works:
void RemoveAnItem(string &in asItem)
{
if(aBoolean == true) RemoveItem(asItem + "_1");
}
This would simply remove the item that is named the same as the item calling it, but with the _1 extension. If the item calling it is named "LibraryKey" then the item removed would be named "LibraryKey_1"
Here I'm adding the extension to the item name.
So far so good. But here's what I would need.
void RemoveAnItem(string &in asItem)
{
if(aBoolean + _1 == true) RemoveItem(asItem);
}
Here I'm adding the extension to the boolean, not the item name. This script won't work for obvious reasons. What I need though, is to change the questioned boolean name into something else. What is asked here is the boolean result, but I need a different boolean's result than the one originally questioned.
I think what I need has something to do with converting a boolean to a string, but the boolean name, not the result.
Let me try another example:
bool x3y3 = false;
bool x3y4 = false;
bool x3y5 = false;
if(asArea == "area_x3y3") x3y3 = true;
if(asArea == "area_x3y4") x3y4 = true;
if(asArea == "area_x3y5") x3y5 = true;
This is something that will work, but is very tedious with many booleans. What would be preferred is to make the boolean name change depending on asArea's name. Using the StringSub function here could possibly return the string from asArea ("area_x3y3") and make a new string which is just "x3y3" however I would need to convert that new string into a boolean name.
If I'm able to do that, I'm already half way. I think this is possible, I just haven't found it yet.
But even if I'm able to convert "area_x3y3" into "x3y3" and reference that as a boolean name, I'll still need to slightly modify that name before referencing it. Ultimately, I'd need to modify it into "x3y3_e" as well as _w, _n and _s (East, west, north, south). I won't mind scripting it to check again 3 more times for each direction, but I'd rather not have to manually script it to check again 400 more times for each co-ordinate and direction (by the way, the boolean is named x3y3 because it's the X=3,Y=3 co-ordinate of a grid).
If you have any sources for constant conversions and references within C++, that'd be great. It's late and I've been messing with this for a few hours, so hopefully my explanation isn't super hard to understand.
Whew.
|
|
03-09-2014, 01:56 AM |
|
PutraenusAlivius
Posting Freak
Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation:
119
|
RE: String reference within constant name (Advanced)
What if instead of the name of x3y3, x3y4 and so on, what if it's x3y_3, x3y_4 instead?
"Veni, vidi, vici."
"I came, I saw, I conquered."
|
|
03-09-2014, 02:08 AM |
|
Mudbill
Muderator
Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation:
179
|
RE: String reference within constant name (Advanced)
Well, since the different booleans don't just change at the last number, that won't make much of a difference. I have booleans named x1y1, x2y1, x3y1 as well as x4y1, x4y2, x4y3. Both numbers vary.
If this was a string, I could've done something like to manipulate both numbers easily. Perhaps I just need to convert that string into the name of the boolean.
(This post was last modified: 03-09-2014, 02:14 AM by Mudbill.)
|
|
03-09-2014, 02:12 AM |
|
PutraenusAlivius
Posting Freak
Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation:
119
|
RE: String reference within constant name (Advanced)
Wait can you increase boolean numbers? Because you could do;
bool P +e, bool N +i, bool S
instead.
"Veni, vidi, vici."
"I came, I saw, I conquered."
|
|
03-09-2014, 02:17 AM |
|
Mudbill
Muderator
Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation:
179
|
RE: String reference within constant name (Advanced)
Well, booleans only have 2 different values (0 = false, anything but 0 = true). What would these e and i placeholders be? I think I can work with the value of the boolean, but the script would be a lot simpler if I could modify the boolean name in the way I want.
Edit: I'm unsure if what I'm trying is even possible because C++ is mostly compile-time. Changing these things may not work on run-time.
I tried having a string with the same name as the boolean, but it's treated separately. The string returns true but the boolean does not recognize it.
(This post was last modified: 03-09-2014, 02:38 AM by Mudbill.)
|
|
03-09-2014, 02:23 AM |
|
PutraenusAlivius
Posting Freak
Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation:
119
|
RE: String reference within constant name (Advanced)
(03-09-2014, 02:23 AM)Mudbill Wrote: Well, booleans only have 2 different values (0 = false, anything but 0 = true). What would these e and i placeholders be? I think I can work with the value of the boolean, but the script would be a lot simpler if I could modify the boolean name in the way I want.
Edit: I'm unsure if what I'm trying is even possible because C++ is mostly compile-time. Changing these things may not work on run-time.
I tried having a string with the same name as the boolean, but it's treated separately. The string returns true but the boolean does not recognize it.
These e and i are indexes. As the X or Y increases, the e and i also increase.
"Veni, vidi, vici."
"I came, I saw, I conquered."
|
|
03-09-2014, 02:52 AM |
|
Daemian
Posting Freak
Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation:
49
|
RE: String reference within constant name (Advanced)
If I understand correctly your scenario, you just want an action, like remove an item,
if a variable, like x1y1 is set true, and matches with the item's number stored in its name.
I just tested and this is how it worked for me:
//public arrays x and y, size 10, false by default. bool[] x (10, false); bool[] y (10, false);
void RemoveAnItem ( string &in asItem ) { string strIndex = StringSub( asItem, 4, 10 ); int iIndex = GetIndex( strIndex ); if (x[iIndex] and y[iIndex]) { RemoveItem( asItem ) } }
I'm sure you understand what that function is doing, you're even gonna improve it. Btw, you need this function I just did:
int GetIndex ( string &in strIndex ) { int aiMax = 10; for (int i=0;i<aiMax;i++) { if ("a"+i == "a"+strIndex) { return i;} } return 0; }
So, if you pass RemoveItem("item5"), the function checks x[5] and y[5], if both are true, item5 is removed.
I don't know how you planned to change these variables named x2y3, etc, but it's the same way for x[] and y[].
Am I missing something? Oh the variables are all set to check to 10, you may want to change that.
|
|
03-09-2014, 11:23 AM |
|
Mudbill
Muderator
Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation:
179
|
RE: String reference within constant name (Advanced)
Thank you for taking your time ^^
I just hope it wasn't wasted. Well, I'm sure I can learn something from this either way.
The RemoveItem I had was just for the example. My actual situation involves a 5x5 grid of rooms, where each room has a door on each wall (n, s, e, w). I haven't quite gotten the hang of arrays yet, so I made a boolean for each room+dir (Ex: x1y1_s, x5y5_n etc). For each room, I have an area named "area_room" which is used to trigger what doors should open in each room. It's quite difficult to explain without explaining how the whole puzzle looks, works and solves.
The script you posted is slightly above my experiences, but I can make sense of it. Basically I already have the booleans I need, but I need a way of telling the code which boolean to change and which not to. I can do this with a bunch of if-statements or a very long switch-statement but I feel that's unnecessary and I'd rather use a more compact method. If I could input the boolean that has the name (excluding dir) of the area it's associated with, then perhaps I could also add the dir on top of that name before asking the question of what boolean it is.
(This post was last modified: 03-09-2014, 04:51 PM by Mudbill.)
|
|
03-09-2014, 04:50 PM |
|
|