Question in title. The reason I want this is so I don't have to keep inputting Xthing.crpl into a script. :P
So like this:$Xvar:"Xscript"
#blablabla...
#here's the part I'm interested in
<-Xunit "Xvar" Addscripttounit
This is not real code, just an example.
I've tried to do this and all it does is add "Xvar.crpl" to the core.
The script has to exist in the scripts folder in the world editor to make AddScriptToUnit(unit ScriptName) work.
Having a month's knowledge of CRPL, I can tell you that you can't write scripts within scripts...
Sorry!
-GameGibu
Actually I'm not sure if this even answers your question, but it's a good answer to its matching question.
Just so you can let your imagination go wild, this is real code (assuming you have a script Xscript.crpl):
$Xvar:"Xscript.crpl"
#blablabla...
#here's the part I'm interested in
<-Xunit <-Xvar Addscripttounit
You can't write code inside code (just do it in code instead of a string), but you can have a ton of scripts stored and have a script attach a random one of them or one given through the in-game input fields. Just don't forget the ".crpl".
edit: Just realised I'm now finally one of the top-10 posters :P
Quote from: J on January 13, 2016, 05:14:29 PM
Just so you can let your imagination go wild, this is real code (assuming you have a script Xscript.crpl):
$Xvar:"Xscript.crpl"
#blablabla...
#here's the part I'm interested in
<-Xunit <-Xvar Addscripttounit
You can't write code inside code (just do it in code instead of a string), but you can have a ton of scripts stored and have a script attach a random one of them or one given through the in-game input fields. Just don't forget the ".crpl".
edit: Just realised I'm now finally one of the top-10 posters :P
Thanks! And I don't have a script called that, just as an example. :D
How would you do it randomly because I have 5 scripts that I want a unit to build around it separately and with a delay and not mess everything up. Usually what happens is 2 separate scripts are attached to the same unit and it will only build one of the units (with or without the error).
To randomize:
RandInt(0 5) "scriptSuffix" concat ->RandScriptName
#then
<-unit <-RandScriptName AddScriptToUnit
Hope that helps!
-GameGibu
Lists and random numbers.
#Create a list
CreateList ->scriptList
#Put all your script names into it (don't forget the ".crpl" on the filenames)
<-scriptList "someScript1.crpl" AppendToList
<-scriptList "someScript2.crpl" AppendToList
<-scriptList "someOtherScript.crpl" AppendToList
<-scriptList "yetAnotherScript.crpl" AppendToList
<-scriptList "someScript3.crpl" AppendToList
#NOTE: everything above here should likely be in a ONCE block
#using GetListCount to feed the random number generator instead of 5 in case you want to add or remove from the list
#this should spit out one of the script names from the list above:
<-scriptList RandInt(0 GetListCount(<scriptList)) GetListElement
#You can either put that line into the AddScriptTounit command, or save it to a variable to make the next part easier to read
->randomScript
#your unit creation stuff followed by the
#...
<-someUnitID <-randomScript AddScriptToUnit
Quote from: GameGibu on January 14, 2016, 09:11:14 AM
To randomize:
RandInt(0 5) "scriptSuffix" concat ".crpl" concat ->RandScriptName
#then
<-unit <-RandScriptName AddScriptToUnit
Hope that helps!
-GameGibu
That works too, but don't forget to concat ".crpl" to the end of the script name as well.
Note that RandInt(A B) generates numbers from A to (B-1). In the above example, 0-4. This works well with my use of a list above, as there are 5 list elements, numbered 0-4.
Quote from: Grayzzur on January 14, 2016, 09:13:35 AM
Quote from: GameGibu on January 14, 2016, 09:11:14 AM
To randomize:
RandInt(0 5) "scriptSuffix" concat ".crpl" concat ->RandScriptName
#then
<-unit <-RandScriptName AddScriptToUnit
Hope that helps!
-GameGibu
That works too, but don't forget to concat ".crpl" to the end of the script name as well.
Note that RandInt(A B) generates numbers from A to (B-1). In the above example, 0-4. This works well with my use of a list above, as there are 5 list elements, numbered 0-4.
The code I'm trying to randomize is intended to be a PAC with sleeper units map. It's supposed to create ONE core if there is creeper under it, destroy creeper under it, and attach ONE script to it per 10 seconds. Instead, it attaches 0-2 scripts to a core and does it 1 every .5 seconds and causes LAG. Here's the code I'm trying to randomize
$NeededAmount:50
#Self CONST_MAXAMMO <-NeededAmount SetUnitAttribute
if(<-creeperAmount <-NeededAmount gte)
0 5 RandInt ->Creep
150 delay
"CRPLCore" 33 26 12 RandCoordsInRange CreateUnit ->unit
if(<-Creep 1 eq)
<-unit "CCannon.crpl" AddScriptToUnit
endif
if(<-Creep 2 eq )
<-unit "CSniper.crpl" AddScriptToUnit
endif
if(<-Creep 3 eq)
<-unit "CSprayer.crpl" AddScriptToUnit
endif
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
endif
<-creeperAmount <-creeperAmount sub
delay 30
if(<-time neq0)
<-time <-time sub
endif
I'm still not finished with it as I want there to be a bar indicating how much creeper is left to go AND not cause serious LAG.
If you're wondering how I made this sorta work I changed all the custom images that go along with it.
EDIT: This is what happens:
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
No conditional there being applied to Creep, so anytime Creep is zero it won't do anything and anytime it's ANY other number you get a CMortar added. (In addition to the scripts you add if it's 1, 2 or 3.)
<-creeperAmount <-creeperAmount sub
<-time <-time sub
Not sure what you're trying to do with these lines. You're putting a value on the stack twice and subtracting, giving you a zero, and you're leaving the zero on the stack and not doing anything with it. If the intent is to reduce the variable by some amount, it would look like this, to say subtract 10 and store the answer back in the same variable:
<-creeperAmount 10 sub ->creeperAmount
Quote from: Grayzzur on January 14, 2016, 11:09:13 AM
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
No conditional there being applied to Creep, so anytime Creep is zero it won't do anything and anytime it's ANY other number you get a CMortar added. (In addition to the scripts you add if it's 1, 2 or 3.)
<-creeperAmount <-creeperAmount sub
<-time <-time sub
Not sure what you're trying to do with these lines. You're putting a value on the stack twice and subtracting, giving you a zero, and you're leaving the zero on the stack and not doing anything with it. If the intent is to reduce the variable by some amount, it would look like this, to say subtract 10 and store the answer back in the same variable:
<-creeperAmount 10 sub ->creeperAmount
I'm trying to reset those variables so I don't have the creeps being made all the time. The time variable is supposed to delay the creeps being made, because the delay doesn't work. On top of that I want the core to subtract creeper every time it makes a creep.
Where this gets creeper amount using GetCreeper?
Do you want it SetCreeper 0 always when it creates unit?
Edit:There is never GetCreeper command so how it knows is there enough creeper under it?
Quote from: Builder17 on January 14, 2016, 11:59:58 AM
Where this gets creeper amount using GetCreeper?
Do you want it SetCreeper 0 always when it creates unit?
Yes so you don't have free creeps.
There is no GetCreeper command so how it knows is there enough creeper under it .
And it also never sets creeper in current coordinates into zero.
Quote from: Builder17 on January 14, 2016, 12:35:31 PM
There is no GetCreeper command so how it knows is there enough creeper under it .
And it also never sets creeper in current coordinates into zero.
I updated the code:
$NeededAmount:50
#Self CONST_MAXAMMO <-NeededAmount SetUnitAttribute
if(<-creeperAmount <-NeededAmount gte)
0 5 RandInt ->Creep
150 delay
"CRPLCore" 33 26 12 RandCoordsInRange CreateUnit ->unit
if(<-Creep 1 eq)
<-unit "CCannon.crpl" AddScriptToUnit
endif
if(<-Creep 2 eq )
<-unit "CSniper.crpl" AddScriptToUnit
endif
if(<-Creep 3 eq)
<-unit "CSprayer.crpl" AddScriptToUnit
endif
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
endif
CurrentCoords 0 Setcreeper
<-creeperAmount <-creeperAmount sub ->creeperAmount
delay 30
if(<-time neq0)
<-time <-time sub ->time
endif
Should you go looking better into CRPL section of wiki?
knucklecracker.com/wiki/doku.php?id=crpl:start
Quote from: Builder17 on January 14, 2016, 01:25:44 PM
Should you go looking better into CRPL section of wiki?
knucklecracker.com/wiki/doku.php?id=crpl:start
That's how I figure out my scripts. I just want a script that creates random units at the cost of some creeper and places them randomly around a area.
Quote from: Grayzzur on January 14, 2016, 09:13:35 AM
Quote from: GameGibu on January 14, 2016, 09:11:14 AM
To randomize:
RandInt(0 5) "scriptSuffix" concat ".crpl" concat ->RandScriptName
#then
<-unit <-RandScriptName AddScriptToUnit
Hope that helps!
-GameGibu
That works too, but don't forget to concat ".crpl" to the end of the script name as well.
Note that RandInt(A B) generates numbers from A to (B-1). In the above example, 0-4. This works well with my use of a list above, as there are 5 list elements, numbered 0-4.
It was assumed ScriptSuffix contained .crpl already...
Quote from: TheLongFellowPLAYERThe code I'm trying to randomize is intended to be a PAC with sleeper units map. It's supposed to create ONE core if there is creeper under it, destroy creeper under it, and attach ONE script to it per 10 seconds. Instead, it attaches 0-2 scripts to a core and does it 1 every .5 seconds and causes LAG.
Scripts run 30 times per second. Make a variable that increments every invocation, and
if(<-myTime <-DelayInSeconds mul(30) mod eq0)
#execute timed stuff
endif
Using delay is kinda sloppy and messes up timing. This method always works for me.
-GameGibu
Quote from: GameGibu on January 15, 2016, 07:13:16 AM
Quote from: TheLongFellowPLAYERThe code I'm trying to randomize is intended to be a PAC with sleeper units map. It's supposed to create ONE core if there is creeper under it, destroy creeper under it, and attach ONE script to it per 10 seconds. Instead, it attaches 0-2 scripts to a core and does it 1 every .5 seconds and causes LAG.
Scripts run 30 times per second. Make a variable that increments every invocation, and
if(<-myTime <-DelayInSeconds mul(30) mod eq0)
#execute timed stuff
endif
Using delay is kinda sloppy and messes up timing. This method always works for me.
-GameGibu
Now it's not working at all. :-\ CODE:
$NeededAmount:50
#Self CONST_MAXAMMO <-NeededAmount SetUnitAttribute
if(<-creeperAmount <-NeededAmount gte)
if(<-myTime <-DelayInSeconds mul(30) mod eq0)
#execute timed stuff
0 5 RandInt ->Creep
150 delay
"CRPLCore" 33 26 12 RandCoordsInRange CreateUnit ->unit
if(<-Creep 1 eq)
<-unit "CCannon.crpl" AddScriptToUnit
endif
if(<-Creep 2 eq )
<-unit "CSniper.crpl" AddScriptToUnit
endif
if(<-Creep 3 eq)
<-unit "CSprayer.crpl" AddScriptToUnit
endif
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
endif
CurrentCoords 0 Setcreeper
<-creeperAmount <-creeperAmount sub ->creeperAmount
delay 30
endif
if(<-time neq0)
<-time <-time sub ->time
endif
I don't know if I did a newbie mistake.
At the beginning of your code MUST be
once
0 ->myTime
endonce
<-myTime add(1) ->myTime
You have to increment it. And lose the delay, that will break the whole thing!
If you want 10 seconds, replace <-DelayInSeconds with the number 10.
-GameGibu
Maybe I could just optimize your code for you if you want.
Quote from: GameGibu on January 15, 2016, 09:14:18 AM
At the beginning of your code MUST be
once
0 ->myTime
endonce
<-myTime add(1) ->myTime
You have to increment it. And lose the delay, that will break the whole thing!
If you want 10 seconds, replace <-DelayInSeconds with the number 10.
-GameGibu
Maybe I could just optimize your code for you if you want.
If you could find the bug in my script and point it out...
once
0 ->myTime
endonce
<-myTime add(1) ->myTime
$NeededAmount:50
#Self CONST_MAXAMMO <-NeededAmount SetUnitAttribute
if(<-creeperAmount <-NeededAmount gte)
if(<-myTime 10 mul(30) mod eq0)
#execute timed stuff
0 5 RandInt ->Creep
150 delay
"CRPLCore" 33 26 12 RandCoordsInRange CreateUnit ->unit
if(<-Creep 1 eq)
<-unit "CCannon.crpl" AddScriptToUnit
endif
if(<-Creep 2 eq )
<-unit "CSniper.crpl" AddScriptToUnit
endif
if(<-Creep 3 eq)
<-unit "CSprayer.crpl" AddScriptToUnit
endif
if(<-Creep)
<-unit "CMortar.crpl" AddScriptToUnit
endif
endif
CurrentCoords 0 Setcreeper
<-creeperAmount <-creeperAmount sub ->creeperAmount
endif
if(<-time neq0)
<-time <-time sub ->time
endif
...because I ran this at 4x speed for a while and nothing happened so I don't know if I'm supposed to rename stuff.
You could make another script that works, just make sure it spawns the units in this one and only this one because the custom units us textures that the PAC map overwrites so I changed the position of said scripts for only some of them. I will credit you if you do that. 8)
Working on optimizing code right now. It'll require you to rename some scripts, unless those scripts are referenced by name in any other scripts, in which case you need to tell me that as soon as possible so I can rewrite to correct for this.
-GameGibu
Quote from: GameGibu on January 15, 2016, 12:06:35 PM
Working on optimizing code right now. It'll require you to rename some scripts, unless those scripts are referenced by name in any other scripts, in which case you need to tell me that as soon as possible so I can rewrite to correct for this.
-GameGibu
Thanks! I'll credit you :D
So you are able to change your script names without screwing up the other scripts? I need confirmation on this to give you the best version of the code.
-GameGibu
Quote from: GameGibu on January 15, 2016, 04:36:26 PM
So you are able to change your script names without screwing up the other scripts? I need confirmation on this to give you the best version of the code.
-GameGibu
The names are used in
MULTIPLE scripts other than this one.
Aye, acknowledged.
Please list the (5?) script names that need to be applied
Then I can post the fixed code.
-GameGibu
Quote from: GameGibu on January 15, 2016, 05:32:45 PM
Aye, acknowledged.
Please list the (5?) script names that need to be applied
Then I can post the fixed code.
-GameGibu
There's more than 5.
- CMortar
- CShotMortar
- CCannon
- CShot
- CBeam
- CSniper
- CSprayer
These are case sensitive, and also add the .crpl on the back. Plus the CShot.crpl is used by more than one creep. Cheers :D
CShot is one of the towers that needs to be randomly spawned?
Almost done except for that...
-GameGibu
Assuming those scripts CMortarShot.crpl and CShot.crpl will be used by the towers this script is creating, here is your long-awaited optimized code:
Quote from: GameGibu on January 16, 2016, 12:04:43 AM
Assuming those scripts CMortarShot.crpl and CShot.crpl will be used by the towers this script is creating, here is your long-awaited optimized code:
Thanks! I'll put your credit in my map and in my map list! 8)
EDIT: For some strange reason, I can't comment out the (now) useless code.EDIT2: Ok, now it's working. Thanks again! :D