BigTito89 23 Newbie Poster

Although it would be cool if they are generated at runtime, I'm not going to bother with that yet. The are going to be predefined in a scripts folder.

BigTito89 23 Newbie Poster

Hello,


I'm creating a game editor and I'm at the current stage that I would like the ability to attach scripts to objects (by selecting the string of the method name to be called). That would be saved into the object's scripts to load / run array. That shouldn't be too difficult, the part I'm having trouble with is finding a method to dynamically call functions at runtime based on what script they need to run. Keep in mind this "script" is just C# code.

I need to run the scripts efficiently at game runtime and I can't have complicated lookups to the functions. Is there some way I can do this? I'm thinking delegates / reflection but I'm not sure how to implement any of it.

There's not really any code yet for this specifically but I'll provide whatever is deemed necessary for a reply.

BigTito89 23 Newbie Poster
double color[3] = {1,2,3}; //an array with 3 elements of type double

double *ptr; //create a pointer to a double

// ... assign ptr ....

ptr = color; // set pointer to color[0] (beginning of it) essentially
ptr++;//increment ptr so it points to color[1] which is 2
	  cout << "ptr " << *ptr;

This would produce 2.
The asterisk is known in a few different ways in C++ (feel free to correct me i'm learning still) , the operator to create a pointer, the dereference operator, and multiplication operator.

In this case, on line 9 I use the dereference operator which basically gets the value stored by the pointer and in this case it is 2.

Remembering pointers point to specific memory addresses, outputting only ptr with no dereference will return the address at color[1]. Again correct me if i'm wrong.

BigTito89 23 Newbie Poster

Thx for replying....I'm confused because line 31 has '}' so what line should it be in? And I'm confused about what you mean for the second loop.

Put one more bracket } right after that one on line 31, your first for loop never stops before you calculate average.

BigTito89 23 Newbie Poster

At the end of your example before the end of the return 0; put std::cin.get();

This will have the program wait for keyboard input from you before it is terminated.

So for example you have your Example.cpp file like this:

int main()
{

//blah blah
std::cin.get();
return 0;
}
Narue commented: Props for not suggesting getch, system("PAUSE"), or some other equally horrible solution. +18
BigTito89 23 Newbie Poster

Well first thing is, you don't have any constructors declared anywhere, a constructor is the 'function' that happens when you instantiate an instance of a class. So basically what you would need to add into your header file would be:

class Account
{
private: //what you had here before //

public:
     Account();//default constructor
     Account(int initialBalance){dataMember = initalBalance;};//constructor with parameter
//what you had here before//

};

And what not. Theres a default constructor which takes no arguments, and in your case you would want a constructor that takes an argument which sets the private data member (dataMember) to the parameter (initialBalance)

Just adjust these values to whatever your variables are.

I'd also recommend putting your function defintions for your class in a separate .cpp file. But that's just me.

EDIT: You don't necessarily need to put the default constructor in there if you're not going to use it, but it is usually good practice.

BigTito89 23 Newbie Poster

EDIT: Fixed my power function and the binaries are correct with the number. I guess all I'm asking now is how to get the right bit first instead of left side. I'm not sure how the binary operators work exactly.

void BinaryString::inttoBinary()
{
	bool bit = 0;
	int a=input.size();
	for(int i=a; i>0; i--)
	{
		//get right-most bit
		bit = binary & 1;
		if(bit)
			twocomp+= '1';
		else
			twocomp+='0';
		binary >>= 1;
	}

}

Yes, I know my power function isn't working properly, it seemed to be working for 3 - 00000011 and I tested another number and that worked so I figured I got it. the pow() in cmath yes I looked it up and implemented it all the way doesnt take an unsigned int, it only takes floats and doubles (and their long counterparts) that's why i couldn't figure it out. Plus I couldn't figure out what those reverse functions did because they threw errors, and yes I did include algorithm.

--
So what you're saying is I had to specifically ENTER the binary backwards (right to left) from the beginning? I was just trying to modify what your idea was and make it go from right to left hence starting at position 7 instead of 0.
e.g. int a=input.size()-1;

Also I'm not going to implement any functionality for the user to input the way they want it to read (left->right or left<-right) because I just don't have to do that and I don't have …

BigTito89 23 Newbie Poster

First id like to say thanks Clinton.
I had a bunch of issues trying to implement Clinton's code, mainly using the pow() function. So I made my own and everything seems to be "working" up until the point I try to do the integer to binary.

unsigned int BinaryString::power()
{
	unsigned int temp=1;
	std::cout << "power called with exponent " << xp-1;
	//loop through # of times to do exponent
	for(int i=0;i<xp-1;i++)
	{
		temp*=2;
	}
	return temp;
}

void BinaryString::doConverttoInt()
{
	int a=input.size()-1;//first digit right to left
	if(input[a] == '1'){
		number = 0;
	}
	for(int i=a;i>0;i--)//start at right
	{
		xp++;
		if(input[i] == '1')
		{
			//exponent via the position
			number+=power();
		}
		
	}
	orig_number = number;
	performComplement();
}


void BinaryString::inttoBinary()
{
	bool bit = 0;
	int a=input.size();
	for(int i=a; i>0; i--)
	{
		//get right-most bit
		bit = number & 1;
		if(bit)
			twocomp+= '1';
		else
			twocomp+='0';
		number >>= 1;
	}

}

When the constructor is passed this: 11011011
the result is: original # in binary 11011011
which is 91
the 2's compliment is: 10100101
which is: 16777215

I think the compliment is just flipped as in left to right rather than right to left. Any thoughts? It's gotta be something simple.

EDIT: Yeah that binary number isn't even 91 so my function doesn't even work right anyway. =\ More help++ ?

Thanks in advance.

BigTito89 23 Newbie Poster

It seems to work for me, I type: x : 0 : 0
colon representing hitting enter and it puts an x in top left.

EDIT: See you've posted something before this.
The way I have done it in the past, which isn't necessarily a good way, is just having an if statement checking each possible win condition. (8 of them)

BigTito89 23 Newbie Poster

You can't use the datatype when calling the functions, just get rid of the voids *on those lines only* not in the declaration/defintions, and I think that will solve that specific problem.

BigTito89 23 Newbie Poster

I'm not sure you ever define the function array_insert(), I don't see it defined anywhere, nor do you actually put any values into it. I'm not that good with c++ yet, but that's the first problem I see. Just my thoughts.

BigTito89 23 Newbie Poster

Hi,
I'm having a problem wrapping my head around a problem. Here is the problem:
Develop a program that will input a binary string and then, using bitwise operations, do a two's complement on the string and display it. Use UNSIGNED data types for the bit strings.

So far, I have a class that takes a c-string in the constructor and converts that to an integer using atoi() then I set a temporary variable to the ~ (complement) of the variable and add 1. But I don't really know if I'm even doing it right.

I call it like this:
BinaryString convert("00000010");

And it "works" like this:

BinaryString::BinaryString(char* in)
{
	binary=0;
	input=in;
	doConverttoInt();
}

void BinaryString::doConverttoInt()
{
	binary =( unsigned int)atoi(input);
	std::cout << "binary is " << binary << std::endl;
	performComplement();

}

void BinaryString::performComplement()
{
	
	//input
	unsigned int temp;
	temp = ~binary;
	temp+=1;
	std::cout << "complement is " << temp << std::endl;

}

This all just produces garbage, as atoi strips the leading 0's, and temp variable gives me some ridiculously high number.

Clinton Portis commented: I love performing bit-level operations..!! :) +5
BigTito89 23 Newbie Poster

Zjarek, thanks for the input but it seems the problem was simple than I thought. I took your advice for a state variable and compared that and modified the code accordingly. Didn't have to use a map nor the Enemy* idea.

But thank you anyway!

BigTito89 23 Newbie Poster

Hello, for my final team project for C++ we are making a simple space invaders clone with the SDL libraries.
Most of it is working great so far but in order to 'destroy' the enemies, they must not be drawn anymore and should be destructed.

ER is just the # of rows, same with EC - columns
Now the problem I'm having is when checking if(!enemies[r][c]==NULL)
which tells me:
error C2676: binary '==' : 'Enemy' does not define this operator or a conversion to a type acceptable to the predefined operator

// Show new enemy locations
for(int r=0; r<ER; r++)
{
	for(int c=0; c<EC; c++)
	{
		if(!enemies[r][c]==NULL)
		// Shows each enemy at their specific locations
		apply_surface(enemies[r][c].x,enemies[r][c].y,enemySprite,screen); 
	}
}

I figured that maybe I might have to try and overload the == operator but I'm not even sure if that's the right step nor do I know how to do it properly.

Any help appreciated, thanks.