Games with programming mechanics

Started by
2 comments, last by Alberth 4 years, 11 months ago

Hey guys,

I was just wondering how do people allow players to inject code into your game?

I never played it but I think game like screeps is what I am looking for.

You can write a "script" and it will run in your game.

The words "interpreter" comes into mind but no idea how that actually works.

Anyway a briefer would be nice, considering I have no idea where to start this, I don't even know if its achievable for me.

Not even sure what to search, thus I come to you guys.
 

Also, not sure if its this is the correct question but even after figuring out an interpreter -  one issue I am thinking I will run into is the "time used by an action"

If the AI a player writes a billion nested IF loops I want it reflected in the processing time... instead of 1 frame.

 

This is probably harder than what I can do but I would like to know, I like to learn things even if I don't complete it.
 

OH yeah, I have dabbled in C# and Java, javascript, and python, if possible can you make the answer revolve around those? I mean I am not oppose to learning new stuff but it would be nice if I can use what I already partially know just to speed things along. It would be great if I can also use unity for this...

 

Ideally I want it to be online multiplayer with rooms, but that is a whole another can of worms we don't have to go into right now but please make the answer compatible with a solution...

 

Thanks

 

If I was unclear in anything, please let me know, I will try and rephrase it.

 

 

Advertisement

The simplest way to explain it is that an interpreter is a program that takes some code, pretends to run it and calculates what the output would be. Games that allow you to program with real languages (such as Screeps) could include an interpreter in their code.

Since the code still needs to be run, it will take time to do that. The more complicated the code, the more time will take, as usual. I think Screeps limits players' CPU time and allows them to buy more.

Basically, you store a program in data, and write code that does what the data says. As a start, evaluating an expression:


class Number:
    def __init__(self, value):
        self.value = value

    def get_value(self, valuation):
        return self.value

class Variable:
    def __init__(self, variable):
        self.variable = variable

    def get_value(self, valuation):
        return valuation[self.variable]

class BinaryOp:
    def __init__(self, left, op, right):
        self.left = left
        self.op = op
        self.right = right

    def get_value(self, valuation):
        left_val = self.left.get_value(valuation)
        right_val = self.right.get_value(valuation)
        if self.op == '+':
            return left_val + right_val
        if self.op == '-':
            return left_val - right_val
        if self.op == '*':
            return left_val * right_val
        if self.op == '/':
            return left_val // right_val

        if self.op == '>':
            return left_val > right_val
        if self.op == '<':
            return left_val < right_val

# v + 2
var_v = Variable('v')
num_2 = Number(2)
addition = BinaryOp(var_v, '+', num_2)

# Initialize v to 0
valuation = {'v': 0}

# print (v+2)
print(addition.get_value(valuation)) # Prints '2'

The mostly bare minimum is to have an object for a number, a variable, and a binary operator like +, -, * etc.

Then you can build an object tree, and compute the value of the expression

 

A program is much the same thing, except with statements. You have an Assignment, a sequence of statements, and some form of loop, eg a While, and you can compute:


# v < 9
num_9 = Number(9)
compare = BinaryOp(var_v, '<', num_9)

class Assign:
    def __init__(self, variable, expr):
        self.variable = variable
        self.expr = expr

    def run(self, valuation):
        valuation[self.variable] = self.expr.get_value(valuation)

class Sequence:
    def __init__(self, stats):
        self.stats = stats

    def run(self, valuation):
        for s in self.stats:
            s.run(valuation)

class While:
    def __init__(self, cond, body):
        self.cond = cond
        self.body = body

    def run(self, valuation):
        while True:
            if self.cond.get_value(valuation):
                self.body.run(valuation)
                continue

            break

# v = v + 2
incr_v_by_2 = Assign('v', addition)

# v = v + 2
# v = v + 2
body = Sequence([incr_v_by_2, incr_v_by_2])

# while v < 9:
#    v = v + 2
#    v = v + 2


loop = While(compare, body)

valuation = {'v': 0}
loop.run(valuation)
print(valuation)  # prints {'v': 12}

Simple eh?  :)

This topic is closed to new replies.

Advertisement