Discuss Scratch

yinilisk
Scratcher
6 posts

Different broadcasts, same button

Hello! I am trying to make a project wherein there will be different broadcasts for the same button. Is that possible? What I'm trying to make is like a visual novel type thing, and I would like to have a “next” button that only requires the space button to move on to the new text without the same text repeating on screen.
Malicondi
Scratcher
1000+ posts

Different broadcasts, same button

This would probably better for Help with Scripts but to answer your question you can.
A simple way would to name all your broadcasts numbers, and use a variable to broadcast each different broadcast like so:
when gf clicked
set [broadcast v] to (1)

when [space v] key pressed // or whenever you're switching text
broadcast (broadcast)
change [broadcast v] by (1)
this will let you broadcast any amount of broadcasts you need, just make sure you have the code for each one.

Last edited by Malicondi (Feb. 24, 2024 15:45:56)

cosmosaura
Scratch Team
1000+ posts

Different broadcasts, same button

I'll move this over to the Help with Scripts section for you - that's a better fit for this kind of topic
yinilisk
Scratcher
6 posts

Different broadcasts, same button

Thank you! Sorry for posting it on the wrong discussion forum, I'm still pretty new ^^ Will update if it works!
yinilisk
Scratcher
6 posts

Different broadcasts, same button

Malicondi wrote:

This would probably better for Help with Scripts but to answer your question you can.
A simple way would to name all your broadcasts numbers, and use a variable to broadcast each different broadcast like so:
when gf clicked
set [broadcast v] to (1)

when [space v] key pressed // or whenever you're switching text
broadcast (broadcast)
change [broadcast v] by (1)
this will let you broadcast any amount of broadcasts you need, just make sure you have the code for each one.


It doesn't seem to work properly… I am using a made text engine that I found in YT by Coding With Chris which is this code

However when I test this code along with the code you have suggested, the text becomes funky (as seen), and I'm not sure why
(First pic is before the button is pressed, and the second is after it is pressed)


Last edited by yinilisk (Feb. 25, 2024 09:11:09)

deck26
Scratcher
1000+ posts

Different broadcasts, same button

Your attempts to share images don't seem to have worked but it is generally better to share the project.

But do you even need a different broadcast? The receiver can do different things according to the value of variables and you can control things by chanigng those variables. For example, if you had a list where each item contained the text for one page you just need the broadcast receiver to show the text for item ‘current-text’ of the list where current-text is changed each time you move on to the next page.
yinilisk
Scratcher
6 posts

Different broadcasts, same button

I'm pretty new to scratch, so I don't really know how to work the other stuff :') And I can't seem to visualize what you're suggesting unless I see it huhu

How can I share the project for everyone to see if ever?
deck26
Scratcher
1000+ posts

Different broadcasts, same button

yinilisk wrote:

I'm pretty new to scratch, so I don't really know how to work the other stuff :') And I can't seem to visualize what you're suggesting unless I see it huhu

How can I share the project for everyone to see if ever?
Look for the share button in the editor and click it to share the project.

I suspect you're unfamiliar with lists. Follow the link to the Scratch Wiki at the bottom of this page and search for Lists. The Wiki is a useful but often overlooked resource.

I'm pretty sure you don't need different broadcasts in this case.
yinilisk
Scratcher
6 posts

Different broadcasts, same button

https://scratch.mit.edu/projects/971549495

Here's my project, I'm sorry if everything is everywhere, but the code is in the last sprite

I'll check the wiki out when I can!
deck26
Scratcher
1000+ posts

Different broadcasts, same button

As I thought, all you need is to keep track of where you are in the overall text.

Let's say item 1 of the list is ‘It had been a long day’, and item 2 was ‘at the agency’.

For any single page the first line of text is always written at the same position. The second line always has a fixed position and so does the third.

So for the first two items you can just do

define PAIN AND SUFFERING TEXT (text) (size) (color) (x) (y) 
you already have this done

PAIN AND SUFFERING TEXT (item (1 v) of [list v] :: list) [100] [42] [-135] [-89]
PAIN AND SUFFERING TEXT (item (2 v) of [list v] :: list) [100] [42] [-135] [-111.75]
So far so good.

Now all we really need for each page is to know which list items to show for that page. Another list is a good answer. Let's call it ListB. So let's say we have the following information

Page Number Text List items

1 1 2 3
2 4 5
3 6 7 8

and so on. In ListB we store the start and end values for the Text List items in adjacent pairs so now we have

Page Number Text List items ListB item values

1 1 2 3 1 3
2 4 5 4 5
3 6 7 8 6 8

So item 1 of ListB is 1 and item 2 of ListB is 3. Item 3 of ListB is 4 and item 4 is 5.

Perhaps seems confusing but now consider what we have.

Let's say we're on page 3 - each time we move on to the next page we just increment the page number.

So there are two items in ListB for each page so we look at 2*pagenum - 1 (5) for the minimum text item number and 2*pagenum (6) for the maximum text item number. As we can see above ListB item 5 is 6 and item 6 is 8. So to display page 3 we want items 6,7 and 8 from the text list.

The first bit of code I showed you tells you how to display 2 lines of text by referring to the text list but we can generalise the code - only the y position really changes and it changes by approx -22. So we now have

define PAIN AND SUFFERING TEXT (text) (size) (color) (x) (y) 
you already have this done

when [space v] key pressed
set [tempy v] to [-89]
set [textline v] to (item (([2] * (pagenum)) - [1]) of [listB v] :: list)
set [lastline v] to (item ([2] * (pagenum)) of [listB v] :: list)
repeat until <(textline) > (lastline)>
PAIN AND SUFFERING TEXT (item (textline) of [list v] :: list) [100] [42] [-135] [tempy]
change [textline v] by [1]
change [tempy v] by [-22]
end

So now you have a generalised script that will print a variable number of text lines to match the current page number.
ducdat0507
Scratcher
5 posts

Different broadcasts, same button

You should put the dialogue into different numbered broadcast receiver blocks, like this:
when I receive [0 v]
... // dialogue goes here
when I receive [1 v]
... // dialogue goes here
when I receive [2 v]
... // dialogue goes here
and so on.
deck26
Scratcher
1000+ posts

Different broadcasts, same button

ducdat0507 wrote:

You should put the dialogue into different numbered broadcast receiver blocks, like this:
when I receive [0 v]
... // dialogue goes here
when I receive [1 v]
... // dialogue goes here
when I receive [2 v]
... // dialogue goes here
and so on.
A perfectly valid approach but when you have what is essentially the same thing happening over and over it is good to get in the habit of considering how to generalise. So my method would work just as well for 100 pages as for 2 or 3. It also means if you want to move the text slightly on every page you're only doing it once rather than having to adjust lots of separate scripts with a good chance of making an error at some point.
pythonicKI
Scratcher
100+ posts

Different broadcasts, same button

deck26 wrote:

(#12)

ducdat0507 wrote:

You should put the dialogue into different numbered broadcast receiver blocks, like this:
when I receive [0 v]
... // dialogue goes here
when I receive [1 v]
... // dialogue goes here
when I receive [2 v]
... // dialogue goes here
and so on.
A perfectly valid approach but when you have what is essentially the same thing happening over and over it is good to get in the habit of considering how to generalise. So my method would work just as well for 100 pages as for 2 or 3. It also means if you want to move the text slightly on every page you're only doing it once rather than having to adjust lots of separate scripts with a good chance of making an error at some point.
then we should see the project to know if it's like what you said or something completely different (like one broadcast makes the sprite brighter, the second makes him jump etc.)
deck26
Scratcher
1000+ posts

Different broadcasts, same button

pythonicKI wrote:

deck26 wrote:

(#12)

ducdat0507 wrote:

You should put the dialogue into different numbered broadcast receiver blocks, like this:
when I receive [0 v]
... // dialogue goes here
when I receive [1 v]
... // dialogue goes here
when I receive [2 v]
... // dialogue goes here
and so on.
A perfectly valid approach but when you have what is essentially the same thing happening over and over it is good to get in the habit of considering how to generalise. So my method would work just as well for 100 pages as for 2 or 3. It also means if you want to move the text slightly on every page you're only doing it once rather than having to adjust lots of separate scripts with a good chance of making an error at some point.
then we should see the project to know if it's like what you said or something completely different (like one broadcast makes the sprite brighter, the second makes him jump etc.)
We have seen the project and insofar as it has been coded the script displays text for the current page with, as I stated, line 1 starting in one position, line 2 in the next position roughly 22 pixels lower and so on.
ducdat0507
Scratcher
5 posts

Different broadcasts, same button

deck26 wrote:

ducdat0507 wrote:

You should put the dialogue into different numbered broadcast receiver blocks, like this:
when I receive [0 v]
... // dialogue goes here
when I receive [1 v]
... // dialogue goes here
when I receive [2 v]
... // dialogue goes here
and so on.
A perfectly valid approach but when you have what is essentially the same thing happening over and over it is good to get in the habit of considering how to generalise. So my method would work just as well for 100 pages as for 2 or 3. It also means if you want to move the text slightly on every page you're only doing it once rather than having to adjust lots of separate scripts with a good chance of making an error at some point.
Oh, I saw that the project already had Malicondi's code so I just answer accordingly.
The generalization can also be done by creating a custom block to handle the dialogue, like this:
define make character say [dialogue]
... // code to handle dialogue goes here
when I receive [0 v]
make character say [I am a dialogue] :: custom
Also I think that this would make adding features easier (I guessed that the person asking the question is trying to make a visual novel type of game, and as far as I know visual novels usually do much more than just displaying text), but I guess that boils down to each other's preferences.

Last edited by ducdat0507 (Feb. 25, 2024 14:35:31)

yinilisk
Scratcher
6 posts

Different broadcasts, same button

Hello! I can't seem to understand much of what is going on, huhu If it's possible to do a step by step of the sprites or a straight on visual tutorial, it would help so much :') Thank you all for the assistance, but my brain just can't seem to understand huhu

Powered by DjangoBB