PIXI Graphics Question / Custom Hookshot Event

Hello all!
I come today with a question about PIXI Graphics. I am currently trying to get the engine to draw a PIXI Graphics line from the player to an event. I'd like it to constantly redraw itself so that the line "updates" as the player moves. I realize that this has been answered before in the past (such as here). However, when I go to implement Dev_with_Coffee's solution, the line draws itself in the corner of the game screen.

Spoiler: Line in Corner
Untitled2.png

Here is the event that's running it:

Spoiler: Event
Untitled1.png



My player coordinate variables are updated from a parallel event elsewhere (Variables 11 and 12).

I'm obviously missing something, but I'm not sure what. I feel like the
JavaScript:
myLine.position.set(0, 0)
is just denoting a pixel measurement because when I play with the numbers, that little line moves.

If anyone has any ideas, I'd be very grateful! My head hurts from bashing it against the wall haha.

Thank you for your time!
This is outside of my experience, but those two function calls don't look right. They look like they should be methods of the PIXI object you made.
e.g. myLine.moveTo and myLine.lineTo
That's a good idea. I'll have to try that when I get home.
Many PIXI.Graphics methods, including lineStyle, return the object for chaining purposes:

A couple of concerning points about your script call setup:
  • It re-adds the display object to the scene every redraw...it only needs to be added once!

  • It defines myLine as a per-session global variable, which will cause bugs in many save/load situations. You could work around this by merging the two script calls into one: create the display object if it doesn't exist, redraw it, wait X frames, repeat. E.g. (untested):
    JavaScript:
    var sc = SceneManager._scene;  // shorthand
    if (!("_myLine" in sc))
      sc.addChild(sc._myLine = new PIXI.Graphics());
    sc._myLine.clear()
              .lineStyle(10, 0xffffff)
              .moveTo($gameVariables.value(11), $gameVariables.value(12))
              .lineTo($gameVariables.value(16), $gameVariables.value(17));
    As you can see, I've declared it as a property of the scene here, rather than a global variable.

I feel like the
JavaScript:
myLine.position.set(0, 0)
is just denoting a pixel measurement
Yes. I believe it will offset the draw coordinates for the graphics object within its parent (the scene). So if you leave it at (0, 0), then all draw coordinates would be scene coordinates.

From your description, I'd suggest using this for variables 11 & 12:
  • Control Variables -> Set -> Game Data -> Character -> Player -> Screen X & Y
These variables should be updated somewhere...in your screenshot they are referenced but never set. Remember to use screen coordinates for the other variables, too.

I would recommend implementing this as a plugin, mostly because that would avoid the whole event interpreter system, including eval, but also because it would help to ensure the sprite is created when it is needed, e.g. on scene creation, rather than having to check if it exists every time. I know you've posted this for MV, but in case it helps, CT_Bolt wrote an MZ plugin here:
Many PIXI.Graphics methods, including lineStyle, return the object for chaining purposes:
And that's something new for me, syntactically...I didn't realize JavaScript could tolerate spaces and line breaks between an object and the dot for its property.

I assumed those had to be getting treated as separate code statements.
JavaScript:
var sc = SceneManager._scene; // shorthand if (!("_myLine" in sc)) sc.addChild(sc._myLine = new PIXI.Graphics()); sc._myLine.clear() .lineStyle(10, 0xffffff) .moveTo($gameVariables.value(11), $gameVariables.value(12))

Thanks for that cleaned up code, I'll try that when I get home and see how it goes! So if it gets added to the scene instead of the global session, it'll clear when the scene changes? And then that won't screw things up in the long run?

Also, do I just need to define the sc once and then loop the rest to draw, clear, then redraw? As a point of curiosity, why not have the sc._myLine.clear() at the end of the script, after the line attributes are defined? I wondered that about the other one too.

From your description, I'd suggest using this for variables 11 & 12:
  • Control Variables -> Set -> Game Data -> Character -> Player -> Screen X & Y
I did add in that spoiler that my player variables were set in a seperate parallel event, but I now see that that's easy to miss. Sorry!

RMMZ - Draw line from event to event?​

Hello, I'm very not into programming, so maybe someone could help me... can we draw a straight white line from one event to another, to connect them? It should work real-time, like when the events are moving, so we can make something like fishing line or a leash for animal? Thank you.
forums.rpgmakerweb.com
forums.rpgmakerweb.com
I would actually love to use this plugin as it would solve my problem entirely, and I've tried it, but I haven't been able to get it to work in MV. That was a sad realization haha.
Wow, something really mangled the formatting of the quotes in your post. 👋

So if it gets added to the scene instead of the global session, it'll clear when the scene changes?
Roughly, yes.
  • "Global" is about where a value can be accessed: a global variable is accessible from any code in your game.
  • "Session" is about when a value can be accessed: 1 play session may include progress in multiple playthroughs.
So defining _myLine as a scene property like that makes it accessible so long as the scene is. Defining it in the update call avoids making assumptions about whether it has already been defined.

Also, do I just need to define the sc once and then loop the rest to draw, clear, then redraw?
I gave a script call that I think should work inside your loop. As the comment on that line indicates, the var sc line is only there as a shorthand. The var declarator means it's a local variable: it will only be available for the duration of the script call.

It is technically safer than using SceneManager._scene each time, since it is possible for some crazy code to change the scene mid-call. More to the point, I think it makes the code easier to read and maintain.

As a point of curiosity, why not have the sc._myLine.clear() at the end of the script, after the line attributes are defined?
Because clear "clears the graphics that were drawn to this Graphics object, and resets fill and line style settings". 😅

I did add in that spoiler that my player variables were set in a seperate parallel event, but I now see that that's easy to miss.
You're correct, I didn't see that. However, this is the more important part:
Remember to use screen coordinates
As seen in your screenshot, variables 16 & 17 are using map coordinates (tiles), not screen coordinates (px). I imagine that's why your line is all the way up in the corner of the screen. 👋
Wow, something really mangled the formatting of the quotes in your post. 👋


Roughly, yes.
  • "Global" is about where a value can be accessed: a global variable is accessible from any code in your game.
  • "Session" is about when a value can be accessed: 1 play session may include progress in multiple playthroughs.
So defining _myLine as a scene property like that makes it accessible so long as the scene is. Defining it in the update call avoids making assumptions about whether it has already been defined.


I gave a script call that I think should work inside your loop. As the comment on that line indicates, the var sc line is only there as a shorthand. The var declarator means it's a local variable: it will only be available for the duration of the script call.

It is technically safer than using SceneManager._scene each time, since it is possible for some crazy code to change the scene mid-call. More to the point, I think it makes the code easier to read and maintain.


Because clear "clears the graphics that were drawn to this Graphics object, and resets fill and line style settings". 😅


You're correct, I didn't see that. However, this is the more important part:

As seen in your screenshot, variables 16 & 17 are using map coordinates (tiles), not screen coordinates (px). I imagine that's why your line is all the way up in the corner of the screen. 👋

Yes, the formatting for my quotes didn't work as well as I had hoped 😅

But, your code worked perfectly after I created new variables to track the screen (X,Y) of the player and event! Thank you so much! 🙂 I had completely forgotten that was a thing I could do.
Spoiler: Line
1703343753583.png

One last question: I was testing this in hopes to add this to a mechanic I've built. It's the hookshot from Legend of Zelda if you're familiar. When the player activates the hookshot, the line draws between the player and the event as the player is "dragged" to that event. The line updates well during player transit, but I can't seem to get the line to clear when the player is done moving.

Here's a picture of part of the hookshot event where I've added your script and where I've added what I thought would be the correct place to add in a script clearing the line. I've also tried it at the end of the event since it's a pretty long event, but I can't get it to clear completely once the player is done moving. I fear this is something simple I've overlooked or that it's simply due to my lack of understanding. I'm more of an art and music guy to be honest 😅
Spoiler: Hookshoot Event Snippet
1703344377954.png
I can't seem to get the line to clear when the player is done moving.
Ah, you've used this script call:
JavaScript:
sc._myLine.clear()
However, sc is not defined in that script call. Try this instead:
JavaScript:
SceneManager._scene._myLine.clear();
Ah, you've used this script call:
JavaScript:
sc._myLine.clear()
However, sc is not defined in that script call. Try this instead:
JavaScript:
SceneManager._scene._myLine.clear();
I see, I didn't really understand how that worked, but I think I have a better idea how of those are classified now.

I tried your new script and it works perfectly! Thank you so much!

Here's a little video of my hookshot function that now works completely as intended! Thank you, I really appreciate it!
Spoiler: Hookshot
Here's a little video of my hookshot function that now works completely as intended!

I was very curious to see but the link you shared can only be accessed by explicitly invited people.
My bad! This link should be availabe to anyone.
Spoiler: Hookshot video (open to viewers)
Nice! You're welcome~ 😂
I can see it, but the plugin by CTB does work in MV, only small testing here, didn't test it fully with
color, id and other data yet, but it might need some small modifications to make it compatible with
MV fully if it doesn't work fully.

but nice hookshot though 🙂
Thank you all!

the plugin by CTB does work in MV
Really? Dang! I couldn't get it to work. I was kind a busy the day I was testing it, maybe I should have been more patient. Oh well, live and learn!
Amazing! Nice job!
Really? Dang! I couldn't get it to work. I was kind a busy the day I was testing it, maybe I should have been more patient. Oh well, live and learn!
I try to see where it doesn't work as I use 1.6.1, lower version might not work though.
but if something doesn't work, I could try to make it work for MV users so they can use it too, but that has to wait
till next year when times allows it.

if your event works, you can keep it, but so far I checked the code, there shouldn't be much problems as MZ and MV
uses both javascript, and most functions exists in both.

try the basic functions that should work, moveTo part like your video is something that isn't added, line to line should.
so using it as hookshot might not work with that plugin yet, and if that is the case, it require some coding to make
that work.

I also make a hookshot plugin for my own which is only working half atm, but not sure if I share that plugin.
if you really need a hookshot, I might share it with you for use, as its more personal use though.
I try to see where it doesn't work as I use 1.6.1, lower version might not work though.
but if something doesn't work, I could try to make it work for MV users so they can use it too, but that has to wait
till next hear when times allows it.

if your event works, you can keep it, but so far I checked the code, there shouldn't be much problems as MZ and MV
uses both javascript, and most functions exists in both.

try the basic functions that should work, moveTo part like your video is something that isn't added, line to line should.
so using it as hookshot might not work with that plugin yet, and if that is the case, it require some coding to make
that work.

I also make a hookshot plugin for my own which is only working half atm, but not sure if I share that plugin.
if you really need a hookshot, I might share it with you for use, as its more personal use though.
I'll have to check the version I'm using when I get a chance. Though if that plugin truly is working for MV, then I'm sure you'd make a lot of people happy haha.

As to my event, it was a lot of work to set up, and though I'm sure it wasn't the hardest thing in the world, I'm fairly proud of it considering my experience level. Couldn't have done it without help from the community haha 🙂 With the framework set up, it's just a good amount of copy/paste for each new hookshot-able event. I might post it here in case any one is interested in see what I did.

As for your plugin, I'd be interested in taking a look at it! But I wouldn't be much help in any troubleshooting haha. But with the MV hookshot plugins seemingly lost to time and broken links, I'm sure people would get a lot of use out of it if you were willing to post it!
Really cool @wiffle88 , I tried looking for something like this in the past and I must have weak google-fu. I might have to "borrow" this for my fishing mechanic. Hehe.
I believe there were some people that might have been interested in my hookshot event in its entirety. And, lacking any current publically available hookshot plugin, I think this might be of some use to others out there searching for such a thing. Let's hope Google catches this. If absolutely necessary, I can make a separate post.

So, here is my take on a custom hookshot event!

Dependencies:
  • Yanfly's Event Spawner Plugin
  • Yanfly's Button Common Events Plugin (player pushes D in my event structure to call this common event)
  • Shaz's Smart Path Plugin
  • Editing the rpg_objects.js file to make locking player movement possible (information found here). Make a copy of the base rep_objects.js before you do so, in case you screw up on accident. MV doesn't allow you to do this natively, MZ does.

What the event does:
  1. Locks player movment.
  2. "Pushes" the button for my UI.
  3. Spawns in an event I use as a target (an animated crosshair in my case).
  4. Allows the target to be moved with a player input loop.
  5. Upon an "OK" button press, looks at the map the player is on, then checks to see if the crosshair event (x,y) lines up with a hookshotable event's (x,y). Also unlock player movement.
  6. If so, it sets the player movement speed to 6, turns Through on, and turns the walking animation off.
  7. Checks what direction the player is facing, then 'pulls' the player with Smart Path to a set of specified (x,y) coordinates.
  8. Draws the PIXI Graphic line between the player and the event the player is pulled to.
  9. When the player is in the correct spot, cancels the Smart Path, and clears the PIXI Graphics line.
  10. Sets player speed back to normal, Through on, and turns the walking animation back on.
  11. Despwans the targeting crosshair event.
Adding hookshot-able events just involves some copy and paste.

A few obvious problems:
  1. Player can hookshot through things like houses if that's the shortest path to the event.
    1. I realized this, but I don't intend to have a lot of hookshot-able events in towns. I needed the player to be able to move through terrain since the whole point is to be dragged to a space that was previously inaccessable. This was the easiest option. I'm going to design maps in a way where this will be hard to get dragged through something that looks strange.
  2. The Hookshot Event is very large.
    1. Yes, hahaha. It's a little unwieldy. But I didn't want to create a new one for every map, hence the comments to make it easier to navigate. If it becomes a problem, I'll edit this post down the road.
  3. I need to add a sound effect for it.


Spoiler: Hookshot Event Screenshot
Event.png

Original thread: https://forums.rpgmakerweb.com/threads/pixi-graphics-question-custom-hookshot-event.164058/