Lench Scripter Mod [Besiege v0.42]

#42
Hello, I'm back again : D

As I was trying to implement a delay to my script, I noticed that there seems to be something wrong with while loops. If the loop is too long(?), it tanks the framerate, ups CPU usage and possibly crashes Besiege.

Here's a function that crashes Besiege when called:
Code:
function sleep(n)
    local t0 = besiege:getScaledTime()
    while besiege:getScaledTime() - t0 <= n do end
end
And this loop completely tanks my fps (with an i7-3770k)
Code:
x = 0
while x<20000 do
    x = x + 1 
end
I'd guess that the mod tries to tie the script updates to fps and introducing a two second delay is like making an immovable object meet an unstoppable force :p
 

Lench

Active Member
#43
Don't do it like that.

Lua scripts run synchronus with the game. This means that any delay in your script will delay the game too. Because of this, getScaledTime() returns the same value in every iteration of your while loop, so your loop is infinite and crashes the game.

What you should do instead is something like this:
Code:
trigger = true

function onUpdate()
     if besiege:getScaledTime() > 5 and trigger then
        -- do something 5 seconds after the start of the simulation here
        trigger = false
    end
end
 

ITR

l̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ֍̫̜̥̭͖̱̟̟͉͙̜̰ͅl̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ
Staff member
#44
GoofS said:
Hello, I'm back again : D

As I was trying to implement a delay to my script, I noticed that there seems to be something wrong with while loops. If the loop is too long(?), it tanks the framerate, ups CPU usage and possibly crashes Besiege.

Here's a function that crashes Besiege when called:
Code:
function sleep(n)
local t0 = besiege:getScaledTime()
while besiege:getScaledTime() - t0 <= n do end
end
And this loop completely tanks my fps (with an i7-3770k)
Code:
x = 0
while x<20000 do
x = x + 1
end
I'd guess that the mod tries to tie the script updates to fps and introducing a two second delay is like making an immovable object meet an unstoppable force :p
The mod is single-threaded, so it's exactly what should happen with such an evil script
 

Lench

Active Member
#45
Release v1.1.0

Block identifiers are no longer printed to console, but displayed in a separate window with handy copy buttons.


Lua scripts can now be saved to .bsg file, so it can be shared through workshop and other platforms with a single file.


Added piston SetPosition method.
Added spring and rope SetInput method.

More information in changelog.
 

Lench

Active Member
#46
Release v2.0.0

Script engine moved from NLua to IronPython - mod now runs Python 2.7 scripts
Breaking all backwards compatibility

Full updated documentation here: https://github.com/lench4991/LenchScripterMod/wiki

Added custom raycasting and tracked collider functionality.
You can now create raycast with origin and direction vectors and use them to find out what object they hit, track it and control it.

Example:

https://gfycat.com/NegligibleEntireBarracuda

Code:
laser = Besiege.GetBlock("LASER 1")
def Update():
    origin = laser.Position + laser.Forward
    direction = laser.Forward
    try:
        collider = Besiege.GetRaycastCollider(origin, direction)
        if collider.Name == "WaterCannon":
            collider.Block.Shoot()
    except Exception as e:
        # If raycast doesn't hit anything, it throws an exception.
        print e
 
#47
Is it possible to have an example of a completed script for the drone in your getting started tutorial ? I'm confused as to where things go, probably because I have never written python before.
 

Lench

Active Member
#48
I haven't converted the drone script yet, but you can compare Lua and Python scripts for Rocket Artillery I made a while ago.
Python: http://pastebin.com/Jtwkfc5m
Lua: http://pastebin.com/h2qxkETd
Machine: http://steamcommunity.com/sharedfiles/filedetails/?id=684802203

They're quite bigger and should help you better with porting your scripts. Also, some other things you should know before porting:

- Lists in Python start at index 0, unlike in Lua, where they start at 1.
- I recommend using built-in math module instead of Mathf where possible. There are some issues (like the Mathf.Clamp method) with calling method overloads.
- Python differentiates between integer and float types, while Lua only knows the generic number type.
- In Python, identation matters!

Overall, Python is much better equipped with libraries, rich object oriented capabilities and exception handling. I'm sure you'll like it when you get more familiar with it.
 
#49
Lench i have a request, would you beable to make a raycast and see the size if it aswell in this? then with that, use it as a variable power control for a water cannon?
 
#51
Lench i mean basically, be able to get the length of the raycast, or the distance to the ground from a certain poing, and then use that to exactly control another block, as the automatotron mods, sensors arent good enough soo yeah.
 

Lench

Active Member
#53
patch v2.0.1:
Updated for Besiege v0.3

Edit: Due to my poor testing, I missed a bug. Download the mod again if cannons cause your script not to run.
 
#55
Lench, i keep getting an unexpected token error with the script you sent me, ive tried modifying it to accomidate for the extra cannons and to fix it but have had no luck, here is the error,
/b>
File "D:/Besange/Besiege/Besiege_Data/Scripts/raycast_distance_example.py", line 20
water_cannon1.SetSliderValue("POWER", distance / 10.0)

^
SyntaxError: unexpected token 'water_cannon1'
 
#57
yes i will, here


laser = Besiege.GetBlock(&quot;STARTING BLOCK 1&quot;)
water_cannon1 = Besiege.GetBlock(&quot;WATER CANNON 1&quot;)
water_cannon1.SetToggleMode(&quot;HOLD TO SHOOT&quot;, True)
water_cannon2 = Besiege.GetBlock(&quot;WATER CANNON 2&quot;)
water_cannon2.SetToggleMode(&quot;HOLD TO SHOOT&quot;, True)
water_cannon3 = Besiege.GetBlock(&quot;WATER CANNON 3&quot;)
water_cannon3.SetToggleMode(&quot;HOLD TO SHOOT&quot;, True)
water_cannon4 = Besiege.GetBlock(&quot;WATER CANNON 4&quot;)
water_cannon4.SetToggleMode(&quot;HOLD TO SHOOT&quot;, True)

def Update():
# we use laser position as origin of the raycast and add forward vector for offset to avoid raycasting laser block body
origin = laser.Position + laser.Forward
# we use laser forward direction vector as raycast direction
direction = laser.Forward

try: # raycasting raises exception if it doesn't hit anyt hing
hit = Besiege.GetRaycastHit(origin, direction) # point of the raycast hit
distance = (hit - origin).magnitude # calcuate distance from origin to hit position
water_cannon1.SetSliderValue(&quot;POWER&quot;, distance / 10.0)
water_cannon1.Shoot()
water_cannon2.SetSliderValue(&quot;POWER&quot;, distance / 10.0)
water_cannon2.Shoot()
water_cannon3.SetSliderValue(&quot;POWER&quot;, distance / 10.0)
water_cannon3.Shoot()
water_cannon4.SetSliderValue(&quot;POWER&quot;, distance / 10.0)
water_cannon4.Shoot()
except:
pass # do nothing
 
#58
here it is Lench , it wont display well as a comment so ill try here, also it is just yours but modified


Code:
laser = Besiege.GetBlock("STARTING BLOCK 1")
water_cannon1 = Besiege.GetBlock("WATER CANNON 1")
water_cannon1.SetToggleMode("HOLD TO SHOOT", True)
water_cannon2 = Besiege.GetBlock("WATER CANNON 2")
water_cannon2.SetToggleMode("HOLD TO SHOOT", True)
water_cannon3 = Besiege.GetBlock("WATER CANNON 3")
water_cannon3.SetToggleMode("HOLD TO SHOOT", True)
water_cannon4 = Besiege.GetBlock("WATER CANNON 4")
water_cannon4.SetToggleMode("HOLD TO SHOOT", True)

def Update():
    # we use laser position as origin of the raycast and add forward vector for offset to avoid raycasting laser block body
    origin = laser.Position + laser.Forward
    # we use laser forward direction vector as raycast direction
    direction = laser.Forward

    try:  # raycasting raises exception if it doesn't hit anyt hing
        hit = Besiege.GetRaycastHit(origin, direction)   # point of the raycast hit
        distance = (hit - origin).magnitude              # calcuate distance from origin to hit position
        water_cannon1.SetSliderValue("POWER", distance / 10.0)
        water_cannon1.Shoot()
        water_cannon2.SetSliderValue("POWER", distance / 10.0)
        water_cannon2.Shoot()
        water_cannon3.SetSliderValue("POWER", distance / 10.0)
        water_cannon3.Shoot()
        water_cannon4.SetSliderValue("POWER", distance / 10.0)
        water_cannon4.Shoot()
    except:
        pass  # do nothing
 

Lench

Active Member
#59
No idea, sorry. It works fine for me. Some formatting is lost when posting code without indents, so it might be that, but otherwise I don't see anything wrong.
 
Top