3

I imported Ursina module. It is a game engine.

I looked up a tutorial and in the code invoke() was used. I tried to find the documentation but there seems to be no information about it. The code from the tutorial is given below:

from ursina import *

# create a window
app = Ursina()

# most things in ursina are Entities. An Entity is a thing you place in the world.
# you can think of them as GameObjects in Unity or Actors in Unreal.
# the first paramenter tells us the Entity's model will be a 3d-model called 'cube'.
# ursina includes some basic models like 'cube', 'sphere' and 'quad'.

# the next parameter tells us the model's color should be orange.

# 'scale_y=2' tells us how big the entity should be in the vertical axis, how tall it should be.
# in ursina, positive x is right, positive y is up, and positive z is forward.

player = Entity(model='cube', color=color.orange, scale_y=2)

# create a function called 'update'.
# this will automatically get called by the engine every frame.

def update():
    player.x += held_keys['d'] * time.dt
    player.x -= held_keys['a'] * time.dt

# this part will make the player move left or right based on our input.
# to check which keys are held down, we can check the held_keys dictionary.
# 0 means not pressed and 1 means pressed.
# time.dt is simply the time since the last frame. by multiplying with this, the
# player will move at the same speed regardless of how fast the game runs.


def input(key):
    if key == 'space':
        player.y += 1
        invoke(setattr, player, 'y', player.y-1, delay=.25)


# start running the game
app.run()

Please help me.

P.S. I am using Linux Mint.

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
Adarsh TS
  • 193
  • 15

1 Answers1

4

It's used for calling a function with a delay, similar to Invoke() in Unity. So in this case it will decrease player.y by 1 after 0.25 seconds.

invoke(function, *args, **kwargs)

Edit: Example from documentation:

def test_func(item, x=None, y=None):
    print(item, x, y)

test_func('test')
invoke(test_func, 'test', delay=.1)
invoke(test_func, 'test1', 1, 2, delay=.2)
invoke(test_func, 'test2', x=1, y=2, delay=.3)`
pokepetter
  • 1,383
  • 5
  • 8
  • It cleared my doubt. But why is `'y'` an argument. Isn't `player.y-1` sufficient enough to do the same. – Adarsh TS Jan 30 '20 at 11:17
  • P.S. I just realized that the developer of Ursina has answered my query. Feeling Lucky! – Adarsh TS Jan 30 '20 at 11:18
  • 1
    That's just Python's setattr works. Added the example from the documentation to my answer. – pokepetter Jan 30 '20 at 12:07
  • I totally understood from your example. I checked the website for documentation for such examples but I did not find any. How to get those documentation. This is where I looked up : [link](https://www.ursinaengine.org/documentation.html) – Adarsh TS Jan 30 '20 at 13:54
  • 1
    It's under 'ursinastuff' (bad name I know). You can also ctrl+f for 'invoke' – pokepetter Jan 30 '20 at 13:58