Send character to a random position?

  • #1, by OcttoManMonday, 29. December 2014, 10:34 6 years ago
    Hi,

    I have a space scene and want to add some ships flying, making a "send character to position (x and y)", being this random numbers, constantly flying around.

    "send ship1 x=¿¿, y=??, wait"
    "send ship1 x=¿, y=?, wait"
    "send ship1 x=!, y=¡, wait"
    pause 300
    "send ship1 x=¡, y=?, wait"
    1
    2
    3
    4
    5
    6


    grin or something like!

    Newbie

    16 Posts


  • #2, by afrlmeMonday, 29. December 2014, 14:41 6 years ago
    hmm you can use lua to send a character to a random position. There's no option for wait with lua script though. You could I suppose create a loop on send to position & check if character coordinates equals the same as the destination postion & if so then randomly generate new position after x time.

    1. create a called by other action & rename it to "dynamic_ship_movement".
    2. create a value & name "v_ship_movement_delay"
    3. add actions below into "dynamic_ship_movement"

    -- action parts
    set random value "v_ship_movement_delay" 0 to 10000 -- will set random value between 0 & 10 seconds
    pause for "v_ship_movement_delay" -- will pause for random value generated above
    execute a script > (see code block below)
    1
    2
    3
    4

    setRandCharPos()
    1
    2


    4. edit code below as needed & then add it to script section of editor.

    -- lua (definition script)
    local x, y -- empty variables
    local w = 1920 -- replace with intended scene width
    local h = 1080 -- replace with intended scene height
    local char = Characters["ship1"] -- replace with intended character name

    function setRandCharPos()
    x = math.random(w); y = math.random(h) -- create random destination
    char.CharacterDestination = {x = x, y = y} -- set new destination
    registerEventHandler("mainLoop", "CPLoop")
    end

    function CPLoop()
    if char.Position.x == x and char.Position.y == y then
    startAction("Actions[dynamic_ship_movement]")
    unregisterEventHandler("mainLoop", "CPLoop")
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17


    5. in an at begin of scene action for the scene call the "dynamic_ship_movement" action...

    This should create an automatic loop which will update the ships position every 0 to 10 seconds after ship reaches its destination. -- in theory as I've not tested it! razz

    Imperator

    6815 Posts

  • #3, by OcttoManMonday, 29. December 2014, 15:43 6 years ago
    Thanks, I'll try it, only one question before, i have de 3.7.1 version, should work equally?

    Newbie

    16 Posts

  • #4, by afrlmeMonday, 29. December 2014, 15:58 6 years ago
    oh er no. One sec.

    local active = false
    local x, y -- empty variables
    local w = 1920 -- replace with intended scene width
    local h = 1080 -- replace with intended scene height
    local char = getObject("Characters[ship1]") -- replace with intended character name

    function setRandCharPos()
    x = math.random(w); y = math.random(h) -- create random destination
    char:setValue( VCharacterDestination, {x = x, y = y} ) -- set new destination
    active = true
    end

    function CPLoop()
    if char:getPoint(VCharacterPosition).x == x and char:getPoint(VCharacterPosition).y == y and active then
    startAction("Actions[dynamic_ship_movement]")
    active = false
    end

    registerEventHandler("mainLoop", "CPLoop")
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    ...I've edited the definition script for the old scripting method of 3.7.1.

    Quick note: in 3.7.1 you are only allowed 1 mainLoop handler, so if you already have one then the mainLoop function in this will not be valid.

    Imperator

    6815 Posts

  • #5, by OcttoManMonday, 29. December 2014, 16:40 6 years ago
    I fail to fit the pieces, programing is not my strong side cry

    1.- In the scene, i create called by other action & renamed to "dynamic_ship_movement".
    2.- Create a value? Set value and radom value yes, but
    3.- In this part i totally lost, where goes it
    4.- OK


    With this sistem, I can put more of an object to different positions?

    maybe i start with the function "set random value", send the "object" to this position and them, in his stop frame add a script to send again to other position...or something like.

    Newbie

    16 Posts

  • #6, by afrlmeMonday, 29. December 2014, 16:49 6 years ago
    It doesn't matter where you create the called by other action as you can use/access it from anywhere. The name is important though as we are calling it inside of the script.

    Create a value in the scenes value tabs.

    Add the actions I mentioned to the called by other action you created. The third code block is what you should add to the execute a script action text input box. Basically it calls/executes the set position function.

    As for the main script itself. Create a new script inside of the script section of the editor. Paste in the script I provided & then navigate to the properties tab for the script & set it to "definition".

    Next create an at begin of scene action in the scenes action tab & then add a call action action part which you link to the called by other action that you created earlier.

    No real programming skills/knowledge required for this.

    Imperator

    6815 Posts

  • #7, by OcttoManTuesday, 30. December 2014, 19:19 6 years ago
    Finally i made it this way:

    1) Created value "pausetime" with a initial value 100 for example
    2) In the same scene > Actions > "called by other action":
    - execute script
    local nave1 = getObject('Characters[Nimbus]')
    nave1 :setValue(VCharacterDestination, { x=math.random(6000), y = math.random(3000)})
    1
    2
    3

    - wait until character ´Nimbus´ stops
    - pause for "pausetime"(value)
    - set random value in "pausetime" between 1 and 6000
    - Quit action "this called by other action"
    - Call action "this called by other action"

    3) In "the begining of scene" added a call to this action "called by other action".


    And it works fine, the ship fly around the scene and make his random breaks and i can add more ships.



    Thanks for all!

    Newbie

    16 Posts

  • #8, by afrlmeTuesday, 30. December 2014, 19:44 6 years ago
    I completely forgot about the wait until character stops action part! razz good call on that. Definitely more efficient than using a loop handler to check position.

    however your final 2 points were were not needed. Instead of the quit & call action, you should just use a "jump to x" action part. Leave it with the default setting & it will automatically loop back to the beginning of the loop.

    Also there's no need for the initial local nave1 seeing as you are using a single line script.

    1. set random value in "pausetime" between 0 and 6000
    2. pause for "pausetime" value
    3. execute a script >
    getObject('Characters[Nimbus]'):setValue( VCharacterDestination, { x = math.random(6000), y = math.random(3000) } )
    1
    2

    4. wait until character nimbus stops
    5. jump to action part #1

    it might be a good idea to create an at end of scene action & include a quit action of the called by other action to kill the loop.

    Imperator

    6815 Posts

  • #9, by OcttoManTuesday, 30. December 2014, 21:34 6 years ago
    Perfect!!wink

    Newbie

    16 Posts

  • #10, by MachtnixWednesday, 31. December 2014, 19:33 6 years ago
    ?
    Wie funktioniert das genau? Ich verstehe leider das Fachenglisch nicht so. Geht das in 4beta auch?

    Ich hatte sowas mal ohne LUA versucht und das folgendermaßen gefakt: wenn man mit der Maus über viele unsichtbare Aktionsbereiche fährt, werden wie zufällig Animationen gestartet (mit Verzögerung, Versetzung und so). Das wirkt denn genauso wie Zufall. Ich hatte mir gedacht, dass der Spieler ohnehin mit der Maus herumfährt, wenn nichts passiert... ;-)

    Frohes neues Jahr
    Machtnix

    Thread Captain

    1078 Posts

  • #11, by afrlmeThursday, 01. January 2015, 00:10 6 years ago
    Yes the final example was for 3.7.1 which will also work in vs4 plus.

    In this case: we are moving a spaceship in the background to a random position after a random amount of time, we wait for the spaceship to move to the random coordinates that were created & then we loop back to the beginning of the action, thus creating a continuous loop that dynamically updates the destination of the spaceship & all we needed was 1 value & 1 called by other action & 5 action parts. Nice & simple! smile

    Imperator

    6815 Posts

Write post