[GUIDE] Random NPC dialogue

Heya, in this guide i will not be covering how to spawn npc/entity. This guide will help you create(send) random NPC dialogue, let's get started.

Raw code-

Player event( Chat msg.) -> If player name equals( Your ign) -> If player msg equals( @Generate-Arrays) -> Create Array( NPC-Dialogue, add all the dialogue you want to add. Make sure its a text-variable(Textbook))

Player event( Right click entity) -> If entity name equals( your npc name) -> Calls function( SelectRandom)

Function( SelectRandom) -> Get array length( NPC-Dialogue, var = arrLength) -> Select random no.( no.(0) to var(ArrLength), Save the value in var = ranIndex) -> Get element( NPC-Dialogue, index = var(ranIndex), output var = Dialogue) -> Send msg.( Dialogue)

Formatted code-

Python:
# Player event: Chat message
Player event(Chat msg.) ->
    If player name equals(Your ign) ->
        If player msg equals(@Generate-Arrays) ->
            Create Array(NPC-Dialogue, add all the dialogue you want to add. Make sure it's a text-variable (Textbook))

# Player event: Right-click entity
Player event(Right-click entity) ->
    If entity name equals(your npc name) ->
        Call function(SelectRandom)

# Function: SelectRandom
Function(SelectRandom) ->
    Get array length(NPC-Dialogue, var = arrLength) ->
    Select random no.(no.(0) to var(arrLength), Save the value in var = ranIndex) ->
    Get element(NPC-Dialogue, index = var(ranIndex), output var = Dialogue) ->
    Send msg.(Dialogue)



Note: There is another efficient way to do this. It involves arrays name based on entity names, this very method can be very helpful as it does not invlove using if-else again & again.( This is something that i haven't tried doing ingame before and this is the reason i'm not discussing this method in this guide)

Thanks for reading! Hope this helps you.
Please consider liking the thread <3
 
Top