Main Menu

Methods?

Started by milkev, April 23, 2016, 10:03:17 PM

Previous topic - Next topic

milkev

Is it possible to make methods in crpl cores?

TLFP

Quote from: milkev on April 23, 2016, 10:03:17 PM
Is it possible to make methods in crpl cores?
Um, I have no idea what "methods" are. Some more explanation?
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

milkev

They are a user created function( like SetCreeper) that runs code set by the player when the command is called. in java a method is created using

int[nameOfMethod](variables input to the meothod) {code thats run when methods called }

Ex:

this would create a emmitter at set coords (x and y) with a amt of (amt) and a delay of (delay)

int CreateEmmitter(x y amt delay) {
    CreateUnit(EMITTER CurrentCoords)
    (idk how to create it with custom variables)
}

TLFP

Quote from: milkev on April 23, 2016, 10:33:00 PM
They are a user created function( like SetCreeper) that runs code set by the player when the command is called. in java a method is created using

int[nameOfMethod](variables input to the meothod) {code thats run when methods called }

Ex:

this would create a emmitter at set coords (x and y) with a amt of (amt) and a delay of (delay)

int CreateEmmitter(x y amt delay) {
    CreateUnit(EMITTER CurrentCoords)
    (idk how to create it with custom variables)
}
Ok, then this is how you can do stuff like that.
:Examplecodename
code goes here...
Then call that in something.
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

GoodMorning

The CRPL name for this concept is a "function". For example:


#Function Demonstration
once
  180 Delay # 1 minute
  4 0 do
    @BigHelp
  loop 
endonce

:BigHelp # [-] Turns the four deepest Creeper cells to 2K AC.
  GetDeepestCreeperCell -2000 SetCreeper


The [-] bit in the comment on the line with the : is something I include to document what the function is supposed to do to the stack, in a similar way to the wiki documentation.
Functions can be called from each other, recursively if need be...

To implement your example:

:BuildEmitter #[X Y Interval Amt Wait - UID] Build an Emitter with specified vars.
  ->BE_Y ->BE_X ->BE_Interval ->BE_Amt ->BE_Wait
  #Add the function-name acronym to the start to reduce confusion of variables. This is in case you have Y or X used elsewhere, and need them unchanged
  "EMITTER" <-BE_X <-BE_Y CreateUnit ->BE_UID
  <-BE_UID 0 "PRODUCTIONAMT" <-BE_Amt SetScriptVar
  <-BE_UID 0 "PRODUCTIONINTERVAL" <-BE_Interval SetScriptVar
  <-BE_UID 0 "STARTTIME" <-BE_Wait SetScriptVar
  <-BE_UID

Don't forget that if you don't need the UID, you will need to remove it from the stack.

In C, JavaScript, ... the name for a code block like this is a function. When it's part of an object (a concept which doesn't exist in CRPL, as it is widely recognised), it's called a method. Just to be confusing, in other languages it's called a "sub", short for subroutine. It it unclear enough yet?

For this purpose, look at the relevant CRPL tutorial section.
A narrative is a lightly-marked path to another reality.

milkev

#5
ok, so the example in the tutorials you referenced works fine, but the ones you made don't do anything for me. on the bighelp one i tried to set the delay to 1, so i can see if its working but nutin, i also tried removing the once, so it just repeats the program over and over... also nothing. i tried creating one on my own too, but that doesnt do anything either :(


$amt:20
$interval:30

@emit
@interval
#----------------
:emit
SetCreeper(<-amt CurrentCoords)
:interval
<-interval Delay
[code/]

TLFP

Quote from: milkev on April 24, 2016, 05:38:00 PM
ok, so the example in the tutorials you referenced works fine, but the ones you made don't do anything for me. on the bighelp one i tried to set the delay to 1, so i can see if its working but nutin, i also tried removing the once, so it just repeats the program over and over... also nothing. i tried creating one on my own too, but that doesnt do anything either :(


$amt:20
$interval:30

@emit
@interval
#----------------
:emit
SetCreeper(<-amt CurrentCoords)
:interval
<-interval Delay
[code/]

Here are some links to the CRPL cheatsheet.
http://knucklecracker.com/wiki/doku.php?id=crpl:docs:func
http://knucklecracker.com/wiki/doku.php?id=crpl:docs:call
Hope that helps some!  :D
Currently ghosting, don't mind me.
Christian, my list of various content creation groups can be found here

GoodMorning

Those were written in-browser. I didn't test them, and they are likely broken.

The key point is that a function is defined with :FUNC_NAME and called with @FUNC_NAME. Other than that, it's code like any other, and works on the same stack.

I'm not sure if you are allowed functions with the same names as variables.

Oh, and the proper ending to a code block puts the "/" before "code" in the square brackets. /code, not code/.
A narrative is a lightly-marked path to another reality.

Vanguard

#8
Quote from: milkev on April 24, 2016, 05:38:00 PM
ok, so the example in the tutorials you referenced works fine, but the ones you made don't do anything for me. on the bighelp one i tried to set the delay to 1, so i can see if its working but nutin, i also tried removing the once, so it just repeats the program over and over... also nothing. i tried creating one on my own too, but that doesnt do anything either :(


$amt:20
$interval:30

@emit
@interval
#----------------
:emit
SetCreeper(<-amt CurrentCoords)
:interval
<-interval Delay
[code/]

As far as I know, CRPL is not OOP, so no methods, only functions.

Mixing forward and reverse notation in the same code is what I try to minimize, as it can be quite confusing.

Helpful hints: http://knucklecracker.com/forums/index.php?topic=20474.0

Next step: Debugging.


# Sticking to all Reverse/stack notation
once
# Always seperate your code into once/endonce for readability. This tells you exactly what your initial conditions were.
20 ->amt
30 ->interval
ShowtraceLog
# Use the tracelog
endonce

@emit
@interval
#----------------
:emit
        #Using the simpler Addcreeper: http://knucklecracker.com/wiki/doku.php?id=crpl:docs:addcreeper
        Trace ("emit was called. We want to place " <-amt " at the current location: " Currentcoords concat concat concat concat)
Currentcoords <-amt AddCreeper
:interval
         Trace ("Sleeping for " <-interval "frames" concat concat)
<-interval Delay


This should tell you every second the exact same thing in the tracelog. One trace from :emit and one from :interval.

More questions and suggestions: Have you checked wether the scripts compile at all in the editor? Have you run the traces? If you have traces, post them, so everyone can see were the problems in the code are.

Thanks and keep on CRPLing :)

GoodMorning

Vanguard has put it the way I would.

The closest thing to OOP that CRPL has involves entire cores as objects. This is clearly impractical for most OOP purposes.

Here's a function I have gotten good use out of, and that I am certain can work:

:TraceCauseOfDeath #[Cause - ] Cause is what will appear in trace.
  IsEditMode
  if
    Trace
    PauseGame
    ShowTraceLog
    1 Delay
    HideTraceLog
  else
    pop
  endif


I have used this a number of times, putting
"I died of ____" @TraceCauseOfDeath
immediately before
Self 0 Destroy

Vanguard, you have avoided warp notation everywhere but the Trace arguments.
Also, the first Trace would work better broken into:
":emit was called. We want to place " <-amt " at the current location: " Trace3
CurrentCoords Trace2
A narrative is a lightly-marked path to another reality.

Vanguard

Quote from: GoodMorning on April 25, 2016, 05:42:56 PM
Vanguard, you have avoided warp notation everywhere but the Trace arguments.
I said I try to minimize, nobody is perfect.  ;D

For some odd reason, when it comes to trace, my PHP coding-style breaks through. I am damaged like that.

GoodMorning

I find that RPL is harder to look at later, but easier to write in the first place.

I know: let's write a script in Pythn to run on a Java VM, rearranging C++ code into CRPL. That will definitely make things better and not worse.
A narrative is a lightly-marked path to another reality.