Jump to content

Modding CounterManager.js, using SendNetworkChat to send.


Hoze
 Share

Recommended Posts

Hello!

I tried to make a simple addition to the interface where by clicking a resource on the top panel, it will send a message to notify allies how much of this particular resource you have. The idea is that in multiplayer games, you like to ask for resource you need and offer the ones you have a lot of. Sounded simple and in my reach :thumbsup:.
Little did I know how little I know...

I first tried the simplest thing, I added an attribut to the object tag in \gui\session\top_panel\Counters.xml

 onPress="Engine.SendNetworkChat(resource[n])"
 

But just adding the "onPress" attribut here seems to break everything; the top panel, and other unrelated things like music.

Next I looked into CounterManager.js to add to the existing onPress method of the class, but the class doesn't provide a way to identify the exact ressource I'm clicking on.

Last CounterResource.js seemed more promising as "rescode" that provide the names in a string ("food"..) is defined in the constructor. But here, I can't make an onPress method that would work just by naming it.

I was just trying to do just this :

onPress()
    {
        Engine.SendNetworkChat("I have " + this.count + " of " this.resCode + "!");
    }

Any ideas of what am I missing, please?

Link to comment
Share on other sites

I think I have better understanding now (after ~15hours of debugging :whistling:).

In CounterManager.js, the onPress method must be defined to handle onPress events. The event seems to be triggered even prior to any GUI interactions after the game loads because it prints uninformative errors all over when onPress is not defined.

The onPress mehtod from CounterManager.js overwrites the one I would define in CounterResources.js

I try to make the resource icon / counter a button that would make a simple chat message:

Engine.SendNetworkChat("I have " + this.count + " of " + this.resCode + "!");

I tried to bind variables resCode and count in various ways.

I tried to make a renamed method in CounterResource like:

class CounterResource {
    constructor(resCode, panel, icon, count, stats) {
		//...
        this.onResourcePress = this.onResourcePress.bind(this);
    }
    onResourcePress() {
        Engine.SendNetworkChat("I have " + this.count + " of " + this.resCode + "!");
    }
}

For it to be callable in the CounterManager:

class CounterManager {
    constructor(playerViewControl) {
        //...
        this.counters = [];
        //...
        for (let resCode of g_ResourceData.GetCodes()) {
            let counter = this.addCounter(resCode, CounterResource);
            counter.onPress = counter.onResourcePress.bind(counter);
        }
        //...
    }

    //...

    onPress(counter) {
        counter.onResourcePress();
    }
}

I don't know how much syntaxes I've tried... I feel like sorry I can't make this seemingly simple thing to work. If you have any clues of what I'm missing, please tell me!

  • Like 1
Link to comment
Share on other sites

On 21/01/2023 at 8:06 AM, Hoze said:

I think I have better understanding now (after ~15hours of debugging :whistling:).

In CounterManager.js, the onPress method must be defined to handle onPress events. The event seems to be triggered even prior to any GUI interactions after the game loads because it prints uninformative errors all over when onPress is not defined.

The onPress mehtod from CounterManager.js overwrites the one I would define in CounterResources.js

I try to make the resource icon / counter a button that would make a simple chat message:

Engine.SendNetworkChat("I have " + this.count + " of " + this.resCode + "!");

I tried to bind variables resCode and count in various ways.

I tried to make a renamed method in CounterResource like:

class CounterResource {
    constructor(resCode, panel, icon, count, stats) {
		//...
        this.onResourcePress = this.onResourcePress.bind(this);
    }
    onResourcePress() {
        Engine.SendNetworkChat("I have " + this.count + " of " + this.resCode + "!");
    }
}

For it to be callable in the CounterManager:

class CounterManager {
    constructor(playerViewControl) {
        //...
        this.counters = [];
        //...
        for (let resCode of g_ResourceData.GetCodes()) {
            let counter = this.addCounter(resCode, CounterResource);
            counter.onPress = counter.onResourcePress.bind(counter);
        }
        //...
    }

    //...

    onPress(counter) {
        counter.onResourcePress();
    }
}

I don't know how much syntaxes I've tried... I feel like sorry I can't make this seemingly simple thing to work. If you have any clues of what I'm missing, please tell me!

will be lovely if you can share the errors you receive @Hoze

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.

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.

 Share

×
×
  • Create New...