Skip to main content
gamedev.net
🔒 Locked

Accessing Variables

Started by giant Apr 15, 2002 at 5:25 AM 5 replies 250+ views
Original Post
giant
giant
Howdy. Already, before I ask this question, I am guessing that the answer will be NO!!!!, but someone out there with superior programming skill might know a way. Here is my situtation. I am writing a game. Its pretty basic, but hey I''m having fun. The enemys in the game are event driven, ie. they responde to thing that happen to them, (what they can see, if things hit into them etc...) To control these badies, I have written a basic scripting language and a parser, so that the user/developer can reprogram the enemy without having to recompile the game. For each badie in the game there will be an object, and an associated script. Presume the following. The object has the following member variable int iXCoor; and in the control script there is the line iXCoor = 7; When I parse the script, the parser will recgonise this as an assignment statement parse it into a destination and an expression. char* destination = iXCoor; int expression = 7; Is there any way that I can then access the member variable which name is held in destination. if I wrote enemy.destination, I will get an error cause enemy has no member called destination. In short, is there any way that I can access a variable, when I have a string, which contains the name of the variable that I wish to access. Thanks a lot Ciaran "Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." --Albert Einstein
SabreMan
SabreMan
quote:
Original post by giant
The object has the following member variable

int iXCoor;

and in the control script there is the line

iXCoor = 7;

When I parse the script, the parser will recgonise this as an assignment statement parse it into a destination and an expression.

OK.
quote:

char* destination = iXCoor;
int expression = 7;

You mean char* destination = "iXCoor", presumably.
quote:

Is there any way that I can then access the member variable which name is held in destination.

We are talking about C++, aren''t we? I''ll make that assumption. The simple answer is as you suspected - NO! However, there are other ways to achieve what you are trying to do. The simplest would probably be to provide an accessor function which goes something like this:


  
class Thing
{
public:
void setVar( std::string varname, int val )
{
if(varname == "destination")
setDestination(val);
else if( varname == ...)
set...(val);
}
private:
void setDestination(int val)
{
destination = val;
}

int destination;
};


The other way you could do it is to have a std::map x-ref of variable names versus values, which would mean that you would only need one function to set the values, rather than one per variable.
giant
giant
Sorry about the lack of information Guys. I was so concerned with my proplem that I forgot the details.

Yeah, I''m using C++. The compile that I am using is MS VC++ Version 6.

Thanks for ye''re help. I will look into it now.

Ciaran

"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." --Albert Einstein
Jeff D
Jeff D
I hope I understand you a little but you want to have the user make a variable with a name and access it? If so you could make a struct like this:


struct ObjectData
{

string dataName;
int Data;

};

vector< ObjectData >;


Of course the vector will be in the Object.
When user makes variable and names it it will go into the vector of ObjectData. I hope this helped.

Or if you wish to make it so it can contain more than just ints make it a templated class:

  

template <class T> class CData
{

public:
//Constructors -- Deconstructor

CData(){}; //Constructor

~CData(){}; //Deconstructor


//Mis. functions

void SetAll(string szName, T &Data) //Sets all the Data to the Data

{

m_szName = szName;
m_Data = Data;

}
void SetData(T &Data){m_Data = Data;} //Sets just the Data to CData


//accessor functions

T GetData() const{return m_Data;} //Returns the data

string GetName() const{return m_szName;} //Returns the Name of the data


private:
string m_szName; //The name of the variable or string

T m_Data; //The data that this CData holds


};//end of CData



Jeff D


[edited by - Jeff D on April 15, 2002 7:38:36 AM]
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
SabreMan
SabreMan
Jeff D: what the hell are you on about?
Jeff D
Jeff D
quote:
Original post by SabreMan
Jeff D: what the hell are you on about?



hmmm? Something wrong with what I posted?? If you read the begginning I say

quote:
I hope I understand you a little but you want to have the user make a variable with a name and access it?


That meant I thought what he meant was that. I'm so very sorry that I mistaken him. It was 3am.

Jeff D


[edited by - Jeff D on April 15, 2002 3:42:32 PM]
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.