Need Help With Scripting

Quote from ChickenMobile on July 9, 2013, 2:26 amI got the following to work:
- Code: Select all
didSetCubes <- false;
DBG <- 1 //set to 0 once finished debugging//Change all the models of the cubes to custom ones
function setCubes() {
local baboo = null//retrieve all cubes with the name Test_Cube and set their model to metal_boy
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if(DBG)printl("found cube!")
if ( baboo.IsValid() ) baboo.SetModel("models/props/metal_boy.mdl")}
}
didSetCubes = true;
}setCubes() //Set all cubes models on level start
if(DBG)printl("setted the cubes! " + UniqueString())I will add IsValid to the list of Portal 2 functions on the wiki.
I got the following to work:
- Code: Select all
didSetCubes <- false;
DBG <- 1 //set to 0 once finished debugging//Change all the models of the cubes to custom ones
function setCubes() {
local baboo = null//retrieve all cubes with the name Test_Cube and set their model to metal_boy
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if(DBG)printl("found cube!")
if ( baboo.IsValid() ) baboo.SetModel("models/props/metal_boy.mdl")}
}
didSetCubes = true;
}setCubes() //Set all cubes models on level start
if(DBG)printl("setted the cubes! " + UniqueString())
I will add IsValid to the list of Portal 2 functions on the wiki.
Quote from Tmast98 on July 9, 2013, 2:42 amHey,
I copy and pasted exactly what you put in the code, and ran the map with it, and I got the same exact error. I put this in:
didSetCubes <- false;
DBG <- 1 //set to 0 once finished debuggingfunction setCubes() {
local baboo = nullwhile((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if(DBG)printl("found cube!")
if ( baboo.IsValid() ) baboo.SetModel("models/props/metal_boy.mdl")}
}
didSetCubes = true;
}setCubes()
if(DBG)printl("setted the cubes! " + UniqueString())How did you actually implement yours in your test map? I'm just using a logic_script with the entityscripts property this code, and having the logic_script RunScriptCode .1 seconds after an env_entity_maker spawns an entity. Is there something wrong in doing this?
Hey,
I copy and pasted exactly what you put in the code, and ran the map with it, and I got the same exact error. I put this in:
didSetCubes <- false;
DBG <- 1 //set to 0 once finished debugging
function setCubes() {
local baboo = null
while((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if(DBG)printl("found cube!")
if ( baboo.IsValid() ) baboo.SetModel("models/props/metal_boy.mdl")}
}
didSetCubes = true;
}
setCubes()
if(DBG)printl("setted the cubes! " + UniqueString())
How did you actually implement yours in your test map? I'm just using a logic_script with the entityscripts property this code, and having the logic_script RunScriptCode .1 seconds after an env_entity_maker spawns an entity. Is there something wrong in doing this?

Quote from ChickenMobile on July 9, 2013, 2:53 amYou do not even need to RunScriptCode as it will auto run because of setCubes() at the end of the code. To test out whether a script is working manually is to type in the console: ent_fire bignet runscriptfile o. It will tell you if there is any errors and where in the code or if the script you are referencing actually doesn't exist.
I assume your script is scripts/vscripts/o.nut
Notes on code:
[spoiler]I didn't understand what the IsValid() function check was for but I understand now. It is just a backup to check if the entity found with the name is actually a valid entity (or one that exists).
I believe if you delete the if ( baboo.IsValid() ) it will still work fine. Though I would keep it in anyway.There is no point in creating the didSetCubes global variable if you are not going to reference it later. Because the function setCubes() is executed on startup, I don't see why you need this at all.
Lastly, try to keep variable names descriptive. I'm not sure if baboo means anything in another language but I would have called it currentCube or foundCube. Remember you can add comments to your code for someone else to better understand by two slashes //[/spoiler]
You do not even need to RunScriptCode as it will auto run because of setCubes() at the end of the code. To test out whether a script is working manually is to type in the console: ent_fire bignet runscriptfile o. It will tell you if there is any errors and where in the code or if the script you are referencing actually doesn't exist.
I assume your script is scripts/vscripts/o.nut
Notes on code:
I believe if you delete the if ( baboo.IsValid() ) it will still work fine. Though I would keep it in anyway.
There is no point in creating the didSetCubes global variable if you are not going to reference it later. Because the function setCubes() is executed on startup, I don't see why you need this at all.
Lastly, try to keep variable names descriptive. I'm not sure if baboo means anything in another language but I would have called it currentCube or foundCube. Remember you can add comments to your code for someone else to better understand by two slashes //
Quote from Tmast98 on July 9, 2013, 2:55 amNo it's not, that's what I can't figure out. It is scripts/vscripts/cubeswitch.nut
I don't know where it is getting that o.nut from. Any idea?
One thing that might be helpful, I'm saving it as a .nut file, and the file type shows it as a .nut, but when I save it, I save it as a .txt file using Notepad++
No it's not, that's what I can't figure out. It is scripts/vscripts/cubeswitch.nut
I don't know where it is getting that o.nut from. Any idea?
One thing that might be helpful, I'm saving it as a .nut file, and the file type shows it as a .nut, but when I save it, I save it as a .txt file using Notepad++

Quote from ChickenMobile on July 9, 2013, 3:00 am.nut files are just pure code, it shouldn't matter what sort of encoding it is saved with.
Double check that your logic_script is actually pointing towards your script file.
script2.jpgClick the Manage button and highlight your script in the menu and click open source. If it doesn't open any file or the wrong one, then change it to suit. If there are multiple files in the dialog, then delete (-) the wrong one and add (+) the right one in.
script-2.jpgLastly: when you spawn the cube and change the cube model, it would be more efficient to change the model of the cube that you have just spawned.
This could probably be done by creating a trigger which the cube passes through when spawned.OnStartTouch > !activator > RunScriptCode > setCubeModel(self)
And in your script file:
- Code: Select all
function setCubeModel(cube){
cube.SetModel("models/props/metal_boy.mdl")
}So the final code would look something like this:
- Code: Select all
function setCubeModel(cube){
cube.SetModel("models/props/metal_boy.mdl")
}function setCubes() {
local baboo = nullwhile((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if ( baboo.IsValid() ) setCubeModel(baboo)
}
}setCubes()
printl("setcubes " + UniqueString());
.nut files are just pure code, it shouldn't matter what sort of encoding it is saved with.
Double check that your logic_script is actually pointing towards your script file.
Click the Manage button and highlight your script in the menu and click open source. If it doesn't open any file or the wrong one, then change it to suit. If there are multiple files in the dialog, then delete (-) the wrong one and add (+) the right one in.
Lastly: when you spawn the cube and change the cube model, it would be more efficient to change the model of the cube that you have just spawned.
This could probably be done by creating a trigger which the cube passes through when spawned.
OnStartTouch > !activator > RunScriptCode > setCubeModel(self)
And in your script file:
- Code: Select all
function setCubeModel(cube){
cube.SetModel("models/props/metal_boy.mdl")
}
So the final code would look something like this:
- Code: Select all
function setCubeModel(cube){
cube.SetModel("models/props/metal_boy.mdl")
}function setCubes() {
local baboo = nullwhile((baboo = Entities.FindByName(baboo,"Test_Cube")) != null){
if ( baboo.IsValid() ) setCubeModel(baboo)
}
}setCubes()
printl("setcubes " + UniqueString());
Quote from Tmast98 on July 9, 2013, 3:13 amHey, here is everything on the comp, I can't get this thing working, maybe looking at it will help you see what is wrong.
Hey, here is everything on the comp, I can't get this thing working, maybe looking at it will help you see what is wrong.
Quote from Tmast98 on July 9, 2013, 3:17 amOh, also there is only one file, and when I hit open source, it does not open it. How do I change it to suit? Under the manage list it is currently under scripts/vscripts/cubeswitch.nut
Oh, also there is only one file, and when I hit open source, it does not open it. How do I change it to suit? Under the manage list it is currently under scripts/vscripts/cubeswitch.nut

Quote from ChickenMobile on July 9, 2013, 3:26 amI have a feeling that your windows is being decieving and saving the file as cubeswitch.nut.txt. Enable file extensions on windows and remove the .txt from the end of your file.
Also why is your steamapps folder on your desktop? I doubt that would even work.
EDIT: I see your problem. Remove the scripts/vscripts in the path and it will work. It looks for scripts relative to that path.
e.g. chicken_scripts/o.nut NOT scripts/vscripts/chicken_scripts/o.nut
I have a feeling that your windows is being decieving and saving the file as cubeswitch.nut.txt. Enable file extensions on windows and remove the .txt from the end of your file.
Also why is your steamapps folder on your desktop? I doubt that would even work.
EDIT: I see your problem. Remove the scripts/vscripts in the path and it will work. It looks for scripts relative to that path.
e.g. chicken_scripts/o.nut NOT scripts/vscripts/chicken_scripts/o.nut
