Jump to content

Calling methods from another class


Zirnek

Recommended Posts

I want to call a method from my Main class from a method inside Another class, then have that method from Another class executed back in the Main class's onLoop() method.

How can I do this? I've tried extending Main class, but that just crashes the script, and using static references obviously won't work for instanced one.

Link to comment
Share on other sites

26 minutes ago, Kayle7 said:

I want to call a method from my Main class from a method inside Another class, then have that method from Another class executed back in the Main class's onLoop() method.

How can I do this? I've tried extending Main class, but that just crashes the script, and using static references obviously won't work for instanced one.

What I hear from this is that your design is not correct, scripts made in java just follow the general java conventions.
Like when you are programming OOP, there should be 0 reason to call a method from the object to where you instanced the object.

Without showing any code it;s kinda hard to see what you are trying to do. so enlighten us and we will see what I can do for you

Link to comment
Share on other sites

10 minutes ago, Khaleesi said:

What I hear from this is that your design is not correct, scripts made in java just follow the general java conventions.
Like when you are programming OOP, there should be 0 reason to call a method from the object to where you instanced the object.

Without showing any code it;s kinda hard to see what you are trying to do. so enlighten us and we will see what I can do for you

Say I have this in my Main class:

private String loggerString ="";

public void status(String log) {
		log(log);
		loggerString=log;
	}

And I want to execute this in onLoop() as I write a method to do something in another class like safeSpot() in class Safespot.

Link to comment
Share on other sites

2 hours ago, Kayle7 said:

Say I have this in my Main class:

private String loggerString ="";

public void status(String log) {
		log(log);
		loggerString=log;
	}

And I want to execute this in onLoop() as I write a method to do something in another class like safeSpot() in class Safespot.

Make it a public static void 

and then you can call it from your other class with MainClass.status("test")

  • Like 1
Link to comment
Share on other sites

3 hours ago, Khaleesi said:

Make it a public static void 

and then you can call it from your other class with MainClass.status("test")

As I already mentioned at the start of my post, you cannot lump in static method with instanced one.

public static void status(String log) {
		log(log); //<---INSTANCED method
		loggerString=log; 
	}

This will not work.

3 hours ago, Gunman said:

@Kayle7 Do what Khaleesi said or pass the instance to the class you want to call it from

I cannot move it over to every class I want to use it in, that's inefficient and won't work as I need the String "loggerString" to be used for onPaint(g).

Edited by Kayle7
Link to comment
Share on other sites

3 hours ago, Kayle7 said:

As I already mentioned at the start of my post, you cannot lump in static method with instanced one.

public static void status(String log) {
		log(log); //<---INSTANCED method
		loggerString=log; 
	}

This will not work.

I cannot move it over to every class I want to use it in, that's inefficient and won't work as I need the String "loggerString" to be used for onPaint(g).

I don't think you understand what you are trying to do yourself at this point.

You are trying to fix something thats completely misdesigned, it's like putting boat parts in a car and expecting it to work :D
I also said to post some code, then you post 5 lines of code which could be anything in the world and you expect to get a proper answer -_-OOP 


I suggest you to learn java OOP properly to begin with :)

Edited by Khaleesi
Link to comment
Share on other sites

2 hours ago, Kayle7 said:

I cannot move it over to every class I want to use it in, that's inefficient and won't work as I need the String "loggerString" to be used for onPaint(g).

Static references in general are usually really bad in scripting, and I want to avoid much as possible.

Welcome to OOP, it's shit

Link to comment
Share on other sites

9 minutes ago, Khaleesi said:

You are trying to fix something thats completely misdesigned, it's like putting boat parts in a car and expecting it to work :D
I also said to post some code, then you post 5 lines of code which could be anything in the world and you expect to get a proper answer -_-


I suggest you to learn java properly to begin with :)
This is very basic stuff you should know after a few hello worlds apps

Yet you can't give me a straight answer to this 'basic' problem. A few lines of code for a simple and straight forward point to be made, there's no reason to paste my whole fucking class in here of redundant code. Just forget it, seems your understanding of Java isn't as great as your ego. Pitiful.

  • Boge 1
Link to comment
Share on other sites

3 hours ago, Kayle7 said:

I cannot move it over to every class I want to use it in, that's inefficient and won't work as I need the String "loggerString" to be used for onPaint(g).

Nothing wrong with passing around the Methodprovider object.

You can also extend the second class from MethodProvider and then exchange bot context, that way you can use log("") in your other class.

public class SecondClass extends MethodProvider {
    public void execute(){
        log("Test");
        FirstClass.status = "Test";
    }

 

public static String status = "";
private SecondClass secondClass;

public void onStart() {
    secondClass = new SecondClass();
    secondClass.exchangeContext(getBot());
}

public int onLoop() {
    secondClass.execute();
}

public void onPaint(Graphics2D g) {
    g.drawString("Status: " + status ,10, 10);
}

 

33 minutes ago, Kayle7 said:

Yet you can't give me a straight answer to this 'basic' problem. A few lines of code for a simple and straight forward point to be made, there's no reason to paste my whole fucking class in here of redundant code. Just forget it, seems your understanding of Java isn't as great as your ego. Pitiful.

Ofc mister know it all who can't write a "hello world" app in java using OOP :???:
Your ego is like the complete opposite from your coding skills at this point, how can you be this cocky while YOU are asking for help in the first place?

Be gratefull anyone wants to help in the first place, but no... let's flame them right away. Good way to make friends :doge:

Edited by Khaleesi
  • Like 3
  • Mald 1
Link to comment
Share on other sites

30 minutes ago, Khaleesi said:

java using OOP :???:
Your ego is like the complete opposite from your coding skills at this point, how can you be this cocky while YOU are asking for help in the first place?

Be gratefull anyone wants to help in the first place, but no... let's flame them right away. Good way to make friends :doge:

I don't want to be friends with anybody here, I don't need help, I realize it was a mistake to engage any conversation here, and you are a fool to assume I have absolutely no skills just because I don't waste my whole fucking life away chatting to nobodies on the internet. Your perception of who I am is worthless and indifferent to me. So long.

Edited by Kayle7
Link to comment
Share on other sites

36 minutes ago, Kayle7 said:

I don't want to be friends with anybody here, I don't need help, I realize it was a mistake to engage any conversation here, and you are a fool to assume I have absolutely no skills just because I don't waste my whole fucking life away chatting to nobodies on the internet. Your perception of who I am is worthless and indifferent to me.

:???:
Critisim, learn to deal with it if you ask to fix your code :)

Edited by Khaleesi
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...