Changing strings to usable functions

GodswarthGodswarth Posts: 8Member
edited August 1 in Programming

I'm trying to make an array that has every movement option in it. It's a long list and I want to call parts of it out and at some points check the whole array of values to make sure it doesn't go out of bounds. I'm trying to simulate extra dimensions so I need lots of movement value in some sort of organized table.

I don't understand why I can't call out the operation of a move function this way or how to make a useful database that has every movement value I want in it. I'm using the GD-script language. Any help would be much appreciated.

var white_pawn_moves = ["global_translate(2,2,0)", "global_translate(2,-2,0)", "global_translate(12,2,0)", ...]
white_pawn_move[0]

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,863Admin

    Welcome to the forums @Godswarth!

    I have not used it myself, but I think you may be able to achieve something like what you are looking for using the Expression class in Godot.

  • GodswarthGodswarth Posts: 8Member
    edited August 1

    Expression would be useful if I could understand whats going on with them. I don't see to much written about them or there use.

    I found a way to make it work in an array amending vectors one by one maybe there is a better way.

    func _on_Area_input_event(camera, event, click_position, click_normal, shape_idx):
        if event is InputEventMouseButton:
            if event.button_index == BUTTON_LEFT and event.pressed == true:
                print(translation) 
                global_translate(position_list[1])
    
    var position_list = Array()
    
    func _physics_process(delta):
        position_list.append(Vector3(2,2,0))
        position_list.append(Vector3(2,-2,0))
        position_list.append(Vector3(12,2,0))
    
  • TwistedTwiglegTwistedTwigleg Posts: 2,863Admin

    @Godswarth said:
    Expression would be useful if I could understand whats going on with them. I don't see to much written about them or there use.

    Yeah, I only learned about the Expression class just today when I was Googling to see if I could find an answer with how to execute GDScript from a string. It seems interesting, but as you said, there's not much written about them or how its used.

    I found a way to make it work in an array amending vectors one by one maybe there is a better way.

    func _on_Area_input_event(camera, event, click_position, click_normal, shape_idx):
    if event is InputEventMouseButton:
    if event.button_index == BUTTON_LEFT and event.pressed == true:
    print(translation)
    global_translate(position_list[1])

    var position_list = Array()

    func _physics_process(delta):
    position_list.append(Vector3(2,2,0))
    position_list.append(Vector3(2,-2,0))
    position_list.append(Vector3(12,2,0))

    Seems like that should work, though because the code is in _physics_process, it will keep appending the three values to the array continuously after every physics call (30 times a second by default). If you want to only add the positions once, you probably should append the positions in the _ready function.

  • MegalomaniakMegalomaniak Posts: 2,778Admin

    I'm guessing it's for regular expressions.
    https://en.wikipedia.org/wiki/Regular_expression

  • GodswarthGodswarth Posts: 8Member

    @TwistedTwigleg said:

    Seems like that should work, though because the code is in _physics_process, it will keep appending the three values to the array continuously after every physics call (30 times a second by default). If you want to only add the positions once, you probably should append the positions in the _ready function.

    Thanks, I was wondering why the array list never ended...

  • SIsilicon28SIsilicon28 Posts: 748Moderator

    @Godswarth said:
    Expression would be useful if I could understand whats going on with them. I don't see to much written about them or there use.

    It's actually quite simple really. :)
    An Expression that can be used to run any code dynamically; it's perfect for running commands in a list. For example:

    var expression := Expression.new()
    # the first parameter is your code, and the other are your input you can use later.
    var error := expression.parse("some_command(x, y)", ["x", "y"])
    
    # the first parameter are your inputs, and the other is a base object that anything inside the expression is referencing.
    # In this case, some_command is to be referenced from self.
    var result := expression.execute([2, 4], self)
    
  • CalinouCalinou Posts: 452Admin Godot Developer

    @SIsilicon28 Interesting, I didn't even know you could execute arbitrary methods on a base object using the Expression class. I guess I should add this to the Expression test suite in the future.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file