Enemy Behaviors: Detection and Reaction
Tosanu
Member, PRO Posts: 388
I am attempting to improve the challenge in my platformer run and fight game by creating more complex enemy challenges. My current goal is to make an enemy that jumps back and away if the player gets too close or attacks, so that you have to jump after it and either lead your shot or hit in mid jump.
However, although I can have the player's position tracked for the enemy to watch for closeness with a magnitude(X- Player x and y) to jump, i cannot do the same for the player's attacks. I could constrain a pair of attributes to watch its position when it fires, but that would limit the weapons in the game to one shot at a time. Is there another way to get the monster watching for something to come within a certain distance of it?
Comments
You could constrain a larger, invisible actor to your player and treat that as a hit zone. So anytime another actor touches this larger hit zone you could do what you need to do.
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
@Tosanu, in other coding languages you would use arrays to do this kind of tracking, well, GS has tables. They act wonderfully as multidimensional arrays.
Get a good understanding of all the table functions, then you can do exactly what you need with them.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
@jamie_c I sort of see that, but how do I make sure they interact properly? I can't use a global attribute, it would limit me to one of the actor per scene, which is part of what I'm trying to dodge. And my method of game creation is antithetical to making in-scene alterations partly due to system limitations (i cant make large scenes without using spawning behaviors, so there's nothing in-scene to edit).
@Hopscotch I've gotten used to a lot of basic functions of tables, particularly using it to assign positions for spawning and character behaviors, but I'm afraid I'm just not clear on what I could do with them here. Is there a way to have it watch positions or the like?
@Tosanu, sure, within reason. How many shots would need to be tracked at once? Just to give you a ballpark figure, 10 enemies tracking the magnitude of 10 bullets should be lag free. My tests on a totally dynamic tower defence game was still smooth at 20 enemies and 20 towers (plus spraying bullets!). Even higher numbers were still acceptable, although probably not ideal for an action runner.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
3 shots. The player never fires more than 3. I use a reflection mechanic for enemy shots from time to time, but they wont need to react the same way.
So im imagining this as a sort of timer based work, in which every .x seconds you have the shot write its coordinates in the table, or something like that. But it seems to have a lot of holes (how does the shot know what row its in and so on) as I imagine it, so I MUST be missing some ideas.
You are on the right track. I am not able to explain it in detail right now.
The short version:
Have a Shots table with X and Y as columns.
As a shot gets spawned, it adds itself to the table and sets a self.MyRow attribute equal to its row number. This is then used to update the X and Y.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
ok, I get that part, and i can sorta see how the enemy might grab that data and make its own self.magnitude attribute with that. So far what is killing me is "how do i then wipe and replace the row while keeping the others balanced." and also "how does the shot know what row to add to before getting .MyRow." not to mention how the enemy can track all the shots without losing track of the rows.
@Tosanu,
above was a little too short an explanation
I found that making a table with the maximum possible rows, and columns for X ,Y and UsedFlag.
When adding an actor to the table, use the function TableSearch to find the first unused row (UsedFlag=0)
Give the actor this as his .MyRow value and set the UsedFlag column to 1.
Then update the X,Y table columns to the appropriate X,Y positions as often as needed.
Enemies can the search the table for the shot with the smallest magnitude distance and act accordingly.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
ok, two followup questions, since im starting to get a better picture.
One: what happens when you run out of rows? thats only 100 after all. is there a way to cycle back around? Or to clear the row once the shot despawns?
Two:: i dont think i have table search. Im using the most recent windows release, but not any nightlies.
Sorry for the double post, but I had an idea, and im wondering if this would work.
My problem with Collision boxes have been that i dont want to use globals to determine if they are touching, because that would cause all copies of the linked creature to react. So i think i can use a table.
I will spawn the creature and box with an Ident number, which i will tie into a boolean column on a table. The boolean will react to collision on the box, and constrain a second boolean on the creature. When the boolaen flips from the collision, it should flip the creature's boolean to true as well, and that would activate the rule. Woudl this work, or does constrain not work on table data?
@Tosanu, oh you can have much much more than 100 rows.
I left out that once a shot despawns, you set the column value UsedFlag to 0 (instead of deleting it) This will allow new shots to reuse those rows. Hence the TableSearch in previous post.
When I said "use the maximum possible rows" I did mean as needed for your scene. That is in your case the number of shots you expect to be active at any one time. (Note to self ... should not be answering forum questions while in meetings)
The stable version 0.11.0.13 has the new Table function is.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
I'm a PC user, I don't have that version, do i?
@Tosanu, no you don't, I'm sorry for not checking.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
It's ok. Anyways, the idea i suggested, do you know if itll work?
I got it most of the way, but for some reason, the creature doesnt notice when the table turns back to false. Thats where im stuck now.
To your suggestion:
In the current win version not, but a change table value in a timer will work just fine.
Is the creature referencing the correct table row? Have you tried wraping the rule in a timer.
You can use the debugger to trace what your code is doing, or which values attributes have by adding "Log Debugging Statement" behaviours.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
I do not know why. But it does not accept the second change. When the collision happens, the table switches to true. But after 1.2 seconds it is supposed to change back, but never does. Is there a limit to changing table values?
I have a display text on the bounding box watching the status of the cell. For some reason, it switches to True when the field is contacted. But even when the field changes color back (i track when the field changes back to false as well) the cell stays True.
In other words, the only thing that wont change is the actual table cell. It only takes the first input for some reason
Maybe post a screenshot of your rules. Without seeing something it is hard to answer something specific.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
Since it seems to be ENTIRELY on this behavior table change (i shut off EVERYTHING else and for some reason the table cannot handle benig asked to changed from True to False. Even if i start it as true, it wont switch back. It will only go from false to true.
Can you try changing the column to integer and using 1 or 0?
I seem to remember someone else having issues with booleans in tables.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
I just did that, and it works. Right then. Argh. The good news is it even copies over into the boolean properly. So is this a known table glitch, possibly?
The good news is now the creature reacts exactly as i wanted it to when threatened.
Cool! Always the small things. I guess it is a bug then with booleans.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
I dont know how to submit that for bug review. But since this is an older version, is it worth doing?
They are starting work on updating the windows version next week as far as I know. So it may be good to report it now so that it gets attention.
Under "Help" in the main menu of creator is the "Report a bug" option.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com