vm

package
v0.0.0-...-6aa2a36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecutionType

type ExecutionType int

ExecutionType tells the VM whether to suspend ot keep running after a callback is executed.

const (
	// PauseExecution tells the VM to suspend
	PauseExecution ExecutionType = iota
	// ContinueExecution tells the VM to keep running
	ContinueExecution
)

type Function

type Function struct {
	// Func is the actual code which handles the events
	Func func(vm *VM, args ...asm.Value) ExecutionType
}

Function is a callback for custom events fired with the Call instruction.

type Option

type Option func(*VM) error

Option is a builder-like function for instantiating a new VM

func HandleEndDialogue

func HandleEndDialogue(handler func(*VM)) Option

HandleEndDialogue assigns a handler for the EndDialogue event. Pass as an option to NewVM.

func HandleEnterNode

func HandleEnterNode(handler func(*VM, string) ExecutionType) Option

HandleEnterNode assigns a handler for the EnterNode event. Pass as an option to NewVM.

func HandleExitNode

func HandleExitNode(handler func(*VM, string) ExecutionType) Option

HandleExitNode assigns a handler for the ExitNode event. Pass as an option to NewVM.

func HandleShowChoice

func HandleShowChoice(handler func(*VM, []string)) Option

HandleShowChoice assigns a handler for the ShowChoice event. Pass as an option to NewVM.

func HandleShowLine

func HandleShowLine(handler func(*VM, string) ExecutionType) Option

HandleShowLine assigns a handler for the ShowLine event. Pass as an option to NewVM.

func RegisterCallback

func RegisterCallback(function Function) Option

RegisterCallback assigns a handler for a custom event which can be fired with the Call instruction.

type VM

type VM struct {
	// contains filtered or unexported fields
}

VM is the virtual machine which runs the instructions generated by the dialogue tree.

func New

func New(program program.Program, options ...Option) (*VM, error)

New instantiates a new VM.

func (*VM) ChooseAndResume

func (vm *VM) ChooseAndResume(selectedChoice int) error

ChooseAndResume will notify the VM that an option was selected and resumes from the decision point according to the option chosen. Which options are available are given by the ShowChoice event, which is fired at the decision point and puts the VM into a waiting state until an option is chosen.

func (*VM) GetVariable

func (vm *VM) GetVariable(name string) (val asm.Value, exists bool)

GetVariable retrieves a named variable saved by the StoreVariable instruction or by the SetVarableT() series of methods. If the variable does not exist, val is Null and exists is false.

func (*VM) Reset

func (vm *VM) Reset()

Reset stops a VM and clears its variables so that the next time it runs, it will be as if running for the first time.

func (*VM) Resume

func (vm *VM) Resume() error

Resume continues the execution of a paused VM from the point it was paused.

func (*VM) Run

func (vm *VM) Run() error

Run executes the program from its start point. Assigned variables are persisted across runs.

func (*VM) SetVariableBoolean

func (vm *VM) SetVariableBoolean(name string, val bool)

SetVariableBoolean stores val as a boolean under the given name. Name strings are converted into symbol values. Saved variables are persisted across runs.

func (*VM) SetVariableNull

func (vm *VM) SetVariableNull(name string)

SetVariableNull stores a null value under the given name. Name strings are converted into symbol values. Saved variables are persisted across runs.

func (*VM) SetVariableNumber

func (vm *VM) SetVariableNumber(name string, val int)

SetVariableNumber stores val as a number under the given name. Name strings are converted into symbol values. Saved variables are persisted across runs.

func (*VM) SetVariableString

func (vm *VM) SetVariableString(name string, val string)

SetVariableString stores val as a string under the given name. Name strings are converted into symbol values. Saved variables are persisted across runs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL