.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
Calling methods from different classes
Old
  (#1)
Zacker
Project Leader, Lead Level Designer, Waypointer
 
Status: Offline
Posts: 337
Join Date: Mar 2004
Location: Denmark
Default Calling methods from different classes - 12-06-2004

I have a method which can add a value to a variable. Calling it works fine, it adds the number to the variable and everything is good.
If the game calls the method from another class, then it looks like it adds it to a new variable.

Example:
Class1:
Add 50 to life. Life is now 50.
Add 50 more to life. Life is now 100.

Class2:
Add 20 to life. Life is now 20.
Add 50 to life. Life is now 70.

I dont understand how this can happen, as the variable only gets declared in the class with the aforemented method.

Edit:
The lang is Java.

Last edited by Zacker; 12-06-2004 at 22:29..
  
Reply With Quote
Re: Calling methods from different classes
Old
  (#2)
MusicMan
Member
 
Status: Offline
Posts: 236
Join Date: Feb 2004
Default Re: Calling methods from different classes - 12-06-2004

Code:
#include <iostream.h>
 
class Class1 //declaring first class
{
 
public:
 
int addvalues (int a, int b) { return (a+b); } //creating method in first class
};
 
class Class2 //declaring second class
{
public:
Class1 object1; // creating an object of Class1 in Class2
 
};
 
int main()
{
 
Class2 object2; // creating an object of Class2
 
cout << object2.object1.addvalues(5, 6); //calling method in Class1 using the object of Class1 declared in Class2
 
return 0; //ending program
 
}
Maybe this little piece of code I wrote can help you understand?

edit: added comments

Last edited by MusicMan; 12-06-2004 at 22:34..
  
Reply With Quote
Re: Calling methods from different classes
Old
  (#3)
Zacker
Project Leader, Lead Level Designer, Waypointer
 
Status: Offline
Posts: 337
Join Date: Mar 2004
Location: Denmark
Default Re: Calling methods from different classes - 12-06-2004

The method declaration lies within Race.Java
Quote:
//Justerer eller returnerer spillerens liv
public int fixliv(int a)
{
if(a!=0)
{
//Giver spilleren a mere i liv
liv = liv + a;
System.out.println("Du har nu " + liv + " i liv");
}
else
{
//Returnerer værdien af liv
return liv;
}
return 0;
}
First it gets called in Race.java
Quote:
fixliv(100);

Then I declare a new instance of the class Race in Sted.java:
Quote:
//Race
//Opret et objekt af klassen Race
Race race = new Race();
And then I call it in Sted.java:
Quote:
race.fixliv(10);
  
Reply With Quote
Re: Calling methods from different classes
Old
  (#4)
botman
Super Moderator
 
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
Default Re: Calling methods from different classes - 12-06-2004

"new Race()" creates a new object.

You have 2 different instances of the same type of class. Each of those 2 instances have their own member variables. Changing a member variable in one class won't change the same member variable in another class.

botman
  
Reply With Quote
Re: Calling methods from different classes
Old
  (#5)
Zacker
Project Leader, Lead Level Designer, Waypointer
 
Status: Offline
Posts: 337
Join Date: Mar 2004
Location: Denmark
Default Re: Calling methods from different classes - 12-06-2004

Thanks for the replies. After a good talk with Musicman on MSN, we were able to work it out.

The variable liv needs to be declared as static. That prevents what Botman explained from happening.

It is done the same way in both Java and c++:
Code:
static int liv;
  
Reply With Quote
Re: Calling methods from different classes
Old
  (#6)
Austin
Moderator
 
Austin's Avatar
 
Status: Offline
Posts: 403
Join Date: Nov 2003
Default Re: Calling methods from different classes - 13-06-2004

Think of it this way.

For classes...

When you declare a static member variable (or static member function) there is only ONE variable (or function) SHARED between all instances of that class currently instantiated at any given point in time.

Last edited by Austin; 13-06-2004 at 11:31..
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com