Letting a player program.

BlitzMax Forums/BlitzMax Beginners Area/Letting a player program.

Ryan Burnside(Posted 2007) [#1]
Recently I want to allow the player to program behaviors into objects. My question is how to allow the player to use math functions and the object's variables.

Lets say the object has an speed value. I currently have a dialog box that pops up and asks for a number to describe the speed. How would I allow the player to set the speed using mathmatical operators while addressing other field values within the object?

Maybe te player would like to set the speed based on another field variable coupled with some operators. Can this be done? Can they type in "cos(direction)*13" and operations such as this?


ImaginaryHuman(Posted 2007) [#2]
Sure, you just would have to `parse` what they type in and set up your own `mini script language` of keywords that are allowed, and then you'd search through the string and extract whatever bits interest you and process them. Ideally you'd probably turn the user input into a series of function calls or something so you don't have to parse it every time you want to execute it.


grable(Posted 2007) [#3]
There are a few expression interpreters in the code archives. and as ImaginaryHuman suggest, generating an intermediate representation (like a node tree feks) would speed things up a little. or go a step further and create your own virtual machine.


Ryan Burnside(Posted 2007) [#4]
Ah ok, I thought so. I suppose there is no way to directly evaluate a string of commands then? Without using word parsing?
There might be around 500 objects preforming actions at once. I think parsing may be too taxing for that...


grable(Posted 2007) [#5]
I suppose there is no way to directly evaluate a string of commands then? Without using word parsing?

Im afraid not. and if there were, that method would still have to parse the string ;)

I would suggest parsing/compiling down to a simple stack machine with a single number type, and implementing functions as opcodes.


Ryan Burnside(Posted 2007) [#6]
I'm really new to this concept. What do you mean my compiling down and single number type and opcodes?


grable(Posted 2007) [#7]
I'm really new to this concept.

My mistake, i forgot this was in "Beginners Area".. hehe

If you havent done any parsing before i suggest reading up on it before jumping in.
http://en.wikipedia.org/wiki/Parsing
http://en.wikipedia.org/wiki/Recursive_descent_parser <-- easiest to implement in my oppinion

What it boils down to is that an expression like this:
x = a + b * 2

Is translated into either a node structure:
(ASSIGN
  (VARIABLE x)
  (ADD
    (VARIABLE a)
    (MUL
       (VARIABLE b)
       (NUMBER 2)
    )
  )
)

A node can defined like this.
Type TNode
  Field Left:TNode
  Field Right:TNode
  Field Value:String
EndType


Or directly to assembler (doesnt have to be real machine asm though, could be an imaginary machine like below)
GET a
GET b
PUSH 2
MUL
ADD
SET x

Compilers usually compile to a node tree, then work (doing optimizations etc) on that tree and then spit out some assembler code.
You dont have to use a virtual machine though, you could jsut as well execute the node structure, but it would be a little slower.

What i meant by single number type was just for simplicity of the virtual machine, eg no strings and other fancy structures like proper function calls.

an OpCode is roughly the same as an Instruction, a single operation a CPU or a virtual machine executes.

I adapted an expression interpreter i made a while back to spit out code for a very simple virtual machine, so you can see how it all fits together:



fredborg(Posted 2007) [#8]
I recommend you take a look at BriskVM, which is a scripting language you can easily use in BlitzMax.

Not only is it very easy to implement for you, it's also very easy to script in, because the syntax is 99.9% the same as BlitzMax.

You can find it here: http://www.koriolis-fx.com/

Highly recommended!


Ryan Burnside(Posted 2007) [#9]
Thanks guys I see I'll have a busy evening. If I have trouble I will post again.