[SOLVED] Getting script to "pause" while waiting for input

I’m almost done with the token spawning functionality of ETA. However, one small problem eludes me. I want the user to be able to spawn the token they have selected from the token spawner button (lower-left corner) by selecting a token, pressing “Spawn Token”, and then clicking a tile. The script is trying to find the pickedEntity from the itemPicker.js script, but because the script just runs beginning to end, there is no time for the user to click a tile.

In the script attached to the token spawn button, on line 53 (there are other lines that have the same problem; I’m just using this line as an example), I want the script to “wait” for the user to click a tile to spawn their token/character on to, before continuing with the rest of the script.

I understand that JavaScript does not “stop” per se – at least not without freezing the computer. I understand that the solution must have something to do with events. I guess what I am looking for is advice for how to create an event of this nature. Should it be part of the tokenSpawner.js file? Is it created similarly to a function? I’ve been studying the tutorial project “Using events with scripts”, but I can’t glean much anything useful from it.

Not 100% sure what you mean here? Are you saying that onSelect is called when you have pressed “Spawn Token”?

No, I’m bypassing onSelect() entirely and jumping straight to pickedEntity. I guess that could be part of my problem. My real problem, though, is that I don’t really understand how to implement events.

TBH, I’m wondering if you need events in this case.

Just trying to understand the problem/current behaviour before suggesting something.

Okay. So, the user (usually the Game Master) wants to add a token (which represents a character or monster) to the scene/battle/whatever. So they click the token spawn button on the bottom-left of the screen. This pulls up a sample list of tokens. The GM selects a token, then clicks “Spawn Token.” This makes the “(De) Spawn Tokens” UI window disappear. At this point, the user is prompted to click a tile. The desired token will then be placed on top of that tile.

The problem is that the script keeps executing past line 53 (in the gold token example; we are ignoring the other tokens for now) before the user can click a tile. And I’m asking how to make it “wait” for the user to select a token, and Google searches told me that the best way to do that is to use events. Which I have next to no idea how to do, despite staring at the events tutorial for about 2.5 hours now.

Side note: The event system in PlayCanvas is one of it’s strongest features and is worth grokking. Maybe I get round to writing that up sometime!

There are several ways that this could be done. The easiest way would be to make the itemPicker responsible for creating the token on the grid and not the button UI.

All the token buttons (and other UI) would do is set the ‘state’ of the itemPicker to be either in spawn token mode, ruler model or any other mode it could be in.

2 Likes

That would be HUGELY welcome. It is one of the things I have on my list of things that I must try to understand better. As it is, I find myself relying on doing things like toggling script variables as a hack to trigger activities.

1 Like