Page 1 of 2

Can't get this code to work

PostPosted: Sat Apr 27, 2013 4:37 pm
by TechieUK
Hello all!
I'm back again with more coding troubles :smt039
This time, it's code from the VDC I can't work, however.

The code randomises models on spawn.

Here is the provided code:
Code: Select all
static const char* modelnames[] = {
"Model1", //0
"Model2", //1
"Model3", //2};

Here is the code edited to fit my needs:
Code: Select all
static const char* modelnames[] = {
"models/hacks/watermelon.mdl", //0
"models/manhack.mdl", //1
};

I also add
Code: Select all
PrecacheModel("models/manhack.mdl");
PrecacheModel("models/hacks/watermelon.mdl");

And replace
Code: Select all
SetModel("models/manhack.mdl);

With
Code: Select all
SetModel (modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]);

Then compile.

I get this error, however:
npc_manhack.cpp(2377) : error C2065: 'modelnames' : undeclared identifier


Any ideas?
Help would be appreciated! :D

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 5:08 am
by Corewarp
I can't spot the error. Specially because the error is "You didn't declare this", but you clearly did. Try an' remove the
Code: Select all
SetModel (modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]);


And just use

Code: Select all
SetModel("models/manhack.mdl);


Because the error is wonky. Try that and if it's still doing it, then you dun goofed somewhere else. Which is what I suspect.

I also rarely offer this kind of support, but try an' post your entire npc_manhack.cpp code on a pastebin and give us the link. Will be easier to spot an error that way.

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 7:03 am
by Stormy
Shooting in the dark here, but from a purely beginner syntactical glance, aren't you commenting out the closing bracket here?
Code: Select all
static const char* modelnames[] = {
"Model1", //0
"Model2", //1
"Model3", //2};

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 7:42 am
by Corewarp
Black_Stormy wrote:Shooting in the dark here, but from a purely beginner syntactical glance, aren't you commenting out the closing bracket here?
Code: Select all
static const char* modelnames[] = {
"Model1", //0
"Model2", //1
"Model3", //2};


Good eyes Stormy.
He is indeed commenting out the ' }; '. Try fixing that!

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 9:33 am
by TechieUK
I didn't notice that, but it seems fine for me.
Code: Select all
static const char* modelnames[] = {
"models/hacks/watermelon.mdl", //0
"models/manhack.mdl", //1
};

Apparently the problem is line 2377, which is this code:
Code: Select all
SetModel (modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]);

Should I try
Code: Select all
SetModel ("modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]");

Instead?

EDIT:
Whoa! That removed every single error!
After that though, it crashed HL2 with an error saying
Code: Select all
modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]

Wasn't precached, so I added:
Code: Select all
PrecacheModel ("modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]");

And now HL2.exe seems to crash when a Manhack is spawned. :(

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 10:05 am
by Stormy
Woo! Progress!

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 10:07 am
by Corewarp
TechieUK wrote:
Code: Select all
modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]

Wasn't precached, so I added:
Code: Select all
PrecacheModel ("modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]");

And now HL2.exe seems to crash when a Manhack is spawned. :(


Don't use the randomizer to precache. Precache ALL the models manually!!
(Worst case scenario, it doesn't precache the right model because new random)

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 10:31 am
by TechieUK
When I precached them all randomly I got an error saying the randomizer wasn't precached.
If I do get this working, I'll probably edit the code on VDC so other people won't have the problem.

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 11:32 am
by Corewarp
TechieUK wrote:When I precached them all randomly I got an error saying the randomizer wasn't precached.
If I do get this working, I'll probably edit the code on VDC so other people won't have the problem.


Could we have the code in a pastebin and a link to this VDC article we keep hearing aboot?

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 11:43 am
by TechieUK

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 12:04 pm
by Corewarp
This
Code: Select all
PrecacheModel ("modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]");

Doesn't work.

"modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]"
That's a string ^. You'll note the ""'s

As I said, just precache all the models MANUALLY.
EG:
Code: Select all
PrecacheModel("models/manhack/model0.mdl");
PrecacheModel("models/manhack/model1.mdl");
etc..


And place this piece of code:
Code: Select all
static const char* modelnames[] = {
        "models/hacks/watermelon.mdl", //0
        "models/manhack.mdl", //1
        };


in this function:
Code: Select all
void CNPC_Manhack::Spawn(void)


Again. This piece of code gives the function SetModel the wrong string.
Code: Select all
SetModel ("modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]");

What you want to give it is:
Code: Select all
"models/hacks/watermelon.mdl"
OR
"models/manhack.mdl"

But what you're really giving it is this:
Code: Select all
"modelnames[ random->RandomInt( 0, ARRAYSIZE(modelnames) - 1 ) ]"

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 1:04 pm
by TechieUK
It still doesn't want to work. Am I missing something? It still closes when you spawn one. I have "npc_create npc_manhack" bound to 'p'.
http://pastebin.com/2nhcPkRa

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 1:31 pm
by Corewarp
Try changing
Code: Select all
SetModel ("models/hacks/watermelon.mdl");

to
Code: Select all
SetModel("models/manhack.mdl");


If it still doesn't work, then you know that it's the model handling(Which is nothing you've messed with) that is wrong.

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 5:31 pm
by TechieUK
That does seem to have fixed something. They can spawn without a crash. However, then only assume the form of the regular manhack model. I'm assuming the crash could even have been a problem with the watermelon.mdl

Re: Can't get this code to work

PostPosted: Sun Apr 28, 2013 6:27 pm
by Sandern
TechieUK wrote:That does seem to have fixed something. They can spawn without a crash. However, then only assume the form of the regular manhack model. I'm assuming the crash could even have been a problem with the watermelon.mdl

Run from the debugger and see where it crashes?