Can't get this code to work

Grab your favourite IDE and tinker with the innards of game engines

Can't get this code to work

Postby TechieUK on Sat Apr 27, 2013 4:37 pm

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
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Can't get this code to work

Postby Corewarp on Sun Apr 28, 2013 5:08 am

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.
User avatar
Corewarp
Been Here A While
Been Here A While
 
Joined: Thu Mar 13, 2008 6:29 pm

Re: Can't get this code to work

Postby Stormy on Sun Apr 28, 2013 7:03 am

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};
User avatar
Stormy
May Contain Skills
May Contain Skills
 
Joined: Sun Nov 28, 2010 6:03 am
Location: Cairns, QLD, AUS

Re: Can't get this code to work

Postby Corewarp on Sun Apr 28, 2013 7:42 am

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!
User avatar
Corewarp
Been Here A While
Been Here A While
 
Joined: Thu Mar 13, 2008 6:29 pm

Re: Can't get this code to work

Postby TechieUK on Sun Apr 28, 2013 9:33 am

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. :(
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Can't get this code to work

Postby Stormy on Sun Apr 28, 2013 10:05 am

Woo! Progress!
User avatar
Stormy
May Contain Skills
May Contain Skills
 
Joined: Sun Nov 28, 2010 6:03 am
Location: Cairns, QLD, AUS

Re: Can't get this code to work

Postby Corewarp on Sun Apr 28, 2013 10:07 am

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)
User avatar
Corewarp
Been Here A While
Been Here A While
 
Joined: Thu Mar 13, 2008 6:29 pm

Re: Can't get this code to work

Postby TechieUK on Sun Apr 28, 2013 10:31 am

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.
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Can't get this code to work

Postby Corewarp on Sun Apr 28, 2013 11:32 am

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?
User avatar
Corewarp
Been Here A While
Been Here A While
 
Joined: Thu Mar 13, 2008 6:29 pm

Re: Can't get this code to work

Postby TechieUK on Sun Apr 28, 2013 11:43 am

Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Can't get this code to work

Postby Corewarp on Sun Apr 28, 2013 12:04 pm

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 ) ]"
User avatar
Corewarp
Been Here A While
Been Here A While
 
Joined: Thu Mar 13, 2008 6:29 pm

Re: Can't get this code to work

Postby TechieUK on Sun Apr 28, 2013 1:04 pm

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
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Can't get this code to work

Postby Corewarp on Sun Apr 28, 2013 1:31 pm

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.
User avatar
Corewarp
Been Here A While
Been Here A While
 
Joined: Thu Mar 13, 2008 6:29 pm

Re: Can't get this code to work

Postby TechieUK on Sun Apr 28, 2013 5:31 pm

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
Also know as WILLAM; TechieUK is my old name. If you see WILLAM on Steam or ModDB, it's me.
User avatar
TechieUK
Regular
Regular
 
Joined: Sat Apr 13, 2013 4:29 pm

Re: Can't get this code to work

Postby Sandern on Sun Apr 28, 2013 6:27 pm

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?
Sandern
Regular
Regular
 
Joined: Thu Jun 30, 2005 1:08 pm
Location: Netherlands
Next

Return to Programming

Who is online

Users browsing this forum: No registered users

cron