Difference between revisions of "Razor Enhanced Basics"

From UO Eventine Wiki
Jump to navigation Jump to search
Line 79: Line 79:
  
 
'''Misc.Pause(650)''' is set to pause for 650ms either after the healing method runs to heal yourself, or your hits are above 50 and do not go to the method. It is always good to have a pause here when this script will loop of some sort to help your pc from a potential crash from running too fast and having an error occur.
 
'''Misc.Pause(650)''' is set to pause for 650ms either after the healing method runs to heal yourself, or your hits are above 50 and do not go to the method. It is always good to have a pause here when this script will loop of some sort to help your pc from a potential crash from running too fast and having an error occur.
 +
 +
 +
 +
We could also use this same method with parameters if we need to do some specific things when a query/condition is met on a thing.
 +
 +
'''EXAMPLE:'''
 +
<syntaxhighlight lang="python">
 +
pausetime = 650
 +
 +
def DoMove(scont, tcont):
 +
    Items.UseItem(scont)
 +
    Items.WaitForContents(scont,2500)
 +
    for i in scont.Contains:
 +
        Items.Move(i,tcont,-1)
 +
        Misc.Pause(pausetime)
 +
    Misc.SendMessage('Done Moving Items', 32)
 +
 +
source = Target.PromptTarget('Select Source Container')
 +
sourcecont = Items.FindBySerial(source)
 +
target = Target.PromptTarget('Select Target Container')
 +
targetcont = Items.FindBySerial(target)
 +
if sourcecont and targetcont:
 +
    DoMove(sourcecont, targetcont) 
 +
Misc.SendMessage('Script End', 32)
 +
</syntaxhighlight>
 +
 +
'''Breakdown:'''
 +
 +
'''pausetime = 650''' is setting a variable to a pause time that we can use for all pauses in this script in case we wanted more than one to save time having to remember changing more than one pause. This would allow you to change all the timers at once with one variable change.
 +
 +
'''def DoMove(scont, tcont):''' is a method that has two incoming parameters 'scont' and 'tcont' which from later in the script will come in as Items and since their itemid are stored in RE as Containers it will know they can be accessed as containers.
 +
 +
''notice here we have an indent again to make sure that all the lines underneath with this indent or further will only happen when this method is called upon.
 +
 +
'''Items.UseItem(scont)''' simulates a double click on an item. so in this case it will open 'scont' with a double click.
 +
 +
'''Items.WaitForContents(scont,2500)''' will not continue past until either all the contents are loaded from the server on the 'scont' or 2500ms (2.5 seconds) have passed. This is used to make sure you don't miss an item when having the script look through them.
 +
 +
'''for i in scont.Contains: ''' This will go through every item inside the 'scont' 1 at a time and do anything indented directly under this tells Razor to do.
 +
 +
''notice an indent here to show that the indent that is now 2 indents wide will all work inside the above "for" statement.''
 +
 +
'''Items.Move(i,tcont,-1)''' this is tellign the item it is on (i) to be moved to the 'tcont' container. the -1 represents any amount. (setting amount to 3 on a stackable item would result in moving only 3 of the stack).
 +
 +
'''Misc.Pause(pausetime)''' is telling the script to pause before any more actions for the set time at the top of the script with 'pausetime' variable being called.
 +
 +
''notice we went back to only one indent. This means this action will only run after the 'for' statement has finished.''
 +
 +
'''Misc.SendMessage('Done Moving Items', 32)'' is telling the client to simulate a system message on the left that says "Done Moving Items" with a hue of 32.
 +
 +
''notice that the indents are gone this means these actions will run when you hit play unlike the method above which needs to be called.''
 +
 +
'''source = Target.PromptTarget('Select Source Container')''' is sending a target to your client to have you target an item that will be your source container. This will return the serial of the container.
 +
 +
'''sourcecont = Items.FindBySerial(source)''' is setting the item you selected by the target as an item to Razor Enhanced so it knows it is an item.
 +
 +
'''target= Target.PromptTarget('Select Target Container')''' is sending a target to your client to have you target an item that will be your source container. This will return the serial of the container.
 +
 +
'''targetcont = Items.FindBySerial(target)''' is setting the item you selected by the target as an item to Razor Enhanced so it knows it is an item.
 +
 +
'''if sourcecont and targetcont:''' this is having razor enhanced to make sure both items exist.
 +
 +
''notice the indent here. this is saying that this next line will only happen if razor enhanced determines 'sourcecont' and 'targetcont' both exist.
 +
 +
'''DoMove(sourcecont, targetcont)''' this is calling the method of DoMove with two parameters that were needed for the method. It is sending first 'sourcecont' (the source container you targeted) and 'targetcont' (the target container you targeted).
 +
 +
''notice the indent has moved again. this is to let you know we are no longer only doing this if the 'sourcecont' and 'targetcont' exist, it will happen even if they do not.
 +
 +
'''Misc.SendMessage('Script End', 32)''' is telling the client to simulate a system message on the left that says "Script End" with a hue of 32. The script will end after this.
  
 
=Lists=
 
=Lists=

Revision as of 22:53, 24 June 2023

This page will add some basics to know and help when coding things in Razor Enhanced. The attempt on writing this is to make it as easy as possible to understand some things and will just give a basic example and try to not get too technical to keep learning as easy.

Indent

An Indent is a very important part of Python. Indents can be made by hitting the Tab key or space bar to create 4 empty spaces. Using Indents are required to add actions or responses to use when a query/condition is met.


EXAMPLE:

If Player.Hits < 20:
    Player.ChatSay("Health is low!")
    Misc.Pause(3500)  
Misc.Pause(50)
Misc.SendMessage("No Longer inside Query")
‎

With this above example, we can see that when the query of if your player has less than 20 hitpoints, it will say 'Health is low!" and then pause for 3.5 seconds. It will then exit what to do after as there are no more direct lines beneath that are in the same indent. This will result in a pause of 50ms and the send message "No Longer inside Query" to go if hits are 20 or over or go after the above conditions actions are finished.


Having actions for a query/condition met inside the first would require a new indent to be placed.


EXAMPLE:

If Player.Hits < 20:
    Player.ChatSay("Health is low!")
    Misc.Pause(3500)
    If Player.Stam > 200:
        Player.ChatSay("But our stamina is over 200!")
        Misc.Pause(2500)
Misc.Pause(50)
Misc.SendMessage("No Longer inside Query")
‎

With this code, we added a check to see when our hits are under 20 if our stamina is over 200. This query/condition when met will result in it having the player say "But our stamina is over 200!" and then pause for 2500. This query/condition can only be accessed if the first query/condition is met of the player's hit points being under 20.

Methods

A method can be a sample of code to run that is only called upon when needed and will not force razor enhanced to only call it when part of the code requires it.

EXAMPLE:

self = Mobiles.FindBySerial(Player.Serial)

def castHealOnSelf():
    Spells.Cast("Greater Heal")
    Target.WaitForTarget(3500,False)
    Target.TargetExecute(self)
    
while Player.Connected:
    if Player.Hits < 50:
        castHealOnSelf()
    Misc.Pause(650)

Let's break down the above code into parts to see what is going on to learn more about what is happening.

self = Mobiles.FindBySerial(Player.Serial) This code is setting the 'self' variable to be linked to the player and tells Razor Enhanced that the variable is also a mobile so we can call on it later without having to write too much code every time.

def castHealOnSelf(): is the method that can be called at any time in this script by using "castHealOnSelf()". This will run all lines that are indented under it before a line that is not indented appears, which in this case, is 3 lines.

notice the next three lines are indented to tell razor enhanced they will run with the above method

Spells.Cast("Greater Heal") tells the game to cast the spell "Greater Heal".

Target.WaitForTarget(3500,False) is telling the script to not go further until a target appears on screen or 3500ms goes past (3.5 seconds). If the target does not appear it will just continue through as well so setting the wait time can vary on latency or your isps routing to a server.

Target.TargetExecute(self) is having the spell target yourself with the 'self' variable we set at the top stating it is for your player.

while Player.Connected: means that the code will run as long as the player is connected to the game. If they are not this code will stop.

these next three lines are indented so they will only run if your player is connected to the game.

if Player.Hits < 50: is a condition that needs to be met to run the following lines again. This one will only run the below code if your hit points go under 50.

castHealOnSelf() is where the script itself is calling the method to heal yourself we set above.

Misc.Pause(650) is set to pause for 650ms either after the healing method runs to heal yourself, or your hits are above 50 and do not go to the method. It is always good to have a pause here when this script will loop of some sort to help your pc from a potential crash from running too fast and having an error occur.


We could also use this same method with parameters if we need to do some specific things when a query/condition is met on a thing.

EXAMPLE:

pausetime = 650 

def DoMove(scont, tcont):
    Items.UseItem(scont)
    Items.WaitForContents(scont,2500)
    for i in scont.Contains: 
        Items.Move(i,tcont,-1)
        Misc.Pause(pausetime)
    Misc.SendMessage('Done Moving Items', 32)

source = Target.PromptTarget('Select Source Container')
sourcecont = Items.FindBySerial(source)
target = Target.PromptTarget('Select Target Container')
targetcont = Items.FindBySerial(target)
if sourcecont and targetcont:
    DoMove(sourcecont, targetcont)  
Misc.SendMessage('Script End', 32)

Breakdown:

pausetime = 650 is setting a variable to a pause time that we can use for all pauses in this script in case we wanted more than one to save time having to remember changing more than one pause. This would allow you to change all the timers at once with one variable change.

def DoMove(scont, tcont): is a method that has two incoming parameters 'scont' and 'tcont' which from later in the script will come in as Items and since their itemid are stored in RE as Containers it will know they can be accessed as containers.

notice here we have an indent again to make sure that all the lines underneath with this indent or further will only happen when this method is called upon.

Items.UseItem(scont) simulates a double click on an item. so in this case it will open 'scont' with a double click.

Items.WaitForContents(scont,2500) will not continue past until either all the contents are loaded from the server on the 'scont' or 2500ms (2.5 seconds) have passed. This is used to make sure you don't miss an item when having the script look through them.

for i in scont.Contains: This will go through every item inside the 'scont' 1 at a time and do anything indented directly under this tells Razor to do.

notice an indent here to show that the indent that is now 2 indents wide will all work inside the above "for" statement.

Items.Move(i,tcont,-1) this is tellign the item it is on (i) to be moved to the 'tcont' container. the -1 represents any amount. (setting amount to 3 on a stackable item would result in moving only 3 of the stack).

Misc.Pause(pausetime) is telling the script to pause before any more actions for the set time at the top of the script with 'pausetime' variable being called.

notice we went back to only one indent. This means this action will only run after the 'for' statement has finished.

'Misc.SendMessage('Done Moving Items', 32) is telling the client to simulate a system message on the left that says "Done Moving Items" with a hue of 32.

notice that the indents are gone this means these actions will run when you hit play unlike the method above which needs to be called.

source = Target.PromptTarget('Select Source Container') is sending a target to your client to have you target an item that will be your source container. This will return the serial of the container.

sourcecont = Items.FindBySerial(source) is setting the item you selected by the target as an item to Razor Enhanced so it knows it is an item.

target= Target.PromptTarget('Select Target Container') is sending a target to your client to have you target an item that will be your source container. This will return the serial of the container.

targetcont = Items.FindBySerial(target) is setting the item you selected by the target as an item to Razor Enhanced so it knows it is an item.

if sourcecont and targetcont: this is having razor enhanced to make sure both items exist.

notice the indent here. this is saying that this next line will only happen if razor enhanced determines 'sourcecont' and 'targetcont' both exist.

DoMove(sourcecont, targetcont) this is calling the method of DoMove with two parameters that were needed for the method. It is sending first 'sourcecont' (the source container you targeted) and 'targetcont' (the target container you targeted).

notice the indent has moved again. this is to let you know we are no longer only doing this if the 'sourcecont' and 'targetcont' exist, it will happen even if they do not.

Misc.SendMessage('Script End', 32) is telling the client to simulate a system message on the left that says "Script End" with a hue of 32. The script will end after this.

Lists

Import

Gumps

Looping