Use Lua to define NPC behavior in the C gaming engine

I am using Lua to perform NPC behavior in C game engine. I encountered some problems during the design process.

For those that require multiple frames to execute For everything, I want to use a linked process list (this is type C). So this:

goto(point_a)
say("Oh dear, this lawn looks really scruffy!")
mowLawn()

A GotoProcess object will be created which will have a pointer to a SayProcess object, which will have a pointer to a MowLawnProcess object. These objects will It is created immediately when the NPC is generated, no further scripting is required.
The first of these objects will be updated every frame. After completion, it will be deleted, and the next one will be used for updating.
I extend it through ParallelProcess With this model, ParallelProcess contains multiple processes that update at the same time.

I found some serious problems. Take a look at this example: I want a character to go to point_a and then go berserk, and then attack any approach的人. The script looks like this:

goto(point_a)
while true do
character = getNearestCharacterId()
attack( character)
end

According to my design, this does not work at all. First, when the character does not even start to walk to point_a, the character variable will be set at the beginning. Then, due to the while loop , The script will continue to add AttackProcesses permanently.

I can implement a WhileProcess for the loop and evaluate the script line by line. I suspect this will increase the readability of the code.

Are there any other common The method I did not think of to solve this problem?

I think the method you provide will lose many of the advantages of using a scripting language. It will break the conditions and Loop.

To use coroutines, what you really need to do is:

npc_behaviour = coroutine.create(
function()
goto(point_a)
coroutine.yield()
say("Oh dear, this lawn looks really scruffy!")
coroutine.yield()
mowLawn( )
coroutine.yield()
end
)

goto, say, and mowLawn return immediately but start operations in C. Once C completes these operations, it will Call coroutine.resume(npc_behaviour)

In order to avoid all the benefits, you can hide them in functions such as goto, or do what I did with the waitFor function, such as:

< /p>

function waitFor(id)
while activeEvents[id] ~= nil do
coroutine.yield()
end
end

activeEvents is just a Lua table that keeps track of everything currently in progress-so goto will add ID to the table when it is started, and delete it when it is done, and then every time the operation is completed, all coroutines are activated to check them Whether the waiting action has been completed.

I am using Lua for a C game engine for NPC behavior. I encountered some problems during the design process.

p>

For everything that requires multiple frames to execute, I want to use a linked list of processes (this is type C). So this:

goto(point_a )
say("Oh dear, this lawn looks really scruffy!")
mowLawn()

A GotoProcess object will be created, which will have a pointer to the SayProcess object, which will have a pointer to the MowLawnProcess object. These objects will be spawned in the NPC When the time is created immediately, no further scripting is required.
The first of these objects will be updated every frame. After completion, it will be deleted, and the next will be used for updates.
I extended this model through ParallelProcess ,ParallelProcess contains multiple processes that update at the same time.

I found some serious problems. Take a look at this example: I want a character to go to point_a and then go berserk, and then attack anyone close to it. The script looks like this:

goto(point_a)
while true do
character = getNearestCharacterId()
attack(character)< br />end

According to my design, this does not work at all. First, when the character does not even start to walk to point_a, the character variable will be set at the beginning. Then, due to the while loop, the script will Continue to add AttackProcesses permanently.

I can implement a WhileProcess for the loop and evaluate the script line by line. I suspect this will increase the readability of the code.

Is there any other common method for me Didn’t think of solving this problem?

I think the method you provide will lose many of the advantages of using a scripting language. It will break conditions and loops.

To use coroutines, what you really need to do is:

npc_behaviour = coroutine.create(
function()
goto(point_a)
coroutine.yield()
say("Oh dear, this lawn looks really scruffy!")
coroutine.yield()
mowLawn()
coroutine.yield()
end
)

goto, say, and mowLawn return immediately but start operations in C. Once C completes these operations, it calls coroutine.resume(npc_behaviour)

< p>In order to avoid all the benefits, you can hide them in functions such as goto, or do what I did with the waitFor function, such as:

function waitFor(id)< br /> while activeEvents[id] ~= nil do
coroutine.yield()
end
end

activeEvents is just a Lua table, it keeps track of what is currently going on All things-so goto will add the ID to the table when it is started and delete it when it is done, and then every time the operation is completed, all the coroutines are activated to check whether the action they are waiting for has completed.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3250 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.