master programmer,
NPC stands for Non-Player Character.
Newbie to 3d,
To control multiple NPCs simultaneously, you must first setup a general NPC engine. Think of the engine as a set of rules and instructions that drive the characters, which is accually what it is. Technically, it is a load of IF statements, or what-not, that make the characters to do something, based off the rules given for each possible situation they can face.
To make each NPC follow these rules, simultaneously, you use arrays in connection with each different sprite. However, to create the engine, forget about the array, and code the rules for a single sprite, first. Elsewise, coding the engine can get really confusing.
Let us start out very basic. Let us make a single NPC that roams around in random directions(left, right, up or down), changing its direction at random times. This code should be easy to follow.
Example 1:
set display mode 800,600,32
sync on
sync rate 60
REM << use a box and text to create the NPC
ink rgb(255,0,0),0
box 0,0,text width("1") + 4,text height("1") + 4
ink rgb(255,255,255),0
text 2,2,"1"
get image 1,0,0,text width("1") + 4,text height("1") + 4
cls
REM << create and place NPC sprite from image
x = rnd(800)
y = rnd(600)
sprite 1,x,y,1
REM << initiate time duration and direction
time = timer()
duration = rnd(5000)
direction = rnd(3)
REM <<<<<<<<<<<<< main loop >>>>>>>>>>>>>>>
repeat
REM <<<<< the engine >>>>>
REM << make NPC to change directions at random lengths of time
if timer() - time > duration
time = timer()
duration = rnd(5000)
REM << change directions
direction = rnd(3)
endif
REM << make NPC to walk in new direction for the set duration of time
if direction = 0 then inc x
if direction = 1 then dec x
if direction = 2 then inc y
if direction = 3 then dec y
REM << update NPC location
sprite 1,x,y,1
sync
until mouseclick() = 1
delete sprite 1
end
Basically, we have a random movement engine for our single NPC. Now, we can implement this same code to work for a variable amount of NPCs, all at the same time. We do so by replacing the variables used, with an array or two. First, let us create more than one sprite NPC, each with their own unit number. Let us use a for next loop to do so, efficiently. Also, create an array, where each slot has 5 dimensions. Slot 1 holds the NPC's x coordinate, slot 2 = y coordinate, slot 3 = time, slot 4 = duration, and slot 5 = direction. Let us fill these slots from within the loop. Look below for what we have so far.
Example 2:
set display mode 800,600,32
sync on
sync rate 60
REM << create array to hold info. for 20 NPCs, and 5 dimensions of info. for each NPC
dim npcinfo(20,5)
REM << use a box and text to create the NPC
for t = 1 to 20
ink rgb(255,0,0),0
box 0,0,text width(str$(t)) + 4,text height(str$(t)) + 4
ink rgb(255,255,255),0
text 2,2,str$(t)
get image t,0,0,text width(str$(t)) + 4,text height(str$(t)) + 4
cls
REM << create and place NPC sprite from image
npcinfo(t,1) = rnd(800)
npcinfo(t,2) = rnd(600)
sprite t,npcinfo(t,1),npcinfo(t,2),t
REM << initiate time duration and direction
npcinfo(t,3) = timer()
npcinfo(t,4) = rnd(5000)
npcinfo(t,5) = rnd(3)
next t
Simply, lets use this same method around the engine. Let us wrap the engine in a for next loop, and replace all the variables with the array. This brings to life our map of fully simultaneously engined, working NPCs.
Example 3:
set display mode 800,600,32
sync on
sync rate 60
REM << create array to hold info. for 20 NPCs, and 5 dimensions of info. for each NPC
dim npcinfo(20,5)
REM << use a box and text to create the NPC
for t = 1 to 20
ink rgb(255,0,0),0
box 0,0,text width(str$(t)) + 4,text height(str$(t)) + 4
ink rgb(255,255,255),0
text 2,2,str$(t)
get image t,0,0,text width(str$(t)) + 4,text height(str$(t)) + 4
cls
REM << create and place NPC sprite from image
npcinfo(t,1) = rnd(800)
npcinfo(t,2) = rnd(600)
sprite t,npcinfo(t,1),npcinfo(t,2),t
REM << initiate time duration and direction
npcinfo(t,3) = timer()
npcinfo(t,4) = rnd(5000)
npcinfo(t,5) = rnd(3)
next t
REM <<<<<<<<<<<<< main loop >>>>>>>>>>>>>>>
repeat
REM <<<<< the engine >>>>>
REM << make NPC to change directions at random lengths of time
for t = 1 to 20
if timer() - npcinfo(t,3) > npcinfo(t,4)
npcinfo(t,3) = timer()
npcinfo(t,4) = rnd(5000)
REM << change directions
npcinfo(t,5) = rnd(3)
endif
REM << make NPC to walk in new direction for the set duration of time
if npcinfo(t,5) = 0 then npcinfo(t,1) = npcinfo(t,1) + 1
if npcinfo(t,5) = 1 then npcinfo(t,1) = npcinfo(t,1) - 1
if npcinfo(t,5) = 2 then npcinfo(t,2) = npcinfo(t,2) + 1
if npcinfo(t,5) = 3 then npcinfo(t,2) = npcinfo(t,2) - 1
REM << update NPC location
sprite t,npcinfo(t,1),npcinfo(t,2),t
next t
sync
until mouseclick() = 1
for t = 1 to 20
delete sprite 1
next t
end
Again said, this is just very basic, to show you the fundamentals of using an array or more, and the for next loop, to task mutiple NPCs simultaneously. The movement here is all random. However, it can get much more difficult and interesting than this, like using scripted movement, and boundary movement. It all depends on your program's needs, and the engine you build. Think about what you need, build the engine, then apply it to the NPCs. Note, that an engine doesn't have to be a set of instructions(sequential IF statements), but can be a formatted value file. That, the program reads the values within a text document, in a certain layout, to create a looped or scripted movement. Using this method, each NPC can be ran by the same engine, but have different set movements. Ever seen where an NPC follows the same path back and forth. Well, it is not hard-coded.
+NanoBrain+