PDA

View Full Version : public function help


maximus_asinus
08-17-2017, 08:12 AM
In one class named "block" I have this script:

public function OrbSwitch() {
// actions
}


and in another class named "switch" I have this script:

function onActionAHPHit() { // custom sword triggeraction
// actions
block.OrbSwitch();
}


Everything under onActionAHPHit() works but the public function isn't being triggered. Am I using the function wrong?

Crow
08-17-2017, 08:28 AM
Pretty much. If you want to use a function that's defined in a class, you'll have to join() that class first. The function also doesn't have to be "public". Public functions can be useful in weapons, though. Let's look at an excerpt of some Atlantis weapons.

-Weather:

function onCreated() {
weather = this; // I love doing this stuff
}

public function GetWeather() {
return this.sWeather;
}


-DayNight:

function UpdateSymbols() {
// lots of code here
temp.we = weather.GetWeather();
// a lot more code using the stuff we just got from -Weather
}

maximus_asinus
08-17-2017, 09:26 AM
I figured that was the problem. Thanks!

Unrelated but can you set a gani using a variable as a wildcard? Lets say I have a gani named "walk_true" and another called "walk_false".

function onCreated() {
this.foobar = !this.foobar;
this.ani = "walk_"@this.foobar;
}

I've tried this and I had no luck.

Crow
08-17-2017, 10:59 AM
Well, first off, you should be using setAni() instead. And then, this.foobar is also a boolean in this case. You should evaluate that to something yourself to avoid issues. (this.foobar ? '1' : '0'), for example.

maximus_asinus
08-17-2017, 05:51 PM
What is the difference between using this.ani and setAni()?

Also coming back to the original post, is it possible for two classes to communicate?

My example:

In the first class I have a switch that the player interacts with by swinging the sword. This class is supposed to tell the second class that it was hit. In the second class I have blocks that raise or lower depending on the status of the switch.

I'd rather keep them as separate classes to keep it neat, and I don't want to use triggeraction because I might have a room full of blocks and defining coordinates would be a pain. There must be a clean way to just do something like "triggerclass" or something.

maximus_asinus
08-18-2017, 03:49 AM
I've exhausted my theories on how to get this to work outside of using a loop to check for a variable change (but that seems very inefficient and not at all what I want to do). Come one guys, someone must know if what I'm suggesting is possible. I've tried to search this on the forums but there is literally tens of thousands of results for my search terms, and about 99.999% of those results have nothing to do with what I am trying to do.

And if I wasn't clear my goal is for when I hit a switch belonging to Class A I want all the items in Class B to update.

maximus_asinus
08-18-2017, 04:41 AM
I went back and tried public function again after reading a post by fp4 and got something that ALMOST works.

CLASS A
//#CLIENTSIDE
function onActionAHPhit() { // custom sword hit detection
temp.foobar = this.level.foobar;
temp.foobar.baz();
}

CLASS B
//#CLIENTSIDE
function onCreated() {
this.setimg("block.png");
this.level.foobar = this;
}
public function baz() {
this.chat = "debug";
}

The problem is only the last NPC to join Class B will receive the update. If I could get around that I would be all set.

Crow
08-18-2017, 10:35 AM
Well, you already have the right idea with making object references available through the level object. Instead of just setting one (which will inevitably overwrite things if you have more than one block or whatever), maybe try storing them all in an array? I haven't tried this before. Should work, though.

maximus_asinus
08-18-2017, 06:03 PM
I am not sure how I would do that. I tried something like this.level.foobar.add(this); and this.level.foobar[0] = this; expecting this.level.foobar[0] to return the object but apparently it doesn't work like that. I can only guess you can't store objects in arrays. This is incredibly frustrating. I wish GS2 was better documented.

PS thanks for the input thus far Crow.

BigNews
08-18-2017, 08:00 PM
You'll probably want to do something like this:

Class A:


//#CLIENTSIDE
function onCreated()
if (this.level.listeners.index(this.id) == -1)
this.level.listeners.add(this.id);

public function baz()
this.chat = "debug";



Class B:


//#CLIENTSIDE
function onActionAHPhit() {
for (temp.id : this.level.listeners) {
findnpcbyid(temp.id).baz();
}
}


Because when the level is updated, it still retains any variables stored on the level object beforehand, which can cause array values to contain non-existent objects.

--------

Alternatively if you want to store the objects and be safe between level updates you could do something like:

Class A:


//#CLIENTSIDE
function onCreated() {

temp.coord = this.x @ "_" @ this.y;
temp.i = this.level.listenerCoords.index(temp.coord);

if (temp.i == -1) {
this.level.listenerCoords.add(temp.coord);
this.level.listeners.add(this);
}
else
this.level.listeners[temp.i] = this;

}

Class B:

//#CLIENTSIDE
function onActionAHPhit() {
for (temp.listener : this.level.listeners) {
temp.listener.baz();
}
}

(which assumes NPC coordinates are always unique)

maximus_asinus
08-18-2017, 09:31 PM
And you just solved in 5 minutes what would have taken me 5 days.

maximus_asinus
08-23-2017, 02:07 AM
I'm pretty desperate for help on this. I'm in over my head.

I'm trying to sync the blocks with everyone in the level. My first guess was to use triggerServer or triggerAction but I can get neither of those to work inside of a class (I've since read that triggerServer doesn't work in local or class NPCs without a hacked workaround).

I thought something like this would work, but no dice on this as well. I just can't grasp what I'm doing wrong.


function onActionFoo () {
// also tried onActionServerSide() when using 0,0 as triggerAction coordinates.
echo("bar");
}

//#CLIENTSIDE
function onCreated() {
setshape(1,32,32);
}

function onPlayerChats() {
if (player.chat == "foo") {
echo("foo");
triggerAction(x,y, "Foo", NULL);
}
}


I'm circling back around to having to use a timeout in the blocks, but I feel like that is a hack option and could cause lag if I had a level with dozens of blocks inside all with a nested loop.

Crow
08-23-2017, 06:50 AM
Try using setshape() on the serverside as well.

maximus_asinus
08-24-2017, 04:32 AM
That makes perfect sense and worked perfectly. I've successfully synced with players in the level.

(yet another post)

What are the limitations of using level variables on a GMAP? I remember reading that the player only loads a 3x3 chunk of the GMAP. Providing that this is true, does the level variable continue to sync past this point?

If not then I need a solution for saving my switch variable. My guess is that I would have to use a DB NPC (another thing I have no experience with. Yay). If I save each quest set with a unique prefix is there a simple way to catch the level prefix so I can try to make each set of switches unique?

I'm thinking of designing it something like:


// database variables
// true means hit, false means not hit
this.quest = dungeon1_, dungeon2_, dungeon3_
this.switch = true, false, false



// get level prefix
with(findNPC(Database)) {
// check for position of prefix in this.quest
// save position of prefix and then check corresponding location in this.switch for switch status.
// change the found value in this.switch
}


Any help on using a database in this context or finding the level prefix would be appreciated.