Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » C++ help question
C++ help question [message #380533] Wed, 15 April 2009 16:56 Go to next message
Xpert is currently offline  Xpert
Messages: 1588
Registered: December 2005
Location: New York City
Karma: 0
General (1 Star)
I'm trying to use a certain code that's in one .cpp file to be used in another .cpp file. How do I go about doing that?

I have this in gmmain.cpp
struct VeteranPlayers {
	std::string PlayerName;
	int VeteranPoints;
};

std::vector<VeteranPlayers> VetInfo;


int VetCheckPoints(int ID) {
	if (!VetInfo.empty()) {
		for (int i = 0; i < VetInfo.size(); i++) {
			if (VetInfo[i].PlayerName == Get_Player_Name_By_ID(ID)) {
				int Points;
				Points = VetInfo[i].VeteranPoints;
				return Points;
			}
		}
	}
	return 0;
}


I'm trying to get this to work also in gmscripts.cpp. Anyone can help me do that?


http://i32.photobucket.com/albums/d42/XpertMaverick/xpertyankee.jpg

Creator of NetGuard, an IRC network regulator.
Developer of the CloudyServ 0.982-X project.
Developer of the CloudyServ Ren-X bot.

Part time streamer - https://twitch.tv/gg_wonder
Re: C++ help question [message #380548 is a reply to message #380533] Wed, 15 April 2009 18:24 Go to previous messageGo to next message
Zuess is currently offline  Zuess
Messages: 16
Registered: August 2004
Karma: 0
Recruit
in the .h file, create the structure.

Then whatever .cpp file you want to use your function in, do an

#include <original_file.h>
/* in gmmain.h */


struct VeteranPlayers {
	std::string PlayerName;
	int VeteranPoints;
};

std::vector<VeteranPlayers> VetInfo;
int VetCheckPoints(int ID);




in your code
#include <gmain.h>

void whateverfunction(int ID)
{
   int points = VetCheckPoints{ID);
   /* DO SOMETHING */

}


Ill double check my syntax when I get home
Re: C++ help question [message #380765 is a reply to message #380533] Thu, 16 April 2009 19:00 Go to previous messageGo to next message
Xpert is currently offline  Xpert
Messages: 1588
Registered: December 2005
Location: New York City
Karma: 0
General (1 Star)
Had errors ;\

Maybe I can get reborn to help me since it is his code.


http://i32.photobucket.com/albums/d42/XpertMaverick/xpertyankee.jpg

Creator of NetGuard, an IRC network regulator.
Developer of the CloudyServ 0.982-X project.
Developer of the CloudyServ Ren-X bot.

Part time streamer - https://twitch.tv/gg_wonder
Re: C++ help question [message #380774 is a reply to message #380765] Thu, 16 April 2009 20:54 Go to previous messageGo to next message
nopol10 is currently offline  nopol10
Messages: 1043
Registered: February 2005
Location: Singapore
Karma: 0
General (1 Star)
In the cpp file where you want to call VetCheckPoints, put in the line

extern int VetCheckPoints(int ID);





nopol10=Nopol=nopol(GSA)

http://i14.photobucket.com/albums/a330/napalmic/siggy.jpg
Re: C++ help question [message #380792 is a reply to message #380533] Fri, 17 April 2009 04:02 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
If you're trying to migrate this back into SSGM because you want it to work with vehicles then I would suggest keeping it as a plugin.
You could add a void reb_vet_System::Custom(GameObject *obj, int message, int param, GameObject *sender) {
And on the custom check to see if they entered a vehicle.
On the objecthookcall vehicles are already hooked in that plug-in, you could attach a vet script similar to that of the player version to vehicles there. But don't forget people will try to exploit this as much as possible... (If they get in the vehicle and set it's hea;th/armor higher, remember to set it back when they exit).



Re: C++ help question [message #380840 is a reply to message #380533] Fri, 17 April 2009 11:58 Go to previous messageGo to next message
Xpert is currently offline  Xpert
Messages: 1588
Registered: December 2005
Location: New York City
Karma: 0
General (1 Star)
I'm not bothered by the health and armor. I'm trying to recreate the mod that we use on our server. There were too many modifications for me to keep it as a plugin because it required so much stuff from my main code. I edited the veteran system a lot and even had it work for CTF mode flag events.

This is what I'm trying to pull off. Maybe you can get the idea. I placed this in gmscripts.cpp


void MDB_SSGM_Vehicle::Custom(GameObject *obj, int message, int param, GameObject *sender) {
	int Points;
	Points = VetCheckPoints(Get_Player_ID(sender));
	float health = Commands->Get_Max_Health(obj);
	float armor = Commands->Get_Shield_Strength(obj);

	
	if (message == CUSTOM_EVENT_VEHICLE_ENTER) {
		if (Settings->EnableVehOwn && Get_Vehicle_Owner(obj) == sender && !Find_My_Veh(sender)) {
			Commands->Attach_Script(obj,"MDB_SSGM_Vehicle_Owner",StrFormat("%d,%d",Commands->Get_ID(sender),Get_Object_Type(sender)).c_str());
			Console_Input(StrFormat("ppage %d Your vehicle has been auto-bound to you. Use !unbind to relinquish ownership. Use !lock to prevent your teammates from stealing the vehicle.",Get_Player_ID(sender)).c_str());
			if (Points >= 120) {
				Set_Max_Health(obj, health*1.40f);
				Set_Max_Shield_Strength(obj, armor*1.40f);
				Attach_Script_Once(obj,"c_Regen","3,1,8");
			}
			else if (Points >= 90 && Points < 120) {
				Set_Max_Health(obj, health*1.30f);
				Set_Max_Shield_Strength(obj, armor*1.30f);
				Attach_Script_Once(obj,"c_Regen","4,1,6");
			}
			else if (Points >= 60 && Points < 90) {
				Set_Max_Health(obj, health*1.20f);
				Set_Max_Shield_Strength(obj, armor*1.20f);
				Attach_Script_Once(obj,"c_Regen","4,1,4");
			}
			else if (Points >= 30 && Points < 60) {
				Set_Max_Health(obj, health*1.10f);
				Set_Max_Shield_Strength(obj, armor*1.10f);
				Attach_Script_Once(obj,"c_Regen","4,1,2");
			}
			Commands->Set_Health(obj, Commands->Get_Max_Health(obj));
			Commands->Set_Shield_Strength(obj, Commands->Get_Max_Shield_Strength(obj));
		}
	}
}


This is what I'm trying to do thus why I'm asking how to use a function from another cpp file. And I'm having no luck right now because I'm having crash issues or code errors.


http://i32.photobucket.com/albums/d42/XpertMaverick/xpertyankee.jpg

Creator of NetGuard, an IRC network regulator.
Developer of the CloudyServ 0.982-X project.
Developer of the CloudyServ Ren-X bot.

Part time streamer - https://twitch.tv/gg_wonder
Re: C++ help question [message #380841 is a reply to message #380533] Fri, 17 April 2009 12:44 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
If you need to use the function from another .cpp file then add
extern <function here>;

before you use the function.

If you're really struggling, then hit me up on MSN and i'll remote to you.
Would be nice if you considered releasing what you've made. In Love



Re: C++ help question [message #380850 is a reply to message #380841] Fri, 17 April 2009 13:05 Go to previous messageGo to next message
Genesis2001
Messages: 1397
Registered: August 2006
Karma: 0
General (1 Star)
reborn wrote on Fri, 17 April 2009 12:44

If you need to use the function from another .cpp file then add
extern <function here>;

before you use the function.

If you're really struggling, then hit me up on MSN and i'll remote to you.
Would be nice if you considered releasing what you've made. In Love



This is what headers are for ... lol

<3 Master Includes file!
Re: C++ help question [message #380851 is a reply to message #380841] Fri, 17 April 2009 13:09 Go to previous messageGo to next message
jnz is currently offline  jnz
Messages: 3396
Registered: July 2006
Location: 30th century
Karma: 0
General (3 Stars)
reborn wrote on Fri, 17 April 2009 20:44

If you need to use the function from another .cpp file then add
extern <function here>;

before you use the function.

If you're really struggling, then hit me up on MSN and i'll remote to you.
Would be nice if you considered releasing what you've made. In Love



extern on a function decloration isn't needed.

/*my_script.h*/
void foo();


/*my_script.cpp*/
void foo()
{
   for(int i = 0; i < 10; i++);
}


/*any other file*/
#include "my_script.h"

...

foo();
Re: C++ help question [message #381736 is a reply to message #380533] Tue, 21 April 2009 14:03 Go to previous messageGo to next message
Xpert is currently offline  Xpert
Messages: 1588
Registered: December 2005
Location: New York City
Karma: 0
General (1 Star)
Okay, I'm completely stomped. I tried so many things and still end up back to square one. This time I went to debug the scripts.dll. It points to 2 parts of the code and it makes no sense to me.


GMMAIN.CPP

struct VeteranPlayers {
        std::string PlayerName;
        int VeteranPoints;
};
 
std::vector<VeteranPlayers> VetInfo;
 
 
int VetCheckPoints(int ID) {
        if (!VetInfo.empty()) {
                for (int i = 0; i < VetInfo.size(); i++) {
                        HERE --> if (VetInfo[i].PlayerName == Get_Player_Name_By_ID(ID)) {
                                int Points;
                                Points = VetInfo[i].VeteranPoints;
                                return Points;
                        }
                }
        }
        return 0;
}



GMMAIN.H

int VetCheckPoints(int ID);



GMSCRIPTS.CPP

void MDB_SSGM_Vehicle::Custom(GameObject *obj, int message, int param, GameObject *sender) {
        HERE -->int Points = VetCheckPoints(Get_Player_ID(sender));
        float health = Commands->Get_Max_Health(obj);
        float armor = Commands->Get_Shield_Strength(obj);
 
        if (message == CUSTOM_EVENT_VEHICLE_ENTER) {
                if (Settings->EnableVehOwn && Get_Vehicle_Owner(obj) == sender && !Find_My_Veh(sender)) {
                        Commands->Attach_Script(obj,"MDB_SSGM_Vehicle_Owner",StrFormat("%d,%d",Commands->Get_ID(sender),Get_Object_Type(sender)).c_str());
                        Console_Input(StrFormat("ppage %d Your vehicle has been auto-bound to you. Use !unbind to relinquish ownership. Use !lock to prevent your teammates from stealing the vehicle.",Get_Player_ID(sender)).c_str());
                        if (Points >= 120) {
                                Set_Max_Health(obj, health*1.40f);
                                Set_Max_Shield_Strength(obj, armor*1.40f);
                                Commands->Set_Health(obj, Commands->Get_Max_Health(obj));
                                Commands->Set_Shield_Strength(obj, Commands->Get_Max_Shield_Strength(obj));
                                Attach_Script_Once(obj,"cAMpa_Regen","3,1,8");
                        }
                        else if (Points >= 90 && Points < 120) {
                                Set_Max_Health(obj, health*1.30f);
                                Set_Max_Shield_Strength(obj, armor*1.30f);
                                Commands->Set_Health(obj, Commands->Get_Max_Health(obj));
                                Commands->Set_Shield_Strength(obj, Commands->Get_Max_Shield_Strength(obj));
                                Attach_Script_Once(obj,"cAMpa_Regen","4,1,6");
                        }
                        else if (Points >= 60 && Points < 90) {
                                Set_Max_Health(obj, health*1.20f);
                                Set_Max_Shield_Strength(obj, armor*1.20f);
                                Commands->Set_Health(obj, Commands->Get_Max_Health(obj));
                                Commands->Set_Shield_Strength(obj, Commands->Get_Max_Shield_Strength(obj));
                                Attach_Script_Once(obj,"cAMpa_Regen","4,1,4");
                        }
                        else if (Points >= 30 && Points < 60) {
                                Set_Max_Health(obj, health*1.10f);
                                Set_Max_Shield_Strength(obj, armor*1.10f);
                                Commands->Set_Health(obj, Commands->Get_Max_Health(obj));
                                Commands->Set_Shield_Strength(obj, Commands->Get_Max_Shield_Strength(obj));
                                Attach_Script_Once(obj,"cAMpa_Regen","4,1,2");
                        }
                }
        }
}



When I debugged it, it pointed to the parts I put "HERE -->" above, as being the problem.

I don't know what to do. This is frustrating me >.>



http://i32.photobucket.com/albums/d42/XpertMaverick/xpertyankee.jpg

Creator of NetGuard, an IRC network regulator.
Developer of the CloudyServ 0.982-X project.
Developer of the CloudyServ Ren-X bot.

Part time streamer - https://twitch.tv/gg_wonder

[Updated on: Tue, 21 April 2009 14:05]

Report message to a moderator

Re: C++ help question [message #381777 is a reply to message #380533] Tue, 21 April 2009 15:34 Go to previous messageGo to next message
cAmpa is currently offline  cAmpa
Messages: 597
Registered: March 2006
Karma: 0
Colonel
#include "GMMAIN.H"

Bückstabü!
Re: C++ help question [message #381845 is a reply to message #381777] Tue, 21 April 2009 23:39 Go to previous messageGo to next message
Xpert is currently offline  Xpert
Messages: 1588
Registered: December 2005
Location: New York City
Karma: 0
General (1 Star)
cAmpa wrote on Tue, 21 April 2009 18:34

#include "GMMAIN.H"


It is included otherwise it wouldn't compile, doh?


http://i32.photobucket.com/albums/d42/XpertMaverick/xpertyankee.jpg

Creator of NetGuard, an IRC network regulator.
Developer of the CloudyServ 0.982-X project.
Developer of the CloudyServ Ren-X bot.

Part time streamer - https://twitch.tv/gg_wonder
Re: C++ help question [message #381848 is a reply to message #380533] Tue, 21 April 2009 23:53 Go to previous messageGo to next message
jnz is currently offline  jnz
Messages: 3396
Registered: July 2006
Location: 30th century
Karma: 0
General (3 Stars)
if (VetInfo[i].PlayerName == Get_Player_Name_By_ID(ID)) {


Very bad
Use:
const char *pName = Get_Player_Name_By_ID(ID);
if(!pName) return 0;
if (strcmp(VetInfo[i].PlayerName.c_str(), pName) == 0) {
...
}
delete []pName;


int Points = VetCheckPoints(Get_Player_ID(sender));
float health = Commands->Get_Max_Health(obj);
float armor = Commands->Get_Shield_Strength(obj);
 
if (message == CUSTOM_EVENT_VEHICLE_ENTER) {



Bad
Use:
if(!Commands->Is_A_Star(sender)) return;

int Points = VetCheckPoints(Get_Player_ID(sender));
float health = Commands->Get_Max_Health(obj);
float armor = Commands->Get_Shield_Strength(obj);
 
if (message == CUSTOM_EVENT_VEHICLE_ENTER) {

Re: C++ help question [message #382011 is a reply to message #381848] Wed, 22 April 2009 16:20 Go to previous message
Xpert is currently offline  Xpert
Messages: 1588
Registered: December 2005
Location: New York City
Karma: 0
General (1 Star)
Omg, I f-ing love you. THANK YOU! It works YAY =D

http://i32.photobucket.com/albums/d42/XpertMaverick/xpertyankee.jpg

Creator of NetGuard, an IRC network regulator.
Developer of the CloudyServ 0.982-X project.
Developer of the CloudyServ Ren-X bot.

Part time streamer - https://twitch.tv/gg_wonder
Previous Topic: [Skin Request]
Next Topic: RenX ERROR
Goto Forum:
  


Current Time: Mon Dec 30 06:26:35 MST 2024

Total time taken to generate the page: 0.00975 seconds